我想在没有选择项目时完成actionmode。我尝试了下面的代码,但没有成功。
bookAdapter.setOnLongClickListener(new RecyclerViewAdapter.OnLongClickListener() {
@Override
public void onLongClick() {
if(mActionMode != null) {
if(bookAdapter.getSelectedBooks().isEmpty()) {
mActionMode.finish();
}
return;
}
ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.delete_book_menu, menu);
buttonAddBook.setVisibility(View.INVISIBLE);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_book :
for(Book book : bookAdapter.getSelectedBooks()) {
bookViewModel.delete(book);
}
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
for(Book book : bookAdapter.getBookList()) {
book.setSelected(false);
bookAdapter.notifyDataSetChanged();
}
buttonAddBook.setVisibility(View.VISIBLE);
}
};
mActionMode = startSupportActionMode(mActionModeCallback);
}
});
但是当我更改代码时,例如当我尝试在选择2个项目时完成模式时,它就起作用了。具体如下:
if(mActionMode != null) {
if(bookAdapter.getSelectedBooks().size() == 2) {
mActionMode.finish();
}
return;
}
我做错什么了?如果你能帮助我,我将不胜感激。提前谢谢。
1条答案
按热度按时间fd3cxomn1#
你用了那个吗
recyclerview-selection
?我认为这篇文章会有用的。https://proandroiddev.com/a-guide-to-recyclerview-selection-3ed9f2381504