首页 > 解决方案 > 防止 RecyclerView 在不创建自定义 ViewGroup 的情况下吞下触摸事件

问题描述

我目前正在开发一个看起来像这样 的 UI 蓝色部分是 ConstraintLayout 而紫色部分是其中的 RecyclerView(它是 RecyclerView,因为它的内容是基于服务响应的动态内容)。
在此处输入图像描述

onClick在 ConstraintLayout 上设置处理程序,将用户带到另一个页面。问题是 RecyclerView 正在消耗点击而不是将其转发给其父级。因此onClick处理程序适用于蓝色区域,但不适用于紫色区域。

我尝试在 RecyclerView 中进行设置android:clickable="false"android:focusable="false"但它仍然不会将点击传播到其父级。

我遇到的一种解决方案是从 ConstraintLayout 扩展并覆盖onInterceptTouchEvent()以返回 true。但是我在我的项目中有一个严格的要求,不能创建自定义小部件,所以我不能使用这个解决方案。

有没有办法告诉 RecyclerView 停止消费触摸事件?

活动布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_margin="16dp"
        android:background="#42d7f4"
        android:onClick="navigate"
        android:padding="16dp">

        <TextView
            android:id="@+id/headerText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="FRUITS"
            android:textSize="36sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/itemsList"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="#9f41f2"
            android:clickable="false"
            android:focusable="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

物品布局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:clickable="false"
    android:focusable="false" />

活动.kt:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val rcView = findViewById<RecyclerView>(R.id.itemsList)
        rcView.layoutManager = LinearLayoutManager(this)
        val items = listOf("Apple", "Banana", "Oranges", "Avocado")
        rcView.adapter = ItemAdapter(items)
    }

    fun navigate(view: View) {
        Toast.makeText(this, "Navigating to details page", Toast.LENGTH_SHORT)
            .show()
    }
}

class ItemAdapter(private val data: List<String>) : RecyclerView.Adapter<ItemViewHolder>() {
    override fun getItemCount(): Int = data.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
        return ItemViewHolder(view)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.bind(data[position])
    }
}

class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    private val itemTv: TextView = view.findViewById(R.id.itemText)

    fun bind(item: String) {
        itemTv.text = item
    }
}

标签: androidandroid-recyclerviewandroidx

解决方案


如果在设置适配器后冻结布局,它不再吞下点击:

recyclerView.isLayoutFrozen = true // kotlin
recyclerView.setLayoutFrozen(true); // java

请记住,如果您需要更改适配器中的数据,则必须在调用 notifyDataSetChanged 之前解冻布局,然后重新冻结布局。我不喜欢这个解决方案,但它是唯一对我有用的解决方案。


推荐阅读