简体   繁体   English

RecyclerView的物品如何相互影响

[英]How RecyclerView's item interact to each other

In my recyclerView contain few option with checkbox. 在我的recyclerView中包含带有复选框的少量选项。 I need those options be able to communicate to each other, how should I do it? 我需要这些选项能够相互通信,该怎么办?

I guess the functions should written in onBindViewHolder() but I'm not sure how to do it. 我猜这些函数应该用onBindViewHolder()编写,但是我不确定该怎么做。 I search through internet but I couldn't find a similar way to solve my problem. 我通过互联网搜索,但是找不到解决问题的类似方法。

For example: 例如:

-------------------------
1. All (With Checkbox)
-------------------------
2. Banana (With Checkbox)
-------------------------
3. Apple (With Checkbox)
-------------------------
4. Meat (With Checkbox)
-------------------------
5. Coconut (With Checkbox)
-------------------------

So when I check on "All" option, the rest of the checkbox must be check automatically by using onCheckedChangeListener(). 因此,当我选中“全部”选项时,必须使用onCheckedChangeListener()自动检查其余复选框。

The major problem I faced is, I have no idea on how item affect another item within Recyclerview. 我面临的主要问题是,我不知道项目如何影响Recyclerview中的另一个项目。

There are many ways to solve this 有很多方法可以解决这个问题

Ex. 例如 You can add isChecked property to your model 您可以将isChecked属性添加到模型中

public class Fruit {
  private Boolean isChecked;
}

Then set it to true/false in OnClickListener of each item. 然后在每个项目的OnClickListener中将其设置为true / false。 You check it value in OnBindViewHolder to show UI checkbox ticked / not ticked 您在OnBindViewHolder中检查它的值以显示已选中/未选中的UI复选框

For Select All option, you can set 对于全选选项,您可以设置

isChecked = true

for all items in list. 对于列表中的所有项目。

So here is what you wanna be ? 所以这就是你想要的? 在此处输入图片说明

In my opinion: First, we need add a property for Fruit to use CheckBox, ex: isFresh,... Second, change dataList (pass to RecyclerView Adapter) when tick CheckBox. 在我看来:首先,我们需要为Fruit添加一个属性以使用CheckBox,例如:isFresh...。其次,在勾选CheckBox时更改dataList(传递给RecyclerView Adapter)。 Third, use adapter.notifyDataSetChanged() to update RecyclerView UI. 第三,使用adapter.notifyDataSetChanged()更新RecyclerView UI。

Follow my github to get detail : https://github.com/pvnhat/ArchitectureComponentDemo/pull/2 跟随我的github获取详细信息: https : //github.com/pvnhat/ArchitectureComponentDemo/pull/2

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM