android listview alternate row color BUT with default cursor selection

[亡魂溺海] 提交于 2019-12-19 04:11:13

问题


i have been all over the web, stackoverflow included and just can't seem to get a clear complete way to

I want to create a ListView that

1) has alternating colors (I am able to do that with code below) 2) retains the default orange selection behavior of android

to accomplish #1 I have an custom adapter that extends ArrayAdapter and then I override getView like so

public View getView(int position,  View convertView,   ViewGroup parent)
{
  ....

  // tableLayoutId is id pointing to each view/row in my list
  View tableLayoutView = view.findViewById(R.id.tableLayoutId); 
  if(tableLayoutView != null)
  {
      int colorPos = position % colors.length;
      tableLayoutView.setBackgroundColor(colors[colorPos]);
  }
}

my member variable for colors is

private int[] colors = new int[] { 0x30ffffff, 0x30ff2020, 0x30808080 };

followed the article "Android – Applying Alternate Row Color in ListView with SimpleAdapter" found here

now this is where i am stuck, I see on stackoverflow some mention of doing this as it would see common, and they suggest adding this attribute to the

android:listSelector="@color/list_item"

where list_item.xml would be something like

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_selected="true"
    android:drawable="@drawable/transparent" />
   .....
 </selector>

Then I would have to add code to getView() to figure out which state I am in and act accordingly.

Is there an example out there for getting this to work? Thanks all I'll gladly post mine for all to use if i could get it to work. :-(


回答1:


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.



来源:https://stackoverflow.com/questions/6552442/android-listview-alternate-row-color-but-with-default-cursor-selection

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