Background Selector in RecyclerView Item

匿名 (未验证) 提交于 2019-12-03 01:17:01

问题:

I'm using the RecyclerView like below:

and my list item:

see in detail this part android:background="@drawable/selector_medium_high" it's a normal selector:

but when I run this code, i have no changes in background color when I touch the row....

回答1:

Set clickable, focusable, focusableInTouchMode to true in all elements of RecyclerView "list".



回答2:

Add :

android:background="?android:attr/selectableItemBackground" 

in item.xml



回答3:

Adding android:background="?android:attr/selectableItemBackground" to my_list_item.xml's root layout seems to work for me (assuming you want the default selection colour).

Also make sure the root layout's android:layout_width is match_parent rather than wrap_content to ensure that the whole row is selectable.



回答4:

If nothing of this works for you, like it didn't for me, use this code:

android:foreground="?android:attr/selectableItemBackground" 

The trick is in android:foreground attribute...



回答5:

Unfortunately using focusable views to simulate item selection is not a good solution because:

  • Focus is lost when notifyDataSetChanged is called
  • Focus is problematic when child views are focusable

I wrote a base adapter class to automatically handle item selection with a RecyclerView. Just derive your adapter from it and use drawable state lists with state_selected, like you would do with a list view.

I have a Blog Post Here about it, but here is the code:

public abstract class TrackSelectionAdapter extends RecyclerView.Adapter {     // Start with first item selected     private int focusedItem = 0;      @Override     public void onAttachedToRecyclerView(final RecyclerView recyclerView) {         super.onAttachedToRecyclerView(recyclerView);          // Handle key up and key down and attempt to move selection         recyclerView.setOnKeyListener(new View.OnKeyListener() {             @Override             public boolean onKey(View v, int keyCode, KeyEvent event) {                 RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();                  // Return false if scrolled to the bounds and allow focus to move off the list                 if (event.getAction() == KeyEvent.ACTION_DOWN) {                     if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {                         return tryMoveSelection(lm, 1);                     } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {                         return tryMoveSelection(lm, -1);                     }                 }                  return false;             }         });     }      private boolean tryMoveSelection(RecyclerView.LayoutManager lm, int direction) {         int tryFocusItem = focusedItem + direction;          // If still within valid bounds, move the selection, notify to redraw, and scroll         if (tryFocusItem >= 0 && tryFocusItem 


回答6:

    viewHolder.mRlPrince.setOnTouchListener(new View.OnTouchListener() {         @Override public boolean onTouch(View v, MotionEvent event) {              if (event.getAction()==MotionEvent.ACTION_DOWN){                 viewHolder.mRlPrince.setBackgroundColor(Color.parseColor("#f8f8f8"));             }if (event.getAction()==MotionEvent.ACTION_UP){                 viewHolder.mRlPrince.setBackgroundColor(Color.WHITE);             }              return false;         }     }); 


回答7:

Add this below attribute in your Item "my_list_item.xml"
android:descendantFocusability="blocksDescendants" This works for me !



回答8:

You need to set android:clickable="true" in the element xml, and if you have more selectors in some view inside your view, you need to set android:duplicateParentState="true" there too. Thats works on pre honeycomb apis.



回答9:

As many others answered the only way is to combine selectors and new Classes to keep track of the selection, but better to delegate this calculation to the Adapter. The library FlexibleAdapter keeps track of the selections for you, configuration change is also compatible.

Backgrounds color with ripple can now be done without XML part, but in the code to manage dynamic data.

Finally, you can use lot of features with the same library, selection is a tiny basic feature you can have.

Please have a look at the description, Wiki pages and full working example: https://github.com/davideas/FlexibleAdapter



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!