SwipeableRecyclerView provides a wrapper class SwipeItemTouchHelperCallback extends ItemTouchHelper.Callback
which can be used to add Swiping capability to your RecyclerView items. You can make use of DataBinding
to bind it via XML.
ItemTouchHelper.Callback swipeCallback = new SwipeItemTouchHelperCallback
.Builder(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
.bgColorSwipeLeft(bgColorSwipeLeft)
.bgColorSwipeRight(bgColorSwipeRight)
.drawableSwipeLeft(drawableSwipeLeft)
.drawableSwipeRight(drawableSwipeRight)
.setSwipeEnabled(swipeEnabled)
.onItemSwipeLeftListener(onItemSwipeLeft)
.onItemSwipeRightListener(onItemSwipeRight)
.build();
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(swipeCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
In your Gradle
dataBinding {
enabled = true
}
In your BindingAdapter
/**
* Bind ItemTouchHelper.SimpleCallback with RecyclerView
*
* @param recyclerView RecyclerView to bind to SwipeItemTouchHelperCallback
* @param swipeEnabled enable/disable swipe
* @param drawableSwipeLeft drawable shown when swiped left
* @param drawableSwipeRight drawable shown when swiped right
* @param bgColorSwipeLeft background color when swiped left
* @param bgColorSwipeRight background color when swiped right
* @param onItemSwipeLeft OnItemSwipeListener for swiped left
* @param onItemSwipeRight OnItemSwipeListener for swiped right
*/
@android.databinding.BindingAdapter(value = {"swipeEnabled", "drawableSwipeLeft", "drawableSwipeRight", "bgColorSwipeLeft", "bgColorSwipeRight", "onItemSwipeLeft", "onItemSwipeRight"}, requireAll = false)
public static void setItemSwipeToRecyclerView(RecyclerView recyclerView, boolean swipeEnabled, Drawable drawableSwipeLeft, Drawable drawableSwipeRight, int bgColorSwipeLeft, int bgColorSwipeRight,
SwipeItemTouchHelperCallback.OnItemSwipeListener onItemSwipeLeft, SwipeItemTouchHelperCallback.OnItemSwipeListener onItemSwipeRight) {
... // attach RecyclerView to SwipeItemTouchHelperCallback as above
}
In your XML
file
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
bind:bgColorSwipeLeft="@{@color/app_green}"
bind:bgColorSwipeRight="@{@color/app_red}"
bind:drawableSwipeLeft="@{@drawable/ic_check_white_24dp}"
bind:drawableSwipeRight="@{@drawable/ic_close_white_24dp}"
bind:onItemSwipeLeft="@{(position) -> handler.onItemSwipedLeft(position)}"
bind:onItemSwipeRight="@{(position) -> handler.onItemSwipedRight(position)}"
bind:swipeEnabled="@{true}"/>
Add Android Support Design dependency to your gradle file.
dependencies {
compile 'com.android.support:design:{latest_version}'
}