我已经遍及整个网络,包括 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 – Applying Alternate Row Color in ListView with SimpleAdapter”发现here
现在这就是我陷入困境的地方,我在 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() 添加代码来确定我处于哪个状态
并采取相应行动。
有没有一个例子可以让它发挥作用?谢谢大家
如果我能让它发挥作用,我很乐意将其发布供所有人使用。 :-(