Skip to content Skip to sidebar Skip to footer

How To Show Single Item Selected In Recyclerview Using Kotlin

How can we mark single item is selected in Recyclerview using kotlin. When I select an item and after that click on other item then previously selected item should be dis-selected.

Solution 1:

Here dataItem is Model Class and please take one extra Boolean variable isSelected in model class (default value is false) and true when select item then true this variable on selected position, below are example :

class AllModuleListAdapter(
var context: Context?,
private var moduleList: ArrayList<DataItem?>?,
private var mCallback: DeviceClickListener
) :
RecyclerView.Adapter<AllModuleListAdapter.MyViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val context = parent.context
    val itemView = LayoutInflater.from(context).inflate(R.layout.item_modules, parent, false)
    return MyViewHolder(itemView)
}

override fun getItemCount(): Int {
    return moduleList?.size ?: 0
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    holder.bind(moduleList?.get(position))

    if (moduleList?.get(position) is DataItem) {
        val dataItem = moduleList?.get(position) as DataItem
        if (dataItem.isSelected) {
            context?.let { ContextCompat.getColor(it, R.color.colorOrange) }
                ?.let { holder.itemView.setBackgroundColor(it) }

        } else {
            context?.let { ContextCompat.getColor(it, R.color.white) }
                ?.let { holder.itemView.setBackgroundColor(it) }

        }
    }

}

inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    init {
        itemView.constraint_main.setOnClickListener {
            val list = moduleList as List<DataItem>
            for (item in list.indices) {
                list[item].isSelected = false
            }
            list[adapterPosition].isSelected = true

            mCallback.onDeviceClick(adapterPosition)
            notifyDataSetChanged()

            context?.let { it1 -> ContextCompat.getColor(it1, R.color.colorOrange) }?.let { it2 ->
                itemView.constraint_main?.setBackgroundColor(it2)
            }

        }

    }

    fun bind(module: DataItem?) {
        itemView.txtModuleName?.text = module?.id.toString()
        itemView.txtSignal?.text = module?.room
    }

 }
}

Solution 2:

        if (mPosition == position)
        {
            //set selected here
        } else
        {
            //set unselected here
        }

        holder.parentView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                mPosition = position;
                notifyDataSetChanged();
            }
        });

Write above code in onBindViewholder and declare mPosition as global int variable in adapter class


Solution 3:

Try with this:- take one variable in your ListModel class as

var selected:boolean  = false

then while setting the listModel items set this value as false as

for(int i=0;i<listModel.size;i++){
   listModel.get(i).selected = false
}//this is for setting all values false

when you select any item from list call this method and then set selected = true for selected position and simply refresh the adapter list.

in your adapter check for this selected value and accordingly set the check box value inside your bindItems method

  itemView.checkBox.selected = items.selected//this will set your checkbox selected value

Post a Comment for "How To Show Single Item Selected In Recyclerview Using Kotlin"