本文介绍了Android的列表视图交替行的颜色,但与默认光标选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直都在网上,计算器包括在内,似乎仍不能得到一个明确完整的方式

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

我想创建一个的ListView

I want to create a ListView that

1)具有交替的颜色(我能够做到这一点与低于code)2)保留了Android的默认橙色选择行为

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

要完成#1我有一个自定义适配器扩展ArrayAdapter然后我重写getView像这样

to accomplish #1 I have an custom adapter thatextends 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 };

随后的文章Android的 - ListView中与SimpleAdapter应用备用行颜色中的这里

现在,这是我坚持,我就计算器一些提到这样做的,因为它会看到共同的看,他们建议将这个属性的

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

安卓listSelector =@色/ list_item可

android:listSelector="@color/list_item"

在这里list_item.xml会是这样

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>

然后,我将不得不增加code到getView()要弄清楚我在哪个国家并采取相应的行动。

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

有一个例子了那里得到这个工作?谢谢大家我会很乐意张贴矿供大家使用,如果我能得到它的工作。 : - (

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

推荐答案

一个解决办法是使用2选择。而不是设置2种颜色从您的适配器,设置2选择。

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在selector_1.xml定义是这样的:

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在selector_2.xml定义是这样的:

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>

这样,你有一个双色ListView和第三彩色/形状/任何你想要的选项。

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

这篇关于Android的列表视图交替行的颜色,但与默认光标选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:47
查看更多