How to highlight selected item in ListView?

后端 未结 6 1037
醉梦人生
醉梦人生 2020-12-08 23:36

I know that android doesn\'t highlight anything in TouchMode. But I am doing something similar to the gmail application in which you select stuff from the left side and show

相关标签:
6条回答
  • 2020-12-08 23:51

    Use listView.setChoiceMode(int choiceMode);

    Parameters

    choiceMode One of CHOICE_MODE_NONE, CHOICE_MODE_SINGLE, or CHOICE_MODE_MULTIPLE from class android.widget.AbsListView

    http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)

    You also need to add MultiChoiceModeListener, you can have CHOICE_MODE_SINGLE

    (android.widget.AbsListView.MultiChoiceModeListener)

    Refer to the sample below

    http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List16.html

    0 讨论(0)
  • 2020-12-08 23:51

    Because the items in the listviews mimics the ones at the top, they also mimics the background of them. You have to set every one of the background of your items in getView() function. In every item in getView() you have to set the background for both selected ones and the unselected ones.

    0 讨论(0)
  • 2020-12-09 00:06

    use this as list selector:

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

    and this list_bg:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/list_bg_color" />
    </shape>
    

    choose the preferred highlight color

    0 讨论(0)
  • 2020-12-09 00:09

    ////@drawable/list_selector

     <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:drawable="@drawable/list_item_bg_normal"
     android:state_pressed="false" android:state_selected="false" android:state_activated="false"/>
    
    <item android:drawable="@drawable/list_item_touch"
     android:state_pressed="true"/>
    
     <item android:drawable="@drawable/list_item_touch"
     android:state_pressed="false" android:state_selected="true"/>
    
     <item android:drawable="@drawable/list_item_bg_pressed"
     android:state_activated="true"/>
    
     </selector>
    

    ////////////////////and on ListView

     <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"
        android:divider="#060606"/>
    

    ///////////////////////listview Item

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/list_selector">
    
      <ImageView
      android:id="@+id/icon"
      android:layout_width="35dp"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginLeft="12dp"
      android:layout_marginRight="12dp"
      android:contentDescription="@string/desc_list_item_icon"
      android:src="@drawable/ic_home"
      android:layout_centerVertical="true" />
    
      </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-09 00:10

    The reason you are highlighting multiple items is probably because you are either: reusing the whole view, or setting the background of both of these views to the same Drawable instance. If you have the same drawable on the screen twice, all events that happen to the first will happen to all the others, because that logic is implemented in the instance of the Drawable itself.

    To solve this, either: do not re-use Views for multiple rows, or do not re-use Drawables for multiple rows (create a new one each time)

    I know this sounds resource intensive and it is, but unless you have a better solution figured out this is the easiest fix.

    0 讨论(0)
  • 2020-12-09 00:11

    this is for custom listactivity or ListFragment

    highlight selected item in ListView

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long arg3)
    {
    
        for(int a = 0; a < parent.getChildCount(); a++)
        {
            parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
        }
    
        view.setBackgroundColor(Color.GREEN);
    }
    
    0 讨论(0)
提交回复
热议问题