How can I always highlight curent item in the listview?

萝らか妹 提交于 2019-12-02 10:27:00

define a custom adapter and in its getView() method:

private int selectedItem;

// is used outside the adapter for example OnItemClickListener
public void setSelectedItem(int position) {
    selectedItem = position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   //...
   // set selected item
    LinearLayout activeItem = (LinearLayout) rowView;
    if (position == selectedItem)
    {
       activeItem.setBackgroundResource(R.drawable.background_dark_blue);
    }
    else
    {
       activeItem.setBackgroundResource(R.drawable.border02);
    }
   //...
}

background_dark_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
<shape android:shape="rectangle" >

            <solid android:color="@color/lightBlue6" />
            <!-- border width and color -->

            <stroke
                android:width="2dp"
                android:color="#FF0404B4" />

            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp" />
        </shape></item>

</layer-list>

border02.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" android:layout_width="wrap_content">
            <stroke android:width="0.5dp" android:color="#FF000000" />
            <solid android:color="#FFFFFFFF" />
            <padding android:left="0dp" android:top="0dp" android:right="0dp"
                android:bottom="0dp" />
            <corners android:radius="0dp" />
        </shape>
    </item>


</layer-list>

This is because there is not a selected item in touchmode. See here for an explanation.

You can do it with a selector, but it requires three things...

1) You must make the list row chooseable

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

2) Add to your selector file a condition for the activated state:

    <item android:state_activated="true" >
        <shape android:shape="rectangle"> 
        <gradient android:startColor="#0000ff"/>

3) You need create a custom adapter to track the state of each position in your list so you can maintain the proper state when scrolling.

EDIT

breceivemail's answer is more suited to your needs. Mine, while it would work, would work better for a multiple checked item type list (by changing it to CHOICE_MODE_MULTIPLE), and is more work for what you need than is necessary.

"android:listSelector" property is used to specify item background when it is clicked. To highlight the first item or the one selected after click you will need to do it programmatically.

rekaszeru

ListViews by default don't have a chioceMode set (it's set to none), so the current selection is not indicated visually. Playing around with the choiceMode attribute will get you to the desired aspect.

You could take a look at this solution as well, maybe it will work for you too.

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