【问题标题】:Prevent RecyclerView from swallowing touch events without creating custom ViewGroup防止 RecyclerView 在不创建自定义 ViewGroup 的情况下吞下触摸事件
【发布时间】:2019-07-22 23:10:39
【问题描述】:

我目前正在开发一个看起来像这样的用户界面

蓝色部分是 ConstraintLayout 而紫色部分是其中的 RecyclerView(它是 RecyclerView,因为它的内容是基于服务响应动态的)。

我在 ConstraintLayout 上设置了onClick 处理程序,它将把用户带到另一个页面。问题是 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" />

Activity.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
    }
}

【问题讨论】:

  • 你不能让RecyclerView 本身可以点击吗?换句话说,为ConstraintLayoutRecyclerView 设置了一个点击处理程序。
  • 尝试在RecyclerView上设置,好像不行。
  • 您是否要滚动recyclerview? @Abhishek
  • @Abhishek 你想滚动 recyclerview 但不想点击?
  • 如果RecyclerView 不需要滚动,我会按照@TanveerMunir 的建议使用叠加层,或者您可以在活动中使用dispatchTouchEvent()

标签: android android-recyclerview androidx


【解决方案1】:

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

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

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

【讨论】:

  • 这里有同样的问题,这个答案对我有用我只是用“suppressLayout(true)”替换了“setLayoutFrozen(true)”,因为第一个已被弃用。谢谢!。
【解决方案2】:

在单个视图中完全阻止与任何内容交互的最简单方法可能是在其上放置一个透明视图,以拦截所有触摸事件。您可以设置单击该视图管理通过该视图的所有功能。

比如这样

<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: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" />
            <View
                android:id="@+id/clickView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:onClick="navigate"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:clickable="true"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>

现在您可以通过该视图执行功能,而不是通过ConstraintLayout。 而且,你看看这个answer

【讨论】:

    【解决方案3】:

    解决此问题的一种方法是使用单击侦听器设置RecyclerView 行,并禁用对RecyclerView 行的子级的单击和长按,如下所示:

    ConstraintLayout: `android:onClick="navigate"`
       For the item layout: `android:onClick="navigate"`
           TextView: android:clickable="false"
                     android:longClickable="false"
            (etc. for all children of the row)
    

    我认为缺少的是使TextView 不能长时间点击。

    【讨论】:

      【解决方案4】:

      您可以将 RecyclerView 焦点设置为 false。

      recyclerView.setFocusable(false);
      

      和/或设置其行view.setFocusable(false);

      编辑

      如果你想让ConstraintLayout 成为焦点

      layout.setFocusable(true);
      layout.setClickable(true);
      
      //
      // add click listener and event ....
      // 
      

      更多信息请参考谷歌提供的official documentation

      希望对你有帮助,干杯

      【讨论】:

      • 我尝试将可聚焦和可点击设置为 false(在 xml 和片段中)。 ConstraintLayout 仍然没有得到点击事件。
      • 那你给 ConstraintLayout 重点了吗?
      • ConstraintLayout 正在除 RecyclerView 之外的所有部分中获得焦点。我在帖子中添加了代码示例。
      猜你喜欢
      • 2014-01-27
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多