I have a nested recycler view where the user should only be able to pick one single item inside the child recycler views. When an item is clicked on it is highlighted. Unfortunately the user is able to highlight an item in each child recycler view instead of just one overall.
How can I achieve only one highlighted item?
Here is my code so far:
Parent onBindViewholder:
override fun onBindViewHolder(viewHolder: ParentViewHolder, position: Int) {
//set the name of the parentitem
val parentItem = parentData[position]
viewHolder.ParentTitle.text = parentItem.ParentName
// bind child adapter
val childAdapter = ChildAdapter(parentData[position].childList, parentData[position].parentName)
viewHolder.childRecycler.layoutManager = LinearLayoutManager(viewHolder.childRecycler.context, LinearLayoutManager.VERTICAL,false)
viewHolder.childRecycler.adapter = childAdapter
}
Here is my Child Adapter:
class ChildAdapter(
list: MutableList<ChildItem> = mutableListOf(),
ParentName : String
) : RecyclerView.Adapter<ChildAdapter.ChildViewHolder>() {
var ChildData: MutableList<ChildItem> = list.toMutableList()
set(value) {
field = value
notifyDataSetChanged()
}
var selectedItem = -1
/**
* ViewHolderClass
*/
inner class ChildViewHolder(view: View, ParrentName: String, childList : MutableList<ChildItem>) :
RecyclerView.ViewHolder(view) {
var childItem : LinearLayoutCompat
init {
childItem = view.findViewById(R.id.child_item)
childItem.setOnClickListener {
if (adapterPosition == RecyclerView.NO_POSITION) return@setOnClickListener;
notifyItemChanged(selectedItem , 0)
selectedItem = adapterPosition
notifyItemChanged(selectedItem , 1)
}
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ChildViewHolder {
val viewHolder = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.child_item, viewGroup, false)
return ChildViewHolder(viewHolder,parentName, childData)
}
override fun onBindViewHolder(viewHolder: ChildViewHolder, position: Int, payloads: MutableList<Any>) {
val childItem = childData[position]
if(payloads.isNotEmpty()){
viewHolder.itemView.setBackgroundColor(if (selectedItem == position) Color.GREY else Color.WHITE)
}
}
override fun onBindViewHolder(viewHolder: ChildViewHolder, position: Int) {
val childItem = childData[position]
viewHolder.itemView.setBackgroundColor(Color.WHITE)
}
By clicking on the item in the child recycler view the item is highlightes. But since the Parentrecycler viwe has multiple child recycler views, an item in evry hild can be highlighted.
I’d like to reset the highlighting if the user change his selection from one child recycler into another child recycler.
Can anyone help with this?
I’ll tried with LiveData but never worked with it before and not quiet sure how to use it