android回收器视图点击事件无响应

问题描述 投票:0回答:1

我想在 kotlin 中使用回收器视图点击事件。 这是我的代码,但它们不起作用。

enter image description here

enter image description here

enter image description here

我已经使用Google上的其他代码尝试了大部分方法,但它们都不起作用。 点击事件本身根本不起作用。 我不知道问题是什么。

您应该知道的一件事是我使用 Firebase Firestore 将项目连接到 recyclerview。 我从Firestore收到数据,并将其一一放入回收视图中。 这是问题吗?

我想在android recyclerview上使用click事件。

android kotlin android-recyclerview onclick
1个回答
0
投票

RecyclerView 没有像 listview 那样自带 onItemClickListener。所以我们使用 addOnItemTouchListener

binding.recyclerView1.addOnItemTouchListener(object: RecyclerView.OnItemTouchListener {

        // This method is called when a touch event occurs on the RecyclerView ie click, scroll, long click etc, allowing the listener to intercept the event before it reaches the child views.

        // If you return true from this method, you indicate that the touch event has been consumed and should not be further processed by child views or other touch listeners.            

        override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
            // for getting click event, you can:
            if (e.action == MotionEvent.ACTION_UP) {
             // you firebase code
            }
            return false
        }

        // allows you to handle touch events directly on the RecyclerView level. You might use this method for more global touch event handling, such as scrolling or zooming.
        override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {

        }

        // called when a parent view requests that the RecyclerView should not intercept touch events.
        override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {

        }

    })
© www.soinside.com 2019 - 2024. All rights reserved.