简体   繁体   中英

How RecyclerView's item interact to each other

In my recyclerView contain few option with checkbox. 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. 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().

The major problem I faced is, I have no idea on how item affect another item within Recyclerview.

There are many ways to solve this

Ex. You can add isChecked property to your model

public class Fruit {
  private Boolean isChecked;
}

Then set it to true/false in OnClickListener of each item. You check it value in OnBindViewHolder to show UI checkbox ticked / not ticked

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. Third, use adapter.notifyDataSetChanged() to update RecyclerView UI.

Follow my github to get detail : https://github.com/pvnhat/ArchitectureComponentDemo/pull/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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