简体   繁体   English

3秒后更改ListView项的背景颜色

[英]Change background color of ListView item after 3 seconds

I have a list of Items that are "seen" or "not seen" in ArrayList<Item> . 我在ArrayList<Item>有一个“已看到”或“未看到”的ArrayList<Item> If they're not seen I change the background color of the ListView item in my CustomArrayAdapter like this : 如果没有看到它们,我将在CustomArrayAdapter更改ListView项的背景颜色,如下所示:

 if(item.is_seen == null || item.is_seen == 0) {
    row.setBackgroundResource(R.color.yellow);
 } else {
    row.setBackgroundResource(R.color.transparent);
 }

Now what I want to do is set all items background to transparent after 3 seconds spent on the page. 现在,我要在页面上停留3秒钟后将所有项目的背景设置为透明。

I already tried to do something like this: 我已经尝试做这样的事情:

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

Updating the value in the DB works, but the rest doesn't. 更新数据库中的值可以,但是其余的则不行。 But I'd like to avoid to reload the data. 但我想避免重新加载数据。 I just need the color to change. 我只需要改变颜色。

I don't have errors, it just does nothing. 我没有错误,它什么也没做。

I look everywhere for an answer and didn't find one. 我到处寻找答案,但没有找到答案。 Am I wrong since the beginning? 我从一开始就错了吗? Is what I want to achieve even possible? 我想实现的目标是否可能?

I thank you in advance for all the help you could give me. 我预先感谢您能给我的所有帮助。

Instead of changing the color of the view like youre doing, 无需像您一样更改视图的颜色,

// change the color of backgrounds
        View view = listViewItem.getChildAt(i);
        if(view != null) {
            view.setBackgroundResource(R.color.red);
        } 

(code above will not work, because the views are recycled in the ListAdapter) update the DATA off which you build your list - add a property to the class you are passing into your ListAdapter, then grab that instance from the list and update that property, you have the position at which it needs to be updated already, so that's easy. (上面的代码将不起作用,因为视图是在ListAdapter中回收的)更新用于构建列表的DATA-向要传递到ListAdapter的类中添加一个属性,然后从列表中获取该实例并更新该属性,您已经拥有需要更新的位置,因此很容易。 Then, call notifyDataSetChanged() on the list. 然后,在列表上调用notifyDataSetChanged()。 It will not redraw the list if you didn't ADD/REMOVE items from list, but it will update correct view for you. 如果您没有从列表中添加/删除项目,它将不会重新绘制列表,但是它将为您更新正确的视图。 This is the only way to do it - absolutely NO WAY to get to a view corresponding to a specific element in a list after list has been drawn already. 这是唯一的方法-绝对不会在已经绘制列表之后到达对应于列表中特定元素的视图。 Only way is to refresh/redraw the list with notifyDataSetChanged(), followed by refreshDrawableState(). 唯一的方法是使用notifyDataSetChanged(),然后是refreshDrawableState()刷新/重绘列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM