[英]Change background color of ListView item after 3 seconds
我在ArrayList<Item>
有一个“已看到”或“未看到”的ArrayList<Item>
。 如果没有看到它们,我将在CustomArrayAdapter
更改ListView
项的背景颜色,如下所示:
if(item.is_seen == null || item.is_seen == 0) {
row.setBackgroundResource(R.color.yellow);
} else {
row.setBackgroundResource(R.color.transparent);
}
现在,我要在页面上停留3秒钟后将所有项目的背景设置为透明。
我已经尝试做这样的事情:
mScheduledExecutor.schedule(new Runnable() {
@Override
public void run() {
for(int i=0; i<mItems.size(); i++) {
final Item n = mItems.get(i);
if(n.is_seen == null || n.is_seen == 0) {
// update value in db
int isSeen = 1;
updateItem(n._id, isSeen);
// change the color of backgrounds
View view = listViewItem.getChildAt(i);
if(view != null) {
view.setBackgroundResource(R.color.red);
}
}
}
更新数据库中的值可以,但是其余的则不行。 但我想避免重新加载数据。 我只需要改变颜色。
我没有错误,它什么也没做。
我到处寻找答案,但没有找到答案。 我从一开始就错了吗? 我想实现的目标是否可能?
我预先感谢您能给我的所有帮助。
无需像您一样更改视图的颜色,
// change the color of backgrounds
View view = listViewItem.getChildAt(i);
if(view != null) {
view.setBackgroundResource(R.color.red);
}
(上面的代码将不起作用,因为视图是在ListAdapter中回收的)更新用于构建列表的DATA-向要传递到ListAdapter的类中添加一个属性,然后从列表中获取该实例并更新该属性,您已经拥有需要更新的位置,因此很容易。 然后,在列表上调用notifyDataSetChanged()。 如果您没有从列表中添加/删除项目,它将不会重新绘制列表,但是它将为您更新正确的视图。 这是唯一的方法-绝对不会在已经绘制列表之后到达对应于列表中特定元素的视图。 唯一的方法是使用notifyDataSetChanged(),然后是refreshDrawableState()刷新/重绘列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.