androidandroid-fragmentstextviewmultipleselectionripple-effect

Ripple effect of TextView gets stuck on long click, when selection is used


I recently refactored my App to use the jetpack navigation. On my MainActivity, I had a ViewPager containing 3 fragments with 1 recyclerview each. I therefore moved the ViewPager from the Activity to a new Fragment. Everything works when it comes to the functionality itself, but a new issue arose which really bugs me: When I select an item on the recyclerview with a long press, the ripple effect gets stuck and stays there, as if I'm still pressing the item, which I don't. Here is a picture of a selected item. As it can be seen, the ripple effect stays active on the TextView.

Here is a picture of a selected item

When I unselect the Item, the ripple effect also stays active: enter image description here

The selection itself is handled with the android SelectionTracker. The TextView has the background set to background="?android:attr/selectableItemBackground". The whole ViewItem has the background set to android:background="@drawable/bg_multi_selection, whereas the bg_multi_selection file looks as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:drawable="@color/secondaryLightColor" android:state_activated="true" />
         <item android:drawable="@android:color/transparent" />
   </selector>

What could be the issue of that? This is especially weird, as the problem did not exist, when the ViewPager with its fragments was contained inside the Activity, and not a Fragment. Any suggestions on how to fix this?


Solution

  • I found a solution which works for me. So for anyone interested, I did the following:

    In the recyclerviewadapter in the onBindViewHolder() method I check if the element is selected and additionally set the background color and background resource of the TextView programmatically.

    if (elementIsSelected) {
                    viewHolder.viewBinding.itemTextView.setBackgroundColor(context.resources.getColor(R.color.transparent, context.theme))
                } else {
                    val outValue = TypedValue()
                    context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)
                    viewHolder.viewBinding.itemTextView.setBackgroundResource(outValue.resourceId)
                }
    

    whereas I definied the color to be transparent, i.e. #00000000

    It's odd that this problem occurs only on some devices, so I'm not sure if the "real" problem lies somewhere else, but at least that's how I fixed it. Hope that might be helpful for someone else.