How to highlight selected item in RecyclerView

后端 未结 8 1308
猫巷女王i
猫巷女王i 2020-12-14 17:49

I have used RecyclerView for showing thumbnails in my Image Editing app.Each item of its comprises of a ImageView(thumbnail) and a textView.In my application I want to

相关标签:
8条回答
  • 2020-12-14 18:33

    So we have a class ViewHolder and a method OnBindViewHolder() in recycler view ok? now when you click an item in recycler view then the OnClickMethod is called for that view from the view holder class. Now create an integer variable int previousClickedItemPosition = -1 ;

    Now in the onClickListener method in viewHolderClass put this line, previousClickedItemPosition = getAdapterPosition(); From here we have the position of the item we clicked recently.

    Now in OnBindViewHolder() set the colour of the item Background like blue,

    holder.fileNameTextView.setTextColor(Color.BLUE)

    now In OnclickListener put these line

    holder.fileNameTextView.setTextColor(Color.YELLOW);
    
    if(previousClickedItemPosition != getAdapterPosition()) {
    notifyDataSetchanged(previousClickedItemPosition );
    }
    

    The tip is that when we call notifyDataSetchanged then onBindViewHolder is called.

    SO when you start recycler view all colour will be blue. When you click an item colour will be yellow. When you click next item then the previous item colour will be blue and for current item, the colour will be yellow.

    0 讨论(0)
  • 2020-12-14 18:33
    android:background="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusableInTouchMode="true"
    
    0 讨论(0)
  • 2020-12-14 18:37

    Just add this below line in bindView

    holder.itemView.setBackgroundColor(Color.parseColor("#000000"));
    

    will work for you.

    if you want to highlight selected item just do like below

    set it global

    int selectedPosition=-1;
    

    inside onBindViewHolder-

    public void onBindViewHolder(FiltersAdapter.ViewHolder holder, int position) {
    if(selectedPosition==position)
      holder.itemView.setBackgroundColor(Color.parseColor("#000000"));
    else
      holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));
    
    holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedPosition=position;
                    notifyDataSetChanged();
    
                }
            });
    }
    
    0 讨论(0)
  • 2020-12-14 18:37

    In Kotlin you can simply do what some folks have already mentioned for Java. You put this code in your adapter class.

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (position < numItems) {
            // Bind your view here
            holder.itemView.setOnClickListener {
                it.setBackgroundResource(R.color.lightBlue)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-14 18:47

    Use background selector and set that in android:background property in the layout xml for the recyclerview item

    background_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="false" android:state_selected="true">
            <shape>
                <solid android:color="@color/lightPrimaryColor" />
            </shape>
        </item>
    
        <item android:state_selected="false">
            <shape>
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
    </selector>
    

    recyclerview_item.xml (background_selector is set in android:background property)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_selector"
        android:orientation="vertical"
        android:paddingBottom="8dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="8dp">
    

    And then the place where you get the click event you can set it as selected with the View function

    view.setSelected(true)

    You would have to implement the logic for when you want to unselect/select the item by storing the position of selected items

    0 讨论(0)
  • 2020-12-14 18:47

    May be just use:

    android:background="?attr/selectableItemBackground"

    for root element at item xml?

    0 讨论(0)
提交回复
热议问题