Adding RippleDrawable and StateListDrawable effects to RecyclerView

I am working on an application that uses a two-pane layout on larger devices, similar to what is described here . Brief description of the layout; one panel contains a list of options, and the other displays detailed information about the option selected in another panel.

Now, when the parameter is selected, the ripple effect appears, which is observed when other elements are selected (buttons, flags, etc.), but after the animation is completed, the element returns to its previous color. I want to keep the selection from the ripples after the animation is completed, and it is difficult for me to understand how to do it.

This is what my wave background looks like. The focus selector does nothing - could not figure out how to actually give the elements the focuses. I also tried using the selected selector, but this happens immediately, overriding the ripple.

 <?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorAccent"> <item android:id="@android:id/mask" android:drawable="@android:color/black" /> <item> <selector> <item android:state_focused="true" android:drawable="@color/accent" /> </selector> </item> </ripple> 

To repeat the repetition, my question is: is it possible to have a ripple effect, followed by a highlighted selection, and if so, how?

+5
source share
2 answers

This saved the ripple animation for me when choosing.

When using your mountain ash:

 <?xml version="1.0" encoding="utf-8"?> <item> <selector> <item android:state_selected="true" android:drawable="@color/selected_color" /> <item android:drawable="@color/normal_color" /> </selector> </item> </ripple> 

On your adapter, call setHasStableIds(true) and implement getItemId to return unique values ​​representing your strings.

When an item is selected, do:

 ViewHolder oldViewHolder = (ViewHolder) recyclerView.findViewHolderForItemId(oldItemId); oldViewHolder.itemView.setSelected(false); ViewHolder newViewHolder = (ViewHolder) recyclerView.findViewHolderForItemId(newItemId); newViewHolder.itemView.setSelected(true); 

Also in your adapter:

 @Override public void onBindViewHolder(ViewHolder viewHolder, int position) { ... viewHolder.itemView.setSelected(isSelected); ... } 

On the one hand, a focused selector is used to highlight navigation on devices using a d-pad, such as a TV and some older phones.

+1
source

I also struggled a lot with this. I ended up using <layer-list/>

You can see my solution in an answer to a question very similar to yours: fooobar.com/questions/825771 / ...

0
source

Source: https://habr.com/ru/post/1208451/


All Articles