![](/img/trans.png)
[英]how to handle click event for each item in recyclerview child in nested recyclerview
[英]Not getting click event on parent recyclerview's item in nested recyclerview situation. How to get for both recyclerviews?
这是ui设计
Parent RecyclerView-> cardview items-> Child RecyclerView-> custom layout items
要求是在这种嵌套的 recyclerview 情况下必须有一个选择。我能够在嵌套的 recyclerview 中获得所选子级的 position 但我也想知道存在嵌套 recyclerview 的父级 recyclerview 子级的 position。 看起来父 recyclerview 正在将点击事件传递给其嵌套 recyclerview 的子级。 请建议我怎样才能做到这一点。 谢谢。
您将不得不手动冒泡一个事件,子回收器视图中的子级获取 onClickListener,它附加一些信息,将其传递给其父级(子级回收器视图),然后子级回收器视图可以附加自身并将其传递给主父母。
主Parent必须传入一个回调,子recyclerView也需要做同样的事情。
代码可能如下所示:
class MainParent {
val list = mutableListOf<ChildParent>()
private val mainParentCallbackImpl = object : MainParentCallback {
override fun childParentPressed(childParent: ChildParent) {
//do whatever you need to do
}
}
init {
list.add(ChildParent(mainParentCallbackImpl))
}
interface MainParentCallback {
fun childParentPressed(childParent: ChildParent)
}
}
class ChildParent(private val parentCallback: MainParent.MainParentCallback) {
val list = mutableListOf<Child>()
private val childParentCallbackImpl = object : ChildParentCallback {
override fun childPressed(child: Child) {
//do whatever you need to do first
parentCallback.childParentPressed(this@ChildParent)
}
}
init {
list.add(Child(childParentCallbackImpl))
}
interface ChildParentCallback {
fun childPressed(child: Child)
}
}
class Child(private val childParentCallback: ChildParent.ChildParentCallback) :
View.OnClickListener {
override fun onClick(v: View?) {
//do whatever you need to do first
childParentCallback.childPressed(this)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.