android listview交替行颜色但具有默认光标选择
我已经遍布网络,包括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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方法是使用 2 个选择器。在适配器中,您不是设置 2 种颜色,而是设置 2 个选择器。
选择器_1 在选择器_1.xml 中定义如下:
选择器_2 在选择器_2.xml 中定义如下:
这样,您就有一个双色列表视图和第三种颜色/形状/您想要的所选项目。
A workaround is to use 2 selectors. From your adapter, instead of setting 2 colors, you set 2 selectors.
selector_1 is defined in selector_1.xml like this:
selector_2 is defined in selector_2.xml like this:
So that, you have a bi-color listview and a third color/shape/whatever-you-want for selected item.