【问题标题】:Set different color of CardView for newly added items in recycler view为回收站视图中新添加的项目设置不同颜色的 CardView
【发布时间】:2017-12-13 09:02:38
【问题描述】:

我想为 Firebase Recycler 视图中添加的项目设置不同的颜色。我正在使用卡片视图来显示数据。我想要的是:假设我已经在列表中有一些数据,并且它们将具有白色背景。现在假设在列表中添加了一些新值。我希望新添加的项目与已经存在的项目具有不同的背景色调(比如绿色)。我正在从 Firebase 获取数据。现在,在我查看卡片详细信息或第一次查看新数据后,这些新卡片的颜色现在应该是白色的,因为它们现在已经旧了。如果卡片在读取卡片详细信息之前保持其颜色会很有帮助(通过点击卡片来访问卡片详细信息,该卡片会启动一个包含详细信息的新活动)。

【问题讨论】:

  • 你有什么尝试吗??
  • 不...实际上我不知道如何开始... @Adithya

标签: android firebase firebase-realtime-database android-recyclerview android-cardview


【解决方案1】:
  • 你必须像下面那样实现Adapter类的getItemViewType方法

    @Override
    public int getItemViewType(int position) {
        // here i'm assuming that you've some method isNewItem(position)
        // which returns item type as integer and
        // ITEM_TYPE_NEW and ITEM_TYPE_OLD are declared as constants
        if (isNewItem(position)) {
            return ITEM_TYPE_NEW;
        } else {
            return ITEM_TYPE_OLD;
        }
    }
    
  • onBindViewHolder 方法中,如果它是 new 类型,您应该更改背景颜色。

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final int itemType = getItemViewType(position);
        if (itemType == ITEM_TYPE_NEW) {
            // here I'm assuming that there is setNewItemBackColor method
            // present in your adapter class which changes background color
            // of card view of ViewHolder
            ((YourViewHolderClassNameHere)holder).setNewItemBackColor();
        }
    }
    

更详细的实现请参考this的回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多