更改ListView中的项目颜色 - Android

时间:2013-07-24 15:10:16

标签: android listview

我有一个ListView,我想更改列表中所选项目的颜色。我可以更改下面所选项目的颜色,但是如何将所有其他项目的颜色恢复(或更改)回原始颜色?

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

我尝试更改父级的颜色,但不会更改项目的颜色:

 @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" />

每个项目都是:

<?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=""/>

4 个答案:

答案 0 :(得分:3)

使用选择器。

到自定义布局xml中的所需视图

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

可绘制文件夹中的bkg.xml。

<?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>

根据您的要求自定义以下内容

drawable文件夹中的pressed.xml

 <?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>

drawable文件夹中的normal.xml

  <?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>  

答案 1 :(得分:2)

正确的方法是定义自定义Selector并设置颜色(Drawable)并根据需要设置颜色,使其处于每种状态。有关详细信息,请参阅此链接:

Can't add custom selector to ListView

在这里:

Android ListView selected item stay highlighted

- 关于此,还有很多关于SO的帖子。

如果您想保留当前的设计,可以尝试以下方法:

 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 :(得分:1)

您可以尝试以下操作从包含视图的父级中检索所有项目 然后设置所有项目的背景颜色

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);
}

答案 3 :(得分:0)

你只需要使用CustomAdapter,你需要在其中声明一个包含所选位置索引的全局int变量,并在适配器的getView方法中检查匹配所选位置和getView方法的位置值并更改行相应地查看背景。