Changing color of items in ListView - Android

五迷三道 提交于 2020-12-12 11:24:09

问题


I have a ListView and I would like to change the color of the selected item in the list. I can change the color of the selected item below, but how do I revert (or change) the color of all the other items back to their original color?

@Override
    public void onListItemClick(ListView parent, View v, int position, long id) 
        v.setBackgroundColor(Color.CYAN);
    }

I tried changing the color of the parent but it doesn't change the color of the items:

 @Override
    public void onListItemClick(ListView parent, View v, int position, long id) 
        parent.setBackgroundColor(Color.WHITE);
        v.setBackgroundColor(Color.CYAN);
    }

ListView:

        <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:drawSelectorOnTop="false" />

Each item is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contacts_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout android:id="@+id/linear"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text=""/>

    <TextView
        android:id="@+id/lastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="3sp"
        android:text=""/>

    <TextView
        android:id="@+id/firstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="3sp"
        android:text=""/>

    <TextView
        android:id="@+id/sipExt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="#59696E"
        android:gravity="right"
        android:layout_marginRight="9dp"
        android:textStyle="italic"
        android:text=""/>

</LinearLayout>

    <TextView
    android:id="@+id/alias"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="#a69d86"
    android:textStyle="italic"
    android:layout_below="@id/linear"
    android:text=""/>


回答1:


There proper way to do this is to define a custom Selector and set the color (Drawable) and set the colors as you want them to be in each state. See this link for details:

Can't add custom selector to ListView

and here:

Android ListView selected item stay highlighted

-- there are plenty of other posts on SO regarding this as well.

If you want to keep your current design you can try something like this:

 View lastTouchedView;

 @Override
 public void onListItemClick(ListView parent, View v, int position, long id) 
    lastTouchedView.setBackgroundColor(Color.WHITE);
    v.setBackgroundColor(Color.CYAN);
    lastTouchedView = v;
 }



回答2:


Use a selector.

To the required view in custom layout xml

 android:background="@drawable/bkg.xml"

bkg.xml in drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/pressed" />
    <item  android:state_focused="false" 
        android:drawable="@drawable/normal" />
</selector>

Customize the below according to your requirements

pressed.xml in drawable folder

 <?xml version="1.0" encoding="UTF-8"?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" >
 <solid android:color="#ff33ffff" />
 <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
 <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
 </shape>

normal.xml in drawable folder

  <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">  // rectangle shape
  <solid android:color="@android:color/transparent"/>   
  <stroke android:width="3dp"
        android:color="#0FECFF" /> 
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp" // rounded corner
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>  



回答3:


You may try the following to retrieve all items from the parent containing the views and then set the background color of all the items

public void onListItemClick(ListView parent, View v, int position, long id){

            //Set background of all items to white
            for (int i=0;i<parent.getChildCount();i++){
                parent.getChildAt(i).setBackgroundColor(Color.WHITE);
            }

            v.setBackgroundColor(Color.CYAN);
}



回答4:


You just need to use CustomAdapter for this and inside it you need to declare a global int variable which contains the selected Position index and inside getView method of the adapter just check match selected Position with position value of getView method and change the row view background accordingly.



来源:https://stackoverflow.com/questions/17838036/changing-color-of-items-in-listview-android

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