我有一个RecyclerView
带有一些可点击TextView
元素的行布局,这些元素的背景设置为?attr/selectableItemBackground
.
我面临的问题是,当我们滚动 RecyclerView 时,背景波纹选择器会被触发,而与 touch-n-scroll 操作的位置无关。这是一个演示此问题的视频(-它是在模拟器上拍摄的,因此有点笨拙): https ://imgur.com/9MInNgv -查看为X Comments
&触发的波纹X Views
我尝试过的事情:
- 尝试使用自定义选择器而不是使用
selectableItemBackground
,但行为保持不变。 - 目前使用最新的RecyclerView版本(1.2.0),也尝试使用旧版本,但问题依旧。
我的问题:
- 为什么这只发生在某些视图上?布局中有更多可点击的视图(例如:上面链接中的
Like
andComment
按钮),但它只发生在演示的 2 个视图中。 - 我该如何防止这种情况发生?关于我可以尝试解决这个问题的任何线索?
提前致谢!
编辑:仅为包含触发波纹的视图的部分添加 layout-XML。这包含在用于该行的其他布局中(我也尝试在不使用“包含”的情况下直接在布局中使用此代码,但这不会改变行为中的任何内容):
<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>