Search code examples
androidkotlinandroid-recyclerviewandroid-databindingandroid-mvvm

Recycler View selected item color not changing properly


I am stuck in this problem. My issue is, I implemented the color change of Recycler view item click. But when i selected 1st item, only that got selected and its fine. But when select 2nd item, 1st and 2nd item got selected. When i selected 5th item, 1,2 & 5th got selected. I want clicked row item only get selected and others not. Kindly help.

My code(using Kotlin & MVVM) is here,

class ItemAdapter() : RecyclerView.Adapter<ItemAdapter.DateViewHolder>() {

    private var ItemList: MutableList<ItemModel>? = ArrayList()
    private lateinit var ItemViewModel: ItemListBinding
    private lateinit var listener: OnItemClickListener

    init {
        this.ItemList = arrayListOf()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DateViewHolder {
        ItemViewModel = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context), R.layout.item_list,
            parent, false
        )
        return DateViewHolder(ItemViewModel)
    }

    override fun onBindViewHolder(holder: DateViewHolder, position: Int) {
        holder.bindItemDetail(ItemList!![position])
    }

    override fun getItemCount(): Int {
        return ItemList!!.size
    }

    fun setItemList(List: MutableList<ItemModel>) {
        this.ItemList = List
        notifyDataSetChanged()
    }

    fun setListener(listener: OnItemClickListener) {
        this.listener = listener
    }

    fun removeAt(selectedPos: Int) {
        ItemList?.removeAt(selectedPos)
        notifyItemRemoved(selectedPos)
    }


    inner class DateViewHolder(private var itemDetailBinding: ItemListBinding) :
        RecyclerView.ViewHolder(itemDetailBinding.root) {

        fun bindItemDetail(ItemResponse: ItemModel) {

            if (itemDetailBinding.ItemDetailModel == null) {
                itemDetailBinding.ItemDetailModel =
                    ItemDetailViewModel(ItemResponse, itemView.context)
            } else {
                itemDetailBinding.ItemDetailModel!!.setDetail(ItemResponse)
                itemDetailBinding.executePendingBindings()
            }

            itemDetailBinding.root.Detail.setOnClickListener {
                if(position == adapterPosition) {
                    itemDetailBinding.root.itemDescp.setTypeface(Typeface.DEFAULT_BOLD)
                    itemDetailBinding.root.Detail.setBackgroundResource(R.color.colorGreyLight)
                    itemView.isSelected = false
                } else {
                    itemDetailBinding.root.itemDescp.setTypeface(Typeface.DEFAULT)
                    itemDetailBinding.root.Detail.setBackgroundResource(R.color.colorWhite)
                    itemView.isSelected = true
                }
                notifyDataSetChanged()
            }

        }
    }
}

xml is here

  <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/Detail"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:background="@color/colorWhite">

            <TextView
                android:id="@+id/itemDescp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="1dp"
                android:layout_marginEnd="60dp"
                android:ellipsize="end"
                android:maxLines="1"
                android:text="@{ItemDetailModel.description}"
                android:textAlignment="textStart"
                app:layout_constraintRight_toLeftOf="@+id/qtyValue"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/qtyValue"
                android:layout_width="40dp"
                android:layout_height="25dp"
                android:layout_marginRight="3dp"
                android:background="@drawable/edit_shape"
                android:maxLines="1"
                android:paddingLeft="5dp"
                android:paddingTop="3dp"
                android:paddingRight="7dp"
                android:paddingBottom="3dp"
                android:text="@{ItemDetailModel.quantity}"
                android:textAlignment="textEnd"
                android:textSize="16sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

Any help would be deeply appreciated. Thanks


Solution

  • Add

    private var selectedItemPosition = -1
    

    And then

    inner class DateViewHolder(private var itemDetailBinding: ItemListBinding) :
        RecyclerView.ViewHolder(itemDetailBinding.root) {
    
        fun bindItemDetail(ItemResponse: ItemModel) {
    
            if (itemDetailBinding.ItemDetailModel == null) {
                itemDetailBinding.ItemDetailModel =
                    ItemDetailViewModel(ItemResponse, itemView.context)
            } else {
                itemDetailBinding.ItemDetailModel!!.setDetail(ItemResponse)
                itemDetailBinding.executePendingBindings()
            }
            if(position == selectedItemPosition) {
                itemDetailBinding.root.itemDescp.setTypeface(Typeface.DEFAULT_BOLD)
                itemDetailBinding.root.Detail.setBackgroundResource(R.color.colorGreyLight)
                itemView.isSelected = true
            } else {
                itemDetailBinding.root.itemDescp.setTypeface(Typeface.DEFAULT)
                itemDetailBinding.root.Detail.setBackgroundResource(R.color.colorWhite)
                itemView.isSelected = false
            }
    
            itemDetailBinding.root.Detail.setOnClickListener {
                selectedItemPosition = position
                notifyDataSetChanged()
            }
    
        }