How to change color of ListView items on focus and on click

前端 未结 7 1743
天命终不由人
天命终不由人 2020-11-27 03:44

i have a list View in my app (this is the xml layout):

   


        
相关标签:
7条回答
  • 2020-11-27 04:18

    Here's a good article on how to use selectors with lists.

    Instead of setting it to be the android:background of the ListView, I believe you want to set android:listSelector as shown below:

    <ListView android:id="@+id/list" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_gravity="center"
      android:divider="@null" 
      android:dividerHeight="0dip"
      android:listSelector="@drawable/list_selector" />
    
    0 讨论(0)
  • 2020-11-27 04:21

    Very old but I have just struggled with this, this is how I solved it in pure xml. In res/values/colors.xml I added three colours (the colour_...);

    <resources>
    
        <color name="black_overlay">#66000000</color>
    
        <color name="colour_highlight_grey">#ff666666</color>
        <color name="colour_dark_blue">#ff000066</color>
        <color name="colour_dark_green">#ff006600</color>
    
    </resources>
    

    In the res/drawable folder I created listview_colours.xml which contained;

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/colour_highlight_grey" android:state_pressed="true"/>
        <item android:drawable="@color/colour_dark_blue" android:state_selected="true"/>
        <item android:drawable="@color/colour_dark_green" android:state_activated="true"/>
        <item android:drawable="@color/colour_dark_blue" android:state_focused="true"/>
    </selector>
    

    In the main_activity.xml find the List View and add the drawable to listSelector;

    <ListView
        android:id="@+id/menuList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@drawable/listview_colours"
        android:background="#ff222222"/>
    </LinearLayout>
    

    Play with the state_... items in the listview_colours.xml to get the effect you want.

    There is also a method where you can set the style of the List View but I never managed to get it to work

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

    The child views in your list row should be considered selected whenever the parent row is selected, so you should be able to just set a normal state drawable/color-list on the views you want to change, no messy Java code necessary. See this SO post.

    Specifically, you'd set the textColor of your textViews to an XML resource like this one:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
        <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
        <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
        <item android:drawable="@color/black" /> <!-- default -->
    </selector> 
    
    0 讨论(0)
  • 2020-11-27 04:26

    In your main.xml include the following in your ListView:

    android:drawSelectorOnTop="false"
    
    android:listSelector="@android:color/darker_gray"
    
    0 讨论(0)
  • 2020-11-27 04:28

    Declare list item components as final outside your setOnClickListener or whatever you want to apply on your list item like this:

    final View yourView;
    final TextView yourTextView;
    

    And in overriding onClick or whatever method you use, just set colors as needed like this:

    yourView.setBackgroundColor(Color.WHITE/*or whatever RGB suites good contrast*/);
    yourTextView.setTextColor(Color.BLACK/*or whatever RGB suites good contrast*/);
    

    Or without the final declaration, if let's say you implement an onClick() for a custom adapter to populate a list, this is what I used in getView() for my setOnClickListener/onClick():

    //reset color for all list items in case any item was previously selected
    for(int i = 0; i < parent.getChildCount(); i++)
    {
      parent.getChildAt(i).setBackgroundColor(Color.BLACK);
      TextView text=(TextView) parent.getChildAt(i).findViewById(R.id.item);
      text.setTextColor(Color.rgb(0,178,178));
    }
    //highlight currently selected item
     parent.getChildAt(position).setBackgroundColor(Color.rgb(0,178,178));
     TextView text=(TextView) parent.getChildAt(position).findViewById(R.id.item);
     text.setTextColor(Color.rgb(0,178,178));
    
    0 讨论(0)
  • 2020-11-27 04:37
    listview.setOnItemLongClickListener(new OnItemLongClickListener() {
    
            @Override
            public boolean onItemLongClick(final AdapterView<?> parent, View view,
                    final int position, long id) {
                // TODO Auto-generated method stub
    
                 parent.getChildAt(position).setBackgroundColor(getResources().getColor(R.color.listlongclick_selection));
    
                return false;
            }
        });
    
    0 讨论(0)
提交回复
热议问题