我想和Kotlin一起使用回收器查看点击事件. 这是我的代码,但它们不起作用.

<AccountInfoAdapter.kt>

enter image description here

enter image description here

<MainActivity.kt>

enter image description here

我用谷歌上的其他代码try 了大多数方法,但都不起作用. 点击事件本身根本不起作用. 我不知道问题出在哪里.

你应该知道的一件事是,我使用Firebase FiRestore将物品连接到Receierview. 我从FiRestore收到数据,并将其逐一放入回收视图中. 这就是问题所在吗?

我想在安卓回收站上使用点击事件.

推荐答案

与Listview不同,RecillerView没有附带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) {

        }

    })

Android相关问答推荐

如何制作带有图标和文本的Fab

未解析的引用:视图模型

升级到 Jetpack Compose 物料 list 2023.08.00 需要我将 targetSdk 更改为 34

在java android studio项目上安装mapbox

在 kotlin 协同 routine 中,如何将数据范围限定为请求路径(以 MDC 为例)?

Android Jetpack Compose全宽度抽屉优化

如何仅使用您的 Android 应用程序定位平板电脑?

在一个函数中组合相同的流 struct

在Android RoomDB中使用Kotlin Flow和删除数据时如何解决错误?

如何用jetpack compose实现垂直李克特量表

无法找到方法 ''java.io.File > org.jetbrains.kotlin.gradle.tasks.KotlinCompile.getDestinationDir()

如何正确地将图像上传到 Jetpack Compose 中的 LazyList 中的项目?

如何使用 Jetpack Compose 制作两个圆圈

如何在 TextButton 中分隔文本和图标

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

Android Studio Emulator Internet 连接问题仅是第一次

在 jetpack compose 中使用 viewmodel 的最佳实践

Android WebView 没有在第一次页面完成时从本地存储读取数据?

为什么在try 实例化 Mediaplayer 时会出现 NullPointerException?安卓Kotlin

等到上一个事件完成 Rx