我在RecyclerView中得到了一些简单的物品清单。使用SwipeTouchHelper,它可以很好地用于撤消和删除操作。我面临的问题是,当我按下Snakbar中的undo时,它会还原项目,并且在下一行中还会显示滑动条。
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction, int position) {
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
appDatabase.open();
itemsList = appDatabase.getfullList(title_id);
appDatabase.close();
// get the removed item name to display it in snackbar
String text = itemsList.get(viewHolder.getAdapterPosition()).getText();
final String id = itemsList.get(position).getId();
// backup of removed item for undo purpose
final int adapterPosition = viewHolder.getAdapterPosition();
final ItemModel items = itemsList.get(adapterPosition);
// showing snack bar with Undo option
Snackbar snackbar = Snackbar.make(coordinatorLayout, text + " removed !", Snackbar.LENGTH_SHORT);
snackbar.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.restoreItem(items, adapterPosition);
}});
snackbar.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
public void onDismissed(Snackbar transientBottomBar, int event) {
switch (event) {
case DISMISS_EVENT_TIMEOUT:
ListNoteDataBase appDatabase = new ListNoteDataBase(context);
adapter.removeItem(viewHolder.getAdapterPosition());
appDatabase.open();
appDatabase.deleteNotes(id);
adapter = new NoteListAdapter(context, appDatabase.getfullList(title_id));
recyclerView.setAdapter(adapter);
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
checkEmpty();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
checkEmpty();
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
checkEmpty();
}});
adapter.notifyDataSetChanged();
appDatabase.close();
break;
}
}
});
snackbar.setActionTextColor(Color.RED);
snackbar.show();
}
从适配器恢复项目的代码:
public void restoreItem(ItemModel item, int position) {
data.add(position, item);
notifyItemInserted(position);
}
In the image, the red line show below a text appears after I press the Undo button from the snakbar
请指导我如何解决此问题。