Android RecyclerView 触发背景 selector/ripple 滚动时的某些视图

Android RecyclerView triggering background selector/ripple for some views on scrolling

我有一个 RecyclerView,行布局有一些可点击的 TextView 元素,这些元素的背景设置为 ?attr/selectableItemBackground

我面临的问题是,当我们滚动 RecyclerView 时,背景波纹选择器会被触发,而与触摸滚动操作的位置无关。这是演示此问题的视频(-它是在模拟器上拍摄的,因此有点笨拙):https://imgur.com/9MInNgv-查看 X CommentsX Views

触发的纹波

我尝试过的事情:

  1. 尝试使用自定义选择器而不是 selectableItemBackground,但行为保持不变。
  2. 目前正在使用最新的RecyclerView版本(1.2.0),也尝试过使用旧版本,但问题依旧。

我的问题:

  1. 为什么只有某些视图会出现这种情况?布局中有更多可点击的视图(例如:上面 link 中看到的 LikeComment 按钮),但它只发生在演示的 2 个视图中。
  2. 如何防止这种情况发生?关于我可以尝试解决这个问题的任何线索?

提前致谢!

编辑:为包含触发涟漪的视图的部分添加布局-XML。这包含在用于该行的其他布局中(我也尝试在不使用 'include' 的情况下直接在布局中使用此代码,但这不会改变行为中的任何内容):

    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/id_separator">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/id_likes"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingStart="@dimen/_20sdp"
            android:gravity="start|center"
            android:background="?attr/selectableItemBackground" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/id_comments"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:includeFontPadding="false"
            android:singleLine="true"
            android:textAllCaps="false"
            android:textStyle="normal"
            android:textSize="@dimen/_12ssp"
            android:background="?attr/selectableItemBackground"
            tools:text="12 Comments" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/id_views"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:paddingEnd="@dimen/_20sdp"
            android:drawablePadding="@dimen/_3sdp"
            android:gravity="end|center"
            android:includeFontPadding="false"
            android:singleLine="true"
            android:textAllCaps="false"
            android:textStyle="normal"
            android:textSize="@dimen/_12ssp"
            android:background="?attr/selectableItemBackground"
            tools:text="12 Views"
            tools:visibility="visible" />

    </androidx.appcompat.widget.LinearLayoutCompat>

    <View
        android:id="@+id/id_separator"
        android:layout_width="0dp"
        android:layout_height="@dimen/_1sdp"
        android:background="#1F000000"
        android:layout_marginTop="@dimen/_30sdp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

更新:我能够解决这个问题 - 只需在此处为遇到类似问题的任何人留下答案。

问题是出现此问题的 2 个视图的点击侦听器已从代码中注释掉,因为这些视图的具体操作已推迟到未来版本。在当前版本中,我们只希望该操作的行为就像单击父项一样。所以,我们简单地评论了他们的监听器,以便父级处理点击事件。

为了使视图仍然可点击,我们仍然保留 'selectableItemBackground' 属性,只要回收器视图项收到触摸事件,该属性就会被触发。 只需添加点击侦听器并调用 parent.performClick() 即可解决问题。