【发布时间】:2018-08-21 08:12:38
【问题描述】:
我使用 FirestoreRecyclerAdapter,我想在其中更改所选项目的颜色。因为在 firestoreAdapter 中它涉及到 onBindViewHolder() 只有项目数据更改。对于单个项目替换它,但我想将所有项目文本颜色更改为除选定项目之外的所有项目文本颜色。
我该怎么办?
谢谢。
【问题讨论】:
标签: android firebase google-cloud-firestore
我使用 FirestoreRecyclerAdapter,我想在其中更改所选项目的颜色。因为在 firestoreAdapter 中它涉及到 onBindViewHolder() 只有项目数据更改。对于单个项目替换它,但我想将所有项目文本颜色更改为除选定项目之外的所有项目文本颜色。
我该怎么办?
谢谢。
【问题讨论】:
标签: android firebase google-cloud-firestore
然后颠倒你的逻辑。声明一个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
}
}
}
【讨论】: