android listview交替行颜色但具有默认光标选择

发布于 2024-11-18 06:20:22 字数 1390 浏览 8 评论 0原文

我已经遍布网络,包括stackoverflow,但似乎无法找到一个清晰完整的方法来

我想创建一个ListView,

1)具有交替颜色(我可以使用下面的代码来做到这一点) 2) 保留 android 的默认橙色选择行为

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

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]);
  }
}

我的颜色成员变量就

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

遵循文章“Android – 使用 SimpleAdapter 在 ListView 中应用备用行颜色”,找到 这里

现在这就是我陷入困境的地方,我在 stackoverflow 上看到一些提到这样做,因为它会很常见,他们建议将此属性添加到

android:listSelector="@color/list_item"

其中 list_item.xml 类似于

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

然后我必须向 getView() 添加代码来确定我处于哪个状态 并采取相应行动。

有没有一个例子可以让它发挥作用?谢谢大家 如果我能让它发挥作用,我很乐意将其发布供所有人使用。 :-(

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. :-(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2024-11-25 06:20:22

解决方法是使用 2 个选择器。在适配器中,您不是设置 2 种颜色,而是设置 2 个选择器。

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

选择器_1 在选择器_1.xml 中定义如下:

<?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>

选择器_2 在选择器_2.xml 中定义如下:

<?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>

这样,您就有一个双色列表视图和第三种颜色/形状/您想要的所选项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文