android listview alternate row color BUT with default cursor selection

淺唱寂寞╮ 提交于 2019-11-30 23:49:14

A workaround is to use 2 selectors. From your adapter, instead of setting 2 colors, you set 2 selectors.

if (position % 2 == 0) {
  view.setBackgroundResource(R.drawable.selector_1);
} else {
  view.setBackgroundResource(R.drawable.selector_2);
}

selector_1 is defined in selector_1.xml like this:

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

selector_2 is defined in selector_2.xml like this:

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

So that, you have a bi-color listview and a third color/shape/whatever-you-want for selected item.

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