public class SwipeDismissRecyclerViewTouchListener
extends java.lang.Object
implements android.view.View.OnTouchListener
View.OnTouchListener that makes the list items in a ListView dismissable.
ListView is given special treatment because by default it handles touches for its list
items... i.e. it's in charge of drawing the pressed state (the list selector), handling list
item
clicks, etc.
After creating the listener, the caller should also call AbsListView.setOnScrollListener(AbsListView.OnScrollListener), passing in the scroll listener
returned by makeScrollListener(). If a scroll listener is already assigned, the caller
should still pass scroll changes through to this listener. This will ensure that this SwipeDismissRecyclerViewTouchListener is paused during list view scrolling.
Example usage:
SwipeDismissRecyclerViewTouchListener touchListener =
new SwipeDismissRecyclerViewTouchListener(
listView,
new SwipeDismissRecyclerViewTouchListener.OnDismissCallback() {
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
adapter.remove(adapter.getItem(position));
}
adapter.notifyDataSetChanged();
}
});
listView.setOnTouchListener(touchListener);
listView.setOnScrollListener(touchListener.makeScrollListener());
This class Requires API level 12 or later due to use of ViewPropertyAnimator.
For a generalized View.OnTouchListener that makes any view dismissable, see SwipeDismissRecyclerViewTouchListener.
SwipeDismissRecyclerViewTouchListener| Modifier and Type | Class and Description |
|---|---|
static interface |
SwipeDismissRecyclerViewTouchListener.DismissCallbacks
The callback interface used by
SwipeDismissRecyclerViewTouchListener to inform its
client about a successful dismissal of one or more list item positions. |
| Constructor and Description |
|---|
SwipeDismissRecyclerViewTouchListener(RecyclerView recyclerView,
SwipeDismissRecyclerViewTouchListener.DismissCallbacks callbacks)
Constructs a new swipe-to-dismiss touch listener for the given list view.
|
| Modifier and Type | Method and Description |
|---|---|
RecyclerView.OnScrollListener |
makeScrollListener()
Returns an
AbsListView.OnScrollListener to be added to the ListView using
AbsListView.setOnScrollListener(AbsListView.OnScrollListener). |
boolean |
onTouch(android.view.View view,
android.view.MotionEvent motionEvent) |
void |
setEnabled(boolean enabled)
Enables or disables (pauses or resumes) watching for swipe-to-dismiss gestures.
|
public SwipeDismissRecyclerViewTouchListener(RecyclerView recyclerView,
SwipeDismissRecyclerViewTouchListener.DismissCallbacks callbacks)
recyclerView - The list view whose items should be dismissable.callbacks - The callback to trigger when the user has indicated that she would like
to dismiss one or more list items.public void setEnabled(boolean enabled)
enabled - Whether or not to watch for gestures.public RecyclerView.OnScrollListener makeScrollListener()
AbsListView.OnScrollListener to be added to the ListView using
AbsListView.setOnScrollListener(AbsListView.OnScrollListener). If a scroll listener is
already assigned, the caller should still pass scroll changes through to this listener. This
will ensure that this SwipeDismissRecyclerViewTouchListener is paused during list
view scrolling.SwipeDismissRecyclerViewTouchListenerpublic boolean onTouch(android.view.View view,
android.view.MotionEvent motionEvent)
onTouch in interface android.view.View.OnTouchListener