How can I always highlight curent item in the listview?

喜你入骨 提交于 2019-12-02 23:22:14

问题


Now I am using:

android:listSelector="@drawable/bg_list_item"
android:drawSelectorOnTop="true"

where bg_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>

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

<item android:state_focused="false"
      android:state_pressed="false"
      android:state_selected="false">
    <shape android:shape="rectangle">
        <gradient android:startColor="#ff0000"/>
    </shape>
</item>

BUT first item is not working and selected item is not highlighting sometimes on scroll (when I am near top/bottom of my listview) and never on listView.setSelection(index).

Does I must to highlight current item programmaticaly?


回答1:


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>



回答2:


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.




回答3:


"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.




回答4:


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.



来源:https://stackoverflow.com/questions/10583163/how-can-i-always-highlight-curent-item-in-the-listview

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