Highlight ListView selected row

前端 未结 12 1460
情歌与酒
情歌与酒 2020-11-27 16:04

I have a list of albums (several hundred). When I touch the selected album I want to offer the user a choice of playing the whole album, or moving to its track ListView. No

相关标签:
12条回答
  • 2020-11-27 16:10

    I solved this by extending the ListView and overriding the getView() method. I kept an internal identifier for the "selected" state and changed the background of the item according to it, like this:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View currView = super.getView(position, convertView, parent);
    StateListItem currItem = getItem(position);
    if (currItem.isItemSelected) {
        currView.setBackgroundColor(Color.RED);
    } else {
        currView.setBackgroundColor(Color.BLACK);
    }
    return currView;
    }  
    

    I have an example program on my blog post: http://udinic.wordpress.com/2011/07/01/selectablelistview-make-selection-work/

    0 讨论(0)
  • 2020-11-27 16:18

    I had the same problem, I solved it by pasting the following code in the ListView code

    android:choiceMode="singleChoice"
    android:listSelector="#003366"
    

    The xml file looks as the following

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <ListView
        android:id="@+id/file_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:listSelector="#003366"/>
    
    <Button
        android:id="@+id/select"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="SELECT"
        android:layout_below="@id/file_list"/>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-27 16:19

    I know that it's two years since this question was last active, but just in case someone else needs it in the future. Here's my ListView xml code

    <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/results"
            android:choiceMode="singleChoice"
            android:listSelector="#ffdb700b"/>
    

    You want to change the android:listSelector value.

    0 讨论(0)
  • 2020-11-27 16:22

    In my case, when using simple_list_item_2, the only solution was to override the getView() method of the adapter, and just change the background color manually:

    listview = new SimpleAdapter(ctx, data,
                    android.R.layout.simple_list_item_2, res,
                    new String[] {"field1", "field2" },
                    new int[] {android.R.id.text1, android.R.id.text2 })
    
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
    
                    if (this_is_the_selected_item)
                    {
                        view.setBackgroundColor(0xff999999);
                    }
                    else
                    {
                        view.setBackgroundColor(Color.BLACK);
                    }
                    return view;
                }
            };
    

    Remember to call listview.invalidateViews() whenever you change the selected item.

    0 讨论(0)
  • 2020-11-27 16:23

    Following solution can be used for highlighting one ore many list items. Starting point: There is a ListFragment to show a List of MyEntity via an ArrayAdapter implementation. The color of an TextView as part of the item layout has to change to grey, if the item is selected. Multiple items should be selectable.

    1. The ListFragment must provide an OnItemSelectListener(YourEntity item) that will be called in onListItemClicked().

      public void onListItemClick(ListView l, View v, int position, long id) {
        MyEntity entity = (MyEntity) getListView().getItemAtPosition(position);
        if (entityListSelectListener != null) 
          itemSelectListener.onItemSelect(entity); }
      
    2. The ArrayAdapter owns a List for the key fields for the selected items and provides the manager methods addSelection(MyEntity) and clearAllSelections(). The overriden method getView() reads the key field list and, if the actual position is in there, changes the particular TextView color to grey, otherwise to transparent.

      public View getView(int position, View convertView, ViewGroup parent) {
        ...
        if (selectedEntityList.contains(entity.key)) //... highlight view field }
      
    3. In onCreate() of the owning activity the listeners has to be implemented to manage the ArrayAdapter: calling addSelection() and notifyDataSetChanged().

      entityListFragment.setEntityListSelectListener(
        new EntityListFragment.OnItemSelectedListener() {
          public void onItemSelect(MyEntity entity) {
          aa.addSelection(entity);
          aa.notifyDataSetChanged(); }});
      
    0 讨论(0)
  • 2020-11-27 16:24

    Here is my solution Listview:

    <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="1dp"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:choiceMode="singleChoice"
            android:paddingTop="8dip" >
        </ListView>
    

    Write a selector for list_row as

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

    Make sure you have item for state_activated=true

    Then add this selector as your list_row background

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/listitem_selector"
      android:orientation="vertical" >
    
    <TextView
        android:id="@+id/itemName"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    0 讨论(0)
提交回复
热议问题