Given: RecyclerView with plain TextView as markup for list item. API version 17 and higher.

Required: select an element, including the click animation at the beginning (standard Material animation), but after a long press, leave the element pressed.

Solution options: the only thing I came up with was to install OnLongClickListener and manually check the press. After a long press, simply change the background to the clicked.

Also, as an option, I wanted to set the background selector, which prescribes the color at the stages of pressing, but then there were two problems: I get the animation by setting android:background="?android:attr/selectableItemBackground , so it will disappear, and, if manually to expose the color, after a certain number of elements, is found the same, highlighted (until I understood why).


Question: How is it right to select elements in RecyclerView , with Material animation (if the Android version is higher than 5th) and without the appearance of bugs with the following elements?

    1 answer 1

    A bug with the following elements is a completely different story. The fact is that when scrolling and hiding a list item, they are not saved, and when you scroll back to the visible place, the item's markup is loaded, hence all sorts of bugs, why they happen so it’s not clear, and you don’t need to know it, fix it

    Already described on this topic a lot, but I will write again

    In order for the list to "not lose" information about your items (list items), you need to create, for example, an array and store information about items in it, and in onBindViewHolder to take it from it. In short.

    Suppose you just need to click on items, and they, in turn, will be placed in gray. You need

    1) Create an array (here and boolean will do)

     boolean[] selects = new boolean[???] 

    Where ??? - your number of items.

    2) When pressed, change the values ​​from the array to true .

     selects[position] = true; 

    3) In onBindViewHolder , depending on the value, set the color

     if(selects[position]) holder.itemView.setBackgroundColor(Color.LTGRAY); else holder.itemView.setBackgroundColor(Color.TRANSPARENT); 

    Due to the fact that the default selector on different androids is different, it may turn out that the logic of colors will not converge, therefore I advise you to make the effect gray on all androids. Create a drawable-v21 . She for Lollipop and above. In her file, for example

    selector.xml

     <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/gray"> <item android:id="@android:id/mask" android:drawable="@color/gray"/> </ripple> 

    And in the folder drawable file with the same name for androids below Lollipop . They have no such effect, they will cost a change of color.

     <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/gray" android:state_pressed="true"/> <item android:drawable="@android:color/transparent"/> </selector> 

    Well, in the root element markup item

     android:background="@drawable/selector" 

    Instead

     android:background="?android:attr/selectableItemBackground" 
    • Well .. Scrolling bug is the least of all troubles. There is no sense in going deeper into it, because, with the right approach, there are no problems. Marking the background with a separate color - that's what I thought (although you added my thought to working condition :)). And what is the illusion of depression? - user189127
    • I did not understand you simply, I thought you needed the item to make the effect that you clicked on it, read the question better, I realized that the answer is complete. The illusion of pressing is my misunderstanding)) - Flippy
    • Keep in mind that on different androids the default animation of pressing is different .. Starting with a candy, it is gray, ripple . On Jelly Bean is blue. So I advise you to create your own background for an item and describe the gray color in it - Flippy
    • And then the effect is blue and the color is gray. - Flippy
    • As I understand it, somewhere in the SDK resources there is a drawable for creating animation, state, etc. for each version of Android. But where ... - user189127