[英]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.