-
Notifications
You must be signed in to change notification settings - Fork 0
Delete with Undo
The list offers the possibility to delete list items and undo the deletion again.
To use that feature you have to create an OnDismissCallback
and pass it to
EnhancedRecyclerListView.setDismissCallback
. If you try to delete an list item before you have done that, you will cause an IllegalStateException
.
You have to implement the onDismiss
method that will be called for every deleted item. The implementation needs to remove the item from your adapter. If you use undo (see below), your implementation most likely will just remove the item from the adapter. If you don't use undo, you also want to delete it from whatever persistent storage you use.
Note: If you modify your adapter in another thread, you must make sure, the onDismiss
method won't return, before the item has been deleted from the adapter.
To delete an item just call the delete(int)
method on the list. It will fade out the item, and if you use undo (see below) show the undo possibility for this item.
If you don't want to use undo on the list view, just return null
from the onDismiss
method. In that case the user won't have the possibility to undo the deletion.
If you want to give the user the possibility of undo, just return an Undoable
from the onDismiss
method.
You must at least overwrite its undo
method. This method will be called when the user presses undo, and you have to reinsert the deleted item into the adapter again.
There are two optional methods you can also overwrite. Overwrite getTitle
to show a special title for this deleted item in the undo window. By default "Item deleted" will be shown or an similar message for collapsed undo (see below). If you return something from getTitle
this will be shown instead.
The second optional method is discard
. This will be called, as soon as the user doesn't have the possibility to undo that deletion anymore. Meaning it will be called as soon as the undo button vanished from the screen. Commonly you will use this method to finally delete the item from a persistent storage (and the onDismiss
method to only remove it from the adapter.
A possible implementation for an OnDismissCallback
that contains a list of persons:
setDismissCallback(new OnDismissCallback() {
@Override public Undoable onDismiss(EnhancedList listView, final int position) {
final String item = (String) adapter.getItem(position);
adapter.remove(position);
return new Undoable() {
@Override public void undo() {
adapter.insert(position, item);
}
@Override public String getTitle() {
return "Deleted '" + item.getFullName() . "'"; // Plz, use the resource system :)
}
@Override public void discard() {
MyDatabasUtils.delete(item);
}
};
}
});
Note: To make sure that discard
will always be called, so you don't end up with items getting reinserted the list on the next load, you have to call discardUndo()
on the list view in the onStop
(or onPause
) method of your activity/fragment. You might also want to call it when you're using a NavigationDrawer and the drawer opens, since otherwise the undo popup will overlay the NavigationDrawer.
The list has several different styles to handle undos. You can set these with setUndoStyle
. If you don't set anything UndoStyle.SINGLE_POPUP
will be used.
SINGLE_POPUP: A popup window is shown at the bottom of the screen that offers an undo button. Only the last deletion can be undone.
MULTILEVEL_POPUP: A popup window is shown at the bottom of the screen. If you make another deletion, while the undo window is still shown, the undo is added to the chains of undos. Pressing 'undo' will undo the last deletion, but the window won't vanish. Pressing 'undo' again will undo the deletion before that, and so on. This is the behavior used from the undo button in desktop applications.
COLLAPSED_POPUP: A popup window is shown at the bottom of the screen. If you make another deletion, while the undo window is still shown, the undo is added to the list of undos. The undo button changes it text to 'Undo all' and pressing it, will cause all waiting undos to be undone all together.
By undo button will only stay on the screen for some time. After that time, it will fade out from the screen, the user doesn't have the possibility to undo the deletion anymore and discard
on your Undoable
will be called. By default this time is 5 seconds. You can change that value via setUndoHideDelay
. The method expects the delay in milliseconds.
By default the delay will not start immediately after the deletion, but waits for the next touch of the user (to make sure the user is currently interacting with the screen, and aware of the undo possibility). If you don't want that behavior you can deactivate it with setRequireTouchBeforeDismiss(false)
.
You can change the offset of the undo popup from the bottom of the screen by creating a dimen
resource with the name elv_undo_bottom_offset
and set it to the desired value.
The "Undo" String can be localized by setting an resource called elv_undo.