A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.
- Add these lines to your
build.gradle
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
compile 'com.github.CodeFalling:RecyclerViewSwipeDismiss:v1.1.3'
}- Build
onTouchListenerand bind it to yourRecyclerView
SwipeDismissRecyclerViewTouchListenerlistener = newSwipeDismissRecyclerViewTouchListener.Builder(
recyclerView,
newSwipeDismissRecyclerViewTouchListener.DismissCallbacks() {
@OverridepublicbooleancanDismiss(intposition) {
returntrue;
}
@OverridepublicvoidonDismiss(Viewview) {
// Do what you want when dismiss
}
})
.setIsVertical(false)
.setItemTouchCallback(
newSwipeDismissRecyclerViewTouchListener.OnItemTouchCallBack() {
@OverridepublicvoidonTouch(intindex) {
// Do what you want when item be touched
}
})
.setItemClickCallback(newSwipeDismissRecyclerViewTouchListener.OnItemClickCallBack() {
@OverridepublicvoidonClick(intposition) {
// Do what you want when item be clicked }
})
.setBackgroundId(R.drawable.bg_item_normal, R.drawable.bg_item_selected)
.create();
recyclerView.setOnTouchListener(listener);setIsVertical(false)means allow swipe in horizontal directionlistener.setEnabled(false)can disable swipe to dismissonTouchwill be called when MOUSE_UP on item without swipeonClickwill be called when ACTION_UP on item within 1 second and move no more than a fixed distanceBy use
setBackgroundId, you can set background id for item's normal and pressed state, just like the normal effect in RecyclerView
You can see sample code in sample/MainActivity.java
