[英]Not getting click event on parent recyclerview's item in nested recyclerview situation. How to get for both recyclerviews?
This is the ui design这是ui设计
Parent RecyclerView-> cardview items-> Child RecyclerView-> custom layout items
Requirement is that there has to be single selection in this nested recyclerview situation.I am able to get the position of the selected child inside the nested recyclerview but I also want to know the position of the parent recyclerview child inside which nested recyclerview is present.要求是在这种嵌套的 recyclerview 情况下必须有一个选择。我能够在嵌套的 recyclerview 中获得所选子级的 position 但我也想知道存在嵌套 recyclerview 的父级 recyclerview 子级的 position。 Looks like parent recyclerview is passing down the click events to the children of its nested recyclerview.
看起来父 recyclerview 正在将点击事件传递给其嵌套 recyclerview 的子级。 Please suggest how can I achieve this.
请建议我怎样才能做到这一点。 Thanks.
谢谢。
You will have to manually bubble up an event, the child in the child recycler view gets the onClickListener, it attaches some information, passes it up to its parent (child recyclerview), and then child recyclerview can attach itself and pass it to the main parent.您将不得不手动冒泡一个事件,子回收器视图中的子级获取 onClickListener,它附加一些信息,将其传递给其父级(子级回收器视图),然后子级回收器视图可以附加自身并将其传递给主父母。
The main Parent has to pass in a callback, and the same needs to be done by the child recyclerView.主Parent必须传入一个回调,子recyclerView也需要做同样的事情。
The code may look something like this:代码可能如下所示:
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.