简体   繁体   中英

highlighting the selected item in the listview in android

I am having 1 list view contactslist . I wrote the code for highlighting the selected item in the ListView . It is working. When I click on 1 item it is highlighting that item but the problem is if I click on other item it is highlighting that too. I want to highlight the selected item only. The previous selection will have to gone when I click on another item.

arg1.setBackgroundResource(R.drawable.highlighter);

This is the code in the click listener using to highlight the selected item. plz help me.

Update
I'm setting the background of the rows in the adapter:

public int[] colors = new int[]{0xFFedf5ff, 0xFFFFFFFF}; 
public int colorPos; 

[...]
colorPos = position % colors.length; 
row.setBackgroundColor(colors[colorPos]);

ListViews by default don't have a choiceMode set (it's set to none ), so the current selection is not indicated visually.

To change this, you just need to set the choiceMode attribute of your ListView to singleChoice .
If you'd like custom background for the selected items in your list, you should also set the listSelector attribute. There you can specify not only colors, but drawables (images, layer-/state-drawables).

<ListView android:id="@+id/my_list"
        android:choiceMode="singleChoice" 
        android:listSelector="@android:color/darker_gray" />

If you don't use a ListView directly, but a ListActivity , then these attributes need to be set from code, so you should extend your activity's onCreate method with these lines:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.darker_gray);

So if you were using a click listener to change the background of the selected row, remove that from your code, and use the proper method from above.

Reply to the update

If you set the background from your getView method, instead of using a static color, apply a state list drawable to the row background with duplicateParentState set to true. This way it will change its display based on the current state of the item: normal, focused, pressed, etc.

In the listview xml add the "singleChoice" mode

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    (...) >
</ListView>

In the list item layout add

android:background="?android:attr/activatedBackgroundIndicator

example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:background="?android:attr/activatedBackgroundIndicator">

     <!-- your item content-->

</LinearLayout> 

A better way is in your theme, @drawable/list_selector

list_selector.xml:

<!-- <item android:drawable="@color/android:transparent"  android:state_selected="true" /> -->
<item android:drawable="@color/list_bg" android:state_selected="true"/>
<item android:drawable="@color/list_bg" android:state_activated="true"/>
<item android:drawable="@color/transparent"/>

then set background for root of your list_row.xml android:background="?android:attr/activatedBackgroundIndicator"

Try this at onListItemClick

view.getFocusables(POSITION);
view.setSelected(true);

It highlights the selection.

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