I have a recyclerview and a recylerview adapter. I have a Room database. Also I have 2 Room models/entities which they're named "Folder" and "Note" and are Many-to-many relationship. The recyclerview displays all the "Folders" and each "Folder" have a checkbox. Each "Note" is differently "added" to "Folders" or should I say, different checked folders for each "Note".
I have already implemented a functionality which checks all "Folder" checkboxes at activity start-up, inside onBindViewHolder and in FolderHolder(RecyclerView Holder) in recyclerview adapter.
The problem is when multiple "Folders" exist in recyclerview when I uncheck the checkbox, then scroll down, then back up, the unchecked folder rechecks itself as it was. I understand that onBindViewHolder gets called every time on scroll and recycling views thus checked "Folder" gets rechecked on scroll.
Please can you suggest me a code to this? I would really appreciate this!
My recyclerview adapter:
public class FoldersAddToFoldersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
private final ArrayList<Folder> folders;
private final List<Folder> checkedFolders;
private final Note note;
private final FoldersDAO foldersDao;
private final NotesFoldersJoinDAO notesFoldersJoinDao;
public FoldersAddToFoldersAdapter(ArrayList<Folder> folders, List<Folder> checkedFolders, Note note, Context context,
FoldersDAO foldersDao, NotesFoldersJoinDAO notesFoldersJoinDao) {
this.folders = folders;
this.checkedFolders = checkedFolders;
this.note = note;
this.context = context;
this.foldersDao = foldersDao;
this.notesFoldersJoinDao = notesFoldersJoinDao;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.folder_add_to_folders_layout, parent, false);
return new FolderHolder(v, this, notesFoldersJoinDao);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final FolderHolder folderHolder = (FolderHolder) holder;
final Folder folder = getFolder(position);
if (folder != null) {
//[..]
folderHolder.setData(note, folder);
// **THIS IS THE CODE WHICH CHECKS THE FOLDERS, WHICH I WAS SAYING ABOUT.**
for (Folder checkedFolder : checkedFolders) {
if (checkedFolder.getId() == folder.getId()) {
folder.setChecked(true);
}
}
folderHolder.checkBoxAddToFolders.setChecked(folder.isChecked());
}
}
public void addCheckedFolder(Folder folder) {
checkedFolders.add(folder);
}
public void removeCheckedFolder(Folder folder) {
checkedFolders.remove(folder);
}
public static class FolderHolder extends RecyclerView.ViewHolder {
CheckBox checkBoxAddToFolders;
FoldersAddToFoldersAdapter adapter;
Note note;
Folder folder;
NotesFoldersJoinDAO notesFoldersJoinDao;
public FolderHolder(@NonNull View itemView, FoldersAddToFoldersAdapter adapter, NotesFoldersJoinDAO notesFoldersJoinDao) {
super(itemView);
checkBoxAddToFolders = itemView.findViewById(R.id.checkbox_add_to_folders);
this.notesFoldersJoinDao = notesFoldersJoinDao;
this.adapter = adapter;
// THIS IS ALSO IMPORTANT FOR CHECKBOX.
checkBoxAddToFolders.setOnClickListener(v -> {
boolean isChecked = checkBoxAddToFolders.isChecked();
NoteFolderJoin noteFolderJoin = new NoteFolderJoin(note.getId(), folder.getId());
if (isChecked) {
folder.setChecked(true);
adapter.addCheckedFolder(folder);
notesFoldersJoinDao.insertNoteFolderJoin(noteFolderJoin);
} else {
folder.setChecked(false);
adapter.removeCheckedFolder(folder);
notesFoldersJoinDao.deleteNoteNoteFolderJoin(noteFolderJoin);
}
});
public void setData(Note note, Folder folder) {
this.note = note;
this.folder = folder;
}
}
}
Aucun commentaire:
Enregistrer un commentaire