简体   繁体   English

如何更改所选项目的颜色以及使用 Firebase Recycler Adapter 时?

[英]How to change the color of selected item and when using Firebase Recycler Adapter?

I'm using the Firebase RecyclerAdapter to display a list of things.我正在使用 Firebase RecyclerAdapter 来显示事物列表。 How do I change the colour of all the items in the RecyclerView except the selected item back to a different colour?如何将 RecyclerView 中除所选项目之外的所有项目的颜色更改回不同的颜色? Changing the colour of the selected item is straightforward.更改所选项目的颜色很简单。

 FirebaseRecyclerAdapter<Home_List, SubChatHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Home_List, SubChatHolder>(

                Home_List.class,
                R.layout.single_division_layout,
                SubChatHolder.class,
                ref

        ) {
            @Override
            protected void populateViewHolder(final SubChatHolder viewHolder, final Home_List model, int position) {

                final String id = getRef(position).getKey();

                viewHolder.setName(model.getName());

                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       mView.viewHolder.setSelectedBg();
                       //how to uncolour the previously selected item?
                    }
                });
            }
        };

How are you selecting the position?您如何选择 position? By clicking on the item?通过点击项目?

If that's the case, you don't event need the position.如果是这种情况,您就不需要 position。 All you need to do is to change the (background) color of mView in your onClick method, where your change the color of selected item comment is.您需要做的就是在您的onClick方法中更改mView的(背景)颜色,您change the color of selected item

Your problem here is going to be deselection of the item, as it is going to remain colored otherwise.您在这里的问题将是取消选择该项目,因为否则它将保持彩色。 You are going to have to find all the views inside the RecyclerView and reset the background color.您将不得不找到RecyclerView中的所有视图并重置背景颜色。

EDIT编辑

Here is the link from comments with one of possible solutions: https://stackoverflow.com/a/40712773/6835732以下是评论中的链接以及可能的解决方案之一: https://stackoverflow.com/a/40712773/6835732

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

相关问题 更改 Firebase Recycler Adapter 中的项目位置 - Change Item Position in Firebase Recycler Adapter 如何在Recycler视图中更改所选项目的文本颜色 - how to change the text color of selected item inside recycler view 如何更改Recycler Item Color? - How to change Recycler Item Color? 如何从firebase onClick获取回收器适配器中的回收器项目的数据? - How to get the data of a recycler item in a recycler adapter from firebase onClick? 如何获取数据并更改所选适配器项的背景颜色? - How to get data and change the background color of selected adapter item? 如何设置“回收者视图”中所选项目的颜色? - how to set color the selected Item of Recycler View? 如何将选定的回收站视图项目与更改背景颜色的第一个项目交换 - How to swap the selected recycler view item with the first item with change background color 将其状态设置为禁用时如何在不使用适配器的情况下以编程方式更改微调器项目文本的颜色 - How to change spinner item text color programmatically without using adapter when set its state as disabled 选择项目时更改“回收者”视图的前景色 - Changing foreground color of Recycler view when an item is selected Firebase回收器适配器的每个项目的按钮中的事件 - Event in a button of each Item of a Firebase Recycler Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM