我有一个嵌套的回收器视图,用户应该只能在子回收器视图中选择一个项目。单击某个项目时,该项目会突出显示。不幸的是,用户能够突出显示每个子回收器视图中的一项,而不是仅突出显示整个项目。 如何才能只实现一项突出显示的项目?
这是迄今为止我的代码:
父级 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
}
这是我的儿童适配器:
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)
}
通过单击子回收器视图中的项目,该项目会突出显示。但由于 Parentrecycler 视图具有多个子回收器视图,因此可以突出显示 evry hild 中的项目。 如果用户将其选择从一个子回收器更改为另一个子回收器,我想重置突出显示。
有人可以帮忙吗? 我会尝试使用 LiveData,但以前从未使用过它,并且不确定如何使用它
我找到了解决问题的方法:
对于子项,我添加了一个变量 isSelected
data class ChildItem(
var ChildName : String,
var isSelected: Boolean = false)
向 ChildAdapter 添加了代码,以便只能选择一项。另外,再次单击该项目即可选择/取消选择该项目:
类 ChildAdapter( 列表:MutableList = mutableListOf(), 父名称:字符串
) : 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)
}
在ParentAdapter的onClick中我添加了一个函数。如果单击了父项,则更新子项中的 isSelected 并通知适配器:
fun deselectItem(name:ChildItem){
val adapter = ParentDialog?.parentRecycler?.adapter
if(adapter != null) {
val childList =
(adapter as ParentAdapter).ParentData.flatMap { it.childList }
for (item in childList) {
if(item != name) {
if (item.isSelected) {
item.isSelected = false
}
}
}
parentDialog?.ParentRecycler?.adapter?.notifyDataSetChanged()
}
}
这有效。但可以改进很多。我迭代整个子列表并使用notifyDataSetChange。 更好的方法是仅更新之前选择的 childItem,并且仅使用 notificationItemChanged(index) 更新 Parent 中相应适配器上的此项。不幸的是我现在不知道如何实现这一点。