如何在 activity 启动时在 recyclerview 中显示已选中的复选框(多对多关系)
How to display already checked checkboxes in recyclerview, on activity start-up(many to many relationship)
每个“笔记”都以不同的方式“添加”到“文件夹”,或者我应该说,每个“笔记”的选中“文件夹”不同。
当我选中“文件夹”时,使用后退按钮返回到之前的笔记 activity,然后打开相同的“笔记”以转到“添加到文件夹 Activity”并添加『注意』文件夹,记得之前勾选『文件夹』,重新勾选。
我已经在 onBindViewHolder 和 FolderHolder(RecyclerView Holder) 中实现了该功能。
问题是当我取消选中复选框时,recyclerview 中存在多个“文件夹”,然后向下滚动,然后返回,未选中的文件夹会按原样重新检查。
我知道 onBindViewHolder 每次在滚动和回收视图时都会被调用,因此在滚动时会重新检查“文件夹”。
我想在 activity 启动时检查之前检查过的文件夹,就像现在一样,但不在滚动时重新检查它们。
你能给我推荐一个代码吗?非常感谢!
我的 recyclerview 适配器:
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;
}
}
}
我的 Activity 有 recyclerview:
public class AddToFoldersActivity extends AppCompatActivity {
public static final String NOTE_EXTRA_KEY = "note_id";
private RecyclerView foldersListRv;
private FoldersDAO folderDao;
private FoldersAddToFoldersAdapter foldersAdapter;
private Note note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_folders);
MaterialToolbar topAppBar = findViewById(R.id.top_app_bar_add_to_folders);
foldersListRv = findViewById(R.id.folder_list_add_to_folders);
folderDao = NotesDB.getInstance(this).foldersDAO();
int smallestScreenWidth = getResources().getConfiguration().smallestScreenWidthDp;
NotesDAO notesDAO = NotesDB.getInstance(this).notesDAO();
int id = getIntent().getExtras().getInt(NOTE_EXTRA_KEY, 0);
note = notesDAO.getNoteById(id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
foldersListRv.setLayoutManager(layoutManager);
loadFolders();
topAppBar.setNavigationOnClickListener(view -> finish());
}
private void loadFolders () {
NotesFoldersJoinDAO notesFoldersJoinDao = NotesDB.getInstance(this).notesFoldersJoinDAO();
List<Folder> folderList = folderDao.getAllFolders();
List<Folder> checkedFolderList = notesFoldersJoinDao.getFoldersFromNote(note.getId());
ArrayList<Folder> folders = new ArrayList<>(folderList);
this.foldersAdapter = new FoldersAddToFoldersAdapter(folders, checkedFolderList, note, this, folderDao, notesFoldersJoinDao);
this.foldersListRv.setAdapter(foldersAdapter);
}
我的“文件夹”房间数据库实体:
@Entity(tableName = "folders")
public class Folder {
@ColumnInfo(name = "folderId")
@PrimaryKey
private int id;
@ColumnInfo(name = "folderName")
private String folderName;
//The checked state gets saved as a room db column, in each folder.
private boolean checked;
public Folder() {
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//From this method, it gets and returns the checked state of the checkbox.
public boolean isChecked() {
return checked;
}
//And from this method, it sets the checked state of the checkbox.
public void setChecked(boolean checked) {
this.checked = checked;
}
}
我当然删除了一些代码,因为这会使这个问题太冗长。
我终于解决了!我移动了:
for (Folder checkedFolder : checkedFolders) {
if (checkedFolder.getId() == folder.getId()) {
folder.setChecked(true);
}
}
从 onBindViewHolder
到具有 recyclerview(AddToFoldersActivity) 的 Activity 并对其进行了一些修改以适应:
for (Folder checkedFolder : checkedFolderList) {
for (Folder folder : folderList) {
if (checkedFolder.getId() == folder.getId()) {
folder.setChecked(true);
}
}
}
适配器(文件夹添加到文件夹适配器):
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);
/* Moved code from here...
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;
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;
}
}
}
并将代码移动到 Activity(AddToFoldersActivity):
public class AddToFoldersActivity extends AppCompatActivity {
public static final String NOTE_EXTRA_KEY = "note_id";
private RecyclerView foldersListRv;
private FoldersDAO folderDao;
private FoldersAddToFoldersAdapter foldersAdapter;
private Note note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_folders);
MaterialToolbar topAppBar = findViewById(R.id.top_app_bar_add_to_folders);
foldersListRv = findViewById(R.id.folder_list_add_to_folders);
folderDao = NotesDB.getInstance(this).foldersDAO();
NotesDAO notesDAO = NotesDB.getInstance(this).notesDAO();
int id = getIntent().getExtras().getInt(NOTE_EXTRA_KEY, 0);
note = notesDAO.getNoteById(id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
foldersListRv.setLayoutManager(layoutManager);
//This is where the code really ends up.
loadFolders();
topAppBar.setNavigationOnClickListener(view -> finish());
}
//The loadFolders() gets called in onCreate().
private void loadFolders () {
NotesFoldersJoinDAO notesFoldersJoinDao = NotesDB.getInstance(this).notesFoldersJoinDAO();
List<Folder> folderList = folderDao.getAllFolders();
List<Folder> checkedFolderList = notesFoldersJoinDao.getFoldersFromNote(note.getId());
ArrayList<Folder> folders = new ArrayList<>(folderList);
//The code gets called here.
checkAlreadyCheckedFolders(checkedFolderList, folderList);
this.foldersAdapter = new FoldersAddToFoldersAdapter(folders, checkedFolderList, note, this, folderDao, notesFoldersJoinDao);
this.foldersListRv.setAdapter(foldersAdapter);
}
//The code moved to here, with a bit of modification and gets called in loadFolders().
private void checkAlreadyCheckedFolders(List<Folder> checkedFolderList, List<Folder> folderList) {
for (Folder checkedFolder : checkedFolderList) {
for (Folder folder : folderList) {
if (checkedFolder.getId() == folder.getId()) {
folder.setChecked(true);
}
}
}
}
最后是“文件夹”房间数据库实体(table):
@Entity(tableName = "folders")
public class Folder {
@ColumnInfo(name = "folderId")
@PrimaryKey
private int id;
@ColumnInfo(name = "folderName")
private String folderName;
//The checked state gets saved as a room db column, in each folder.
private boolean checked;
public Folder() {
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//From this method, it gets and returns the checked state of the checkbox.
public boolean isChecked() {
return checked;
}
//And from this method, it sets the checked state of the checkbox.
public void setChecked(boolean checked) {
this.checked = checked;
}
}
这样,复选框检查仅在 Activity(AddToFoldersActivity) starts/gets 创建时运行一次,而不是在 onBindViewHolder() 中的每个滚动中一次又一次地运行。 onBindViewHolder() 在每次滚动时运行。
每个“笔记”都以不同的方式“添加”到“文件夹”,或者我应该说,每个“笔记”的选中“文件夹”不同。
当我选中“文件夹”时,使用后退按钮返回到之前的笔记 activity,然后打开相同的“笔记”以转到“添加到文件夹 Activity”并添加『注意』文件夹,记得之前勾选『文件夹』,重新勾选。
我已经在 onBindViewHolder 和 FolderHolder(RecyclerView Holder) 中实现了该功能。
问题是当我取消选中复选框时,recyclerview 中存在多个“文件夹”,然后向下滚动,然后返回,未选中的文件夹会按原样重新检查。
我知道 onBindViewHolder 每次在滚动和回收视图时都会被调用,因此在滚动时会重新检查“文件夹”。
我想在 activity 启动时检查之前检查过的文件夹,就像现在一样,但不在滚动时重新检查它们。
你能给我推荐一个代码吗?非常感谢!
我的 recyclerview 适配器:
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;
}
}
}
我的 Activity 有 recyclerview:
public class AddToFoldersActivity extends AppCompatActivity {
public static final String NOTE_EXTRA_KEY = "note_id";
private RecyclerView foldersListRv;
private FoldersDAO folderDao;
private FoldersAddToFoldersAdapter foldersAdapter;
private Note note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_folders);
MaterialToolbar topAppBar = findViewById(R.id.top_app_bar_add_to_folders);
foldersListRv = findViewById(R.id.folder_list_add_to_folders);
folderDao = NotesDB.getInstance(this).foldersDAO();
int smallestScreenWidth = getResources().getConfiguration().smallestScreenWidthDp;
NotesDAO notesDAO = NotesDB.getInstance(this).notesDAO();
int id = getIntent().getExtras().getInt(NOTE_EXTRA_KEY, 0);
note = notesDAO.getNoteById(id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
foldersListRv.setLayoutManager(layoutManager);
loadFolders();
topAppBar.setNavigationOnClickListener(view -> finish());
}
private void loadFolders () {
NotesFoldersJoinDAO notesFoldersJoinDao = NotesDB.getInstance(this).notesFoldersJoinDAO();
List<Folder> folderList = folderDao.getAllFolders();
List<Folder> checkedFolderList = notesFoldersJoinDao.getFoldersFromNote(note.getId());
ArrayList<Folder> folders = new ArrayList<>(folderList);
this.foldersAdapter = new FoldersAddToFoldersAdapter(folders, checkedFolderList, note, this, folderDao, notesFoldersJoinDao);
this.foldersListRv.setAdapter(foldersAdapter);
}
我的“文件夹”房间数据库实体:
@Entity(tableName = "folders")
public class Folder {
@ColumnInfo(name = "folderId")
@PrimaryKey
private int id;
@ColumnInfo(name = "folderName")
private String folderName;
//The checked state gets saved as a room db column, in each folder.
private boolean checked;
public Folder() {
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//From this method, it gets and returns the checked state of the checkbox.
public boolean isChecked() {
return checked;
}
//And from this method, it sets the checked state of the checkbox.
public void setChecked(boolean checked) {
this.checked = checked;
}
}
我当然删除了一些代码,因为这会使这个问题太冗长。
我终于解决了!我移动了:
for (Folder checkedFolder : checkedFolders) {
if (checkedFolder.getId() == folder.getId()) {
folder.setChecked(true);
}
}
从 onBindViewHolder
到具有 recyclerview(AddToFoldersActivity) 的 Activity 并对其进行了一些修改以适应:
for (Folder checkedFolder : checkedFolderList) {
for (Folder folder : folderList) {
if (checkedFolder.getId() == folder.getId()) {
folder.setChecked(true);
}
}
}
适配器(文件夹添加到文件夹适配器):
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);
/* Moved code from here...
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;
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;
}
}
}
并将代码移动到 Activity(AddToFoldersActivity):
public class AddToFoldersActivity extends AppCompatActivity {
public static final String NOTE_EXTRA_KEY = "note_id";
private RecyclerView foldersListRv;
private FoldersDAO folderDao;
private FoldersAddToFoldersAdapter foldersAdapter;
private Note note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_folders);
MaterialToolbar topAppBar = findViewById(R.id.top_app_bar_add_to_folders);
foldersListRv = findViewById(R.id.folder_list_add_to_folders);
folderDao = NotesDB.getInstance(this).foldersDAO();
NotesDAO notesDAO = NotesDB.getInstance(this).notesDAO();
int id = getIntent().getExtras().getInt(NOTE_EXTRA_KEY, 0);
note = notesDAO.getNoteById(id);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
foldersListRv.setLayoutManager(layoutManager);
//This is where the code really ends up.
loadFolders();
topAppBar.setNavigationOnClickListener(view -> finish());
}
//The loadFolders() gets called in onCreate().
private void loadFolders () {
NotesFoldersJoinDAO notesFoldersJoinDao = NotesDB.getInstance(this).notesFoldersJoinDAO();
List<Folder> folderList = folderDao.getAllFolders();
List<Folder> checkedFolderList = notesFoldersJoinDao.getFoldersFromNote(note.getId());
ArrayList<Folder> folders = new ArrayList<>(folderList);
//The code gets called here.
checkAlreadyCheckedFolders(checkedFolderList, folderList);
this.foldersAdapter = new FoldersAddToFoldersAdapter(folders, checkedFolderList, note, this, folderDao, notesFoldersJoinDao);
this.foldersListRv.setAdapter(foldersAdapter);
}
//The code moved to here, with a bit of modification and gets called in loadFolders().
private void checkAlreadyCheckedFolders(List<Folder> checkedFolderList, List<Folder> folderList) {
for (Folder checkedFolder : checkedFolderList) {
for (Folder folder : folderList) {
if (checkedFolder.getId() == folder.getId()) {
folder.setChecked(true);
}
}
}
}
最后是“文件夹”房间数据库实体(table):
@Entity(tableName = "folders")
public class Folder {
@ColumnInfo(name = "folderId")
@PrimaryKey
private int id;
@ColumnInfo(name = "folderName")
private String folderName;
//The checked state gets saved as a room db column, in each folder.
private boolean checked;
public Folder() {
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//From this method, it gets and returns the checked state of the checkbox.
public boolean isChecked() {
return checked;
}
//And from this method, it sets the checked state of the checkbox.
public void setChecked(boolean checked) {
this.checked = checked;
}
}
这样,复选框检查仅在 Activity(AddToFoldersActivity) starts/gets 创建时运行一次,而不是在 onBindViewHolder() 中的每个滚动中一次又一次地运行。 onBindViewHolder() 在每次滚动时运行。