【问题标题】:change FirestoreRecyclerAdapter clicked item color更改 FirestoreRecyclerAdapter 单击的项目颜色
【发布时间】:2018-08-21 08:12:38
【问题描述】:

我使用 FirestoreRecyclerAdapter,我想在其中更改所选项目的颜色。因为在 firestoreAdapter 中它涉及到 onBindViewHolder() 只有项目数据更改。对于单个项目替换它,但我想将所有项目文本颜色更改为除选定项目之外的所有项目文本颜色。

我该怎么办?

谢谢。

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    然后颠倒你的逻辑。声明一个list 来保留被点击的项目(存储索引、id 或任何你喜欢的东西),然后在你的onBindViewHolder() 中,如果被点击的项目在那个list 中,则不做任何事情,否则改变任何你想要的。

    当然,如果您允许在列表中插入/删除/更改,则必须更新此 list

    编辑

    要回答您在 cmets 中的问题,首先您必须设计逻辑以在 onBindViewHolder() 中相应地更改颜色。然后,将onClickListener 设置为响应视图。最后,要刷新您的显示列表,请调用notifyDataSetChanged() 或类似方法。

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        // declaration and setup here
    
        final index = position;
    
        // Define the logic to change the color if clicked item is/not in the list
        if (yourClickedItemList.contain(position)) {
            // Change what you want
        }
        else {
            // Change what you want if the item is not in the list
        }
    
        // Then set the click listener
        viewHolder.yourItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!yourClickedItemList.contain(index)) {
                    yourClickedItemList.add(index);
                }
                else {
                    // the list already contain that item
                    // do whatever you want here, like toggle off selection and remove from the list
                }
    
                // call to refresh your view
                notifyDataSetChanged(); // or use notifyItemChanged() etc
            }
        }
    }
    

    【讨论】:

    • 但我只能在 onBindViewHolder() 中更改项目颜色和背景。例如:第一次我想要 0 索引项颜色更改,然后如果我单击另一个项,那么我想将单击的索引项颜色和第 0 个索引项颜色更改为正常。
    • 并且每次数据更改我的整个列表都不会刷新。只有点击的项目替换
    猜你喜欢
    • 1970-01-01
    • 2017-06-13
    • 2019-08-20
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多