Home > Software engineering >  Dynamically add item into recyclerview when button pressed
Dynamically add item into recyclerview when button pressed

Time:12-23

I am trying to build a layout in which there is a recyclerview where each item has three edittexts horizontally alligned. I want to add the next item when a button is pressed. This the design I am trying to build

This is the Adapter class for the recycler view

class AddProjectAdapter(private val itemsList : ArrayList<ItemDetails>) : RecyclerView.Adapter<AddProjectAdapter.AddProjectViewHolder>() {

    inner class AddProjectViewHolder(private val binding : AddProjectItemViewBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item : ItemDetails){
            binding.item = item
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddProjectViewHolder{
           return AddProjectViewHolder(AddProjectItemViewBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: AddProjectViewHolder, position: Int) {
        holder.bind(itemsList[position])
    }

    override fun getItemCount(): Int = itemsList.size

}

Layout file for the item add_project_item_view.xml `

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="item"
            type="com.example.hero.models.ItemDetails" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.card.MaterialCardView
            android:id="@ id/item_name_cardview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:flow_horizontalBias="3"
            app:cardBackgroundColor="@color/light_background"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="@id/g_v_1"
            android:layout_marginRight="5dp">

            <EditText
                android:id="@ id/item_name_edittext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Item Name"
                android:text="@={item.itemName}"
                android:textColor="@color/t_dark"
                android:fontFamily="@font/metropolislight"
                android:textSize="14dp"
                android:textColorHint="@color/t_dark"
                android:background="@null"
                android:padding="10dp" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@ id/rate_unit_cardview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:flow_horizontalBias="2"
            android:layout_marginRight="5dp"
            app:cardBackgroundColor="@color/light_background"
            app:layout_constraintStart_toEndOf="@id/g_v_1"
            app:layout_constraintEnd_toEndOf="@id/g_v_2">

            <EditText
                android:id="@ id/rate_unit_edittext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Rate/Unit"
                android:text="@={item.rate}"
                android:textColorHint="@color/t_dark"
                android:textColor="@color/t_dark"
                android:fontFamily="@font/metropolislight"
                android:textSize="14dp"
                android:padding="10dp"
                android:background="@null" />
        </com.google.android.material.card.MaterialCardView>

        <com.google.android.material.card.MaterialCardView
            android:id="@ id/unit_cardview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:flow_horizontalBias="2"
            app:cardBackgroundColor="@color/light_background"
            app:layout_constraintStart_toEndOf="@id/g_v_2"
            app:layout_constraintEnd_toEndOf="parent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@ id/unit_textview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:background="@null"
                    android:fontFamily="@font/metropolislight"
                    android:hint="Unit"
                    android:padding="10dp"
                    android:text="@={item.unit}"
                    android:textColor="@color/t_dark"
                    android:textColorHint="@color/t_dark"
                    android:textSize="14dp" />

                <ImageView
                    android:id="@ id/drop_down_imageview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@drawable/dropdown"
                    android:layout_centerVertical="true" />
            </RelativeLayout>
        </com.google.android.material.card.MaterialCardView>

        <androidx.constraintlayout.widget.Guideline
            android:id="@ id/g_v_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.4" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@ id/g_v_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.8" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

`

//code of the activity in which the recycler view is present

private val itemsList : ArrayList<ItemDetails> = ArrayList()
private lateinit var addProjectAdapter: AddProjectAdapter
private lateinit var binding: ActivityAddProjectBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityAddProjectBinding.inflate(layoutInflater)
    setContentView(binding.root)

    setAdapter()
    setListner()
}

private fun setAdapter(){
    addProjectAdapter = AddProjectAdapter(itemsList)
    binding.itemDetailsRecyclerview.apply {
        layoutManager = LinearLayoutManager(this@AddProject)
        adapter = addProjectAdapter
    }
}

override fun onClick(v : View?) {
    when(v?.id){
        binding.addItemButton.id -> {
            itemsList.add(ItemDetails())
            addProjectAdapter.notifyItemInserted(itemsList.size-1)
        }
    }
}

private fun setListner(){
    binding.addItemButton.setOnClickListener(this)
}
data class ItemDetails(
var itemName : String = "",
var rate : String = "",
var unit : String = "")

And my recyclerview id is item_details_recyclerview. Output for the above code

When I click the add button the item added is not visible but the add button moves down. Can someone help me out with this issue? And also if you could provide some better approach it would be great. Thanks in advance.

CodePudding user response:

Change height of ConstraintLayout from add_project_item_view.xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

to this

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
  • Related