简体   繁体   English

在嵌套的 recyclerview 情况下,没有在父 recyclerview 的项目上获得点击事件。 如何获得两个recyclerviews?

[英]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.

相关问题 如何处理嵌套recyclerview中recyclerview子项中每个项目的点击事件 - how to handle click event for each item in recyclerview child in nested recyclerview 如何获取 RecyclerView 项目的父视图? - How to get RecyclerView item's parent view? 如何处理Recyclerview列表项单击事件 - How to handle Recyclerview list item click event 父级RecyclerView单击事件的子级RecyclerView项 - Items of Child RecyclerView of parent RecyclerView click event 如何将点击事件传递给RecyclerView的项目? - How to pass a click event to RecyclerView's items? 从嵌套的 RecyclerView 到父 Fragment 获取 onSwipe 事件 - Getting onSwipe event from a nested RecyclerView to the parent Fragment Recyclerview(在Recyclerview上获取项目) - Recyclerview(Getting item on Recyclerview) 嵌套的 RecyclerView。 如何防止父 RecyclerView 在子 RecyclerView 滚动时滚动? - Nested RecyclerView. How to prevent parent RecyclerView from getting scrolled while child RecyclerView is scrolling? 如何获取RecyclerView的特定项目视图? - How to get particular item's view of RecyclerView? 单击子recyclerview后如何在嵌套recyclerview中获取父recyclerview值 - How to get parent recyclerview value in nested recyclerview after clicking child recyclerview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM