简体   繁体   中英

Change background on selected item in listView

I have problem when Im trying to change background on selected item in listview . When I select item A, it's background is changed. If I select item B, it changed too, but item A doesn't back to default background.

This drawable for background selected_item.xml :

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

This is the XML item_kategori.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lay_nama_kategori"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical"
    android:background="@drawable/selected_item">

    <TextView
        android:id="@+id/txtView_kategori"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textAllCaps="false"
        android:textColor="@color/colorMegna"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

</LinearLayout>

This is the setOnClickListener() in Adapter:

inner class CategoryViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
    fun bind(kategori: Category){
            itemView.txtView_kategori.text = kategori.category
            itemView.setOnClickListener {

                if(kategoriList[adapterPosition] == kategori){
                    itemView.isSelected = true
                    itemView.txtView_kategori.setTextColor(Color.WHITE)
                }
            }
        }
    }

You need to keep track of which ListItem is selected. In your Adapter class, create a variable that stores the current ListItem , which is selected

  1. Create your variable lastClicked
  2. In the constructor of the adapter, set that variable to -1, so that it doesn't point to anything
  3. Whenever you click ListItem B, change the lastClicked ListItem (in this case it Item A) background to the original, then save the new ListItem (item B) position to selectedItem and change the background to show the newly clicked ListItem

Because you are not setting the item A to its original color. When you click item B you need to d0 2 things. First, set the item B to the color you want and secondly set the item A back to its original color. Try this:

if(kategoriList[adapterPosition] == kategori){
      itemView.isSelected = true
      itemView.txtView_kategori.setTextColor(Color.WHITE)
  } else {
      itemView.isSelected = false
      itemView.txtView_kategori.setTextColor(//any color you want)  
  }
notifydatasetChanged();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM