[英]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.我的问题是,我实现了 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. But when select 2nd item, 1st and 2nd item got selected. When i selected 5th item, 1,2 & 5th got selected.当我选择第 5 项时,第 1,2 和第 5 项被选中。 I want clicked row item only get selected and others not.我希望单击的行项目只能被选中,而其他人则不会。 Kindly help.请帮助。
My code(using Kotlin & MVVM) is here,我的代码(使用 Kotlin 和 MVVM)在这里,
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 xml在这里
<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谢谢
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()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.