更改ListView中项目的颜色-Android
我有一个ListView,我想更改列表中所选项目的颜色。我可以更改下面所选项目的颜色,但如何将所有其他项目的颜色恢复(或更改)为其原始颜色更改ListView中项目的颜色-Android,android,listview,Android,Listview,我有一个ListView,我想更改列表中所选项目的颜色。我可以更改下面所选项目的颜色,但如何将所有其他项目的颜色恢复(或更改)为其原始颜色 @Override public void onListItemClick(ListView parent, View v, int position, long id) v.setBackgroundColor(Color.CYAN); } 我尝试更改父项的颜色,但没有更改项目的颜色: @Override pu
@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
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=""/>
正确的方法是定义一个自定义的
选择器
,设置颜色(可绘制),并根据需要在每个状态下设置颜色。有关详细信息,请参阅此链接:
在这里:
--关于这一点,还有很多其他的帖子
如果要保留当前设计,可以尝试以下方法:
View lastTouchedView;
@Override
public void onListItemClick(ListView parent, View v, int position, long id)
lastTouchedView.setBackgroundColor(Color.WHITE);
v.setBackgroundColor(Color.CYAN);
lastTouchedView = v;
}
使用选择器
到自定义布局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>
<?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>
drawable文件夹中的normal.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>
<?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>
//矩形
您只需为此使用CustomAdapter,并在其中声明一个全局int变量,该变量包含适配器的选定位置索引和内部getView方法,只需将选定位置与getView方法的位置值进行匹配,并相应地更改行视图背景。您可以尝试以下操作:从包含视图的父级检索所有项
然后设置所有项目的背景色
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);
}
public void onListItemClick(ListView父视图、视图v、整数位置、长id){
//将所有项目的背景设置为白色
对于(int i=0;i您有自定义列表视图吗?是的,我刚刚添加了xxml文件,我有高亮度重复。大约339个项目,看起来非常12个项目突出显示。有什么线索吗?为了澄清,您是说您有一个包含339个项目的列表,并且每个第12个项目都保持突出显示?如果是这样,请告诉我您的屏幕上至少有多少项目适合您任何时候都可以。这很奇怪。如果第一个突出显示,我会滚动屏幕,直到第12个突出显示。当我滚动回第一个时,它偶尔会被取消选择,第二个或第三个突出显示。12立即显示在屏幕上。如果第一个突出显示,则第14个突出显示。您是否使用带有覆盖的自定义适配器dengetView(…)
method?是的,有。您在我提供的链接中设置了与@raghundandan answer相同的xml文件。然后,在onListItemClick(…)
中调用“parent.setSelected(position)”或类似的东西。android:background=“@dravable/bkg.xml”
是否使用ListView布局?@ono我看你已经接受了答案。问题似乎已经解决了。@ono你需要有android:background=“@drawable/bkg.xml”
fro您的自定义布局,每行都要充气。对于问题中的最后一个xml。您不能接受两个答案。如果另一个答案有帮助,请将其标记为已接受。