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

514 views Asked by At

I have a RecyclerView with the row layouts having some clickable TextView elements that have the background set to ?attr/selectableItemBackground.

The issue I'm facing is that the background ripple selector gets triggered when we are scrolling the RecyclerView, irrespective of the location of touch-n-scroll action. Here's a video that demonstrates this issue (-It was taken on emulator, hence a bit janky): https://i.stack.imgur.com/lsiH6.jpg -See the ripple getting triggered for X Comments & X Views

Things I've tried:

  1. Tried using a custom selector instead of using selectableItemBackground but the behaviour remains the same.
  2. Currently using the latest RecyclerView version (1.2.0), also tried using old versions, but the issue persists.

My questions:

  1. Why does this happen only for certain views? There are more clickable views in the layout (example: the Like and Comment button seen in the above link), but it happens only for the 2 views demonstrated.
  2. How do I prevent this from happening? Any leads on what can I try to resolve this?

Thanks in advance!

EDIT: Adding layout-XML just for the part that contains the views which have the ripple triggered. This gets included in other layouts which are used for the row (I've also tried to have this code directly in the layouts as well without using 'include' but that doesn't change anything in the behavior):

    <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>
1

There are 1 answers

1
Kiran Parmar On BEST ANSWER

Update: I was able to resolve this - just leaving the answer here for anyone who encounters a similar issue.

The problem was that the on-click listeners for the 2 views which were having this issue, were commented out from the code as the specific actions for those were deferred to a future release. In the current version, we just wanted the action to behave as if the parent is clicked. So, we simply commented their listeners so that the parent handles the click-event.

To give an effect of the views still being clickable, we had still left in the 'selectableItemBackground' attribute, which was getting triggered whenever the recycler view-item got a touch event. Just adding a click-listener & calling parent.performClick() fixed the issue.