[英]ListView items background issue
我在使用ListView项目时遇到问题。 我正在使用listSelector。 按下时我需要项目更改背景。 但是当我按它时,所有项目都会改变背景。 它发生在Android 2.3.6上。 在4.0.3及更高版本中,它运行良好。
ListSelector
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/defaultSelector">
<item android:drawable="@color/defaultDashboardItemBackground" android:state_enabled="false" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@color/defaultDashboardItemHover" android:state_pressed="true"/>
<item android:drawable="@color/defaultDashboardItemBackground" android:state_focused="true" android:state_pressed="false"/>
一些代码
ListView list = (ListView) act.findViewById(R.id.menu_listview);
list.setBackgroundColor(act.getResources().getColor(getThemeBackground(theme)));
list.setSelector(act.getResources().getDrawable( getThemeSelector(theme)));
list.addHeaderView(createHeader("HEADER"));
list.setAdapter(adap);
这对我有用
<ListView
android:id="@+id/playList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@android:color/holo_orange_light"
android:cacheColorHint="#00000000"
>
</ListView>
在onItemClickListener事件中添加
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
parent.setSelection(position);
希望这会帮助你。
将此属性设置为您的listview android:cacheColorHint="@null"
这可能有助于修复它。
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:listSelector="#00000000"
android:fadingEdge="none"/>
并且不要在ListView中定义背景色。 在listitem.xml根目录布局中定义背景色(选择器)或图像(选择器)。
希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.