在屏幕旋转时,所选项目未被选中,我想保存其状态。我在recyclerView中拥有所有这些表单,点击后即可选中。这是onListItemClick中的代码
private void onListItemClick(View view, int position) {
Cursor cursor = instanceAdapter.getCursor();
cursor.moveToPosition(position);
CheckBox checkBox = view.findViewById(R.id.checkbox);
checkBox.setChecked(!checkBox.isChecked());
long id = cursor.getLong(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns._ID));
if (selectedInstances.contains(id)) {
selectedInstances.remove(id);
} else {
selectedInstances.add(id);
}
Bundle bundle=new Bundle();
sendButton.setEnabled(selectedInstances.size() > 0);
toggleButtonLabel();
}
其中selectedInstances是LinkedHashSet
private LinkedHashSet<Long> selectedInstances;
这是GIF
除非您可以将它存储在数据库或任何“持久”的内容中,否则您可以保留一个包含与ListView一样多的条目的布尔列表/数组。选中第二个复选框时,设置array [1] = true。
然后在适配器中,您只需检查当前项目列表位置的状态。
有点像一个例子
boolean[] checkedState = new boolean[list.count];
private void onListItemClick(View view, int position) {
//...
checkedState[position] = //checked state
}
//adapter
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
//...
checkBox.isChecked = checkedState[position]
}
ViewModel存储在应用程序轮换时未销毁的UI相关数据。