Mohammed Atif Mohammed Atif - 3 months ago 10
Android Question

How to control swipe to dismiss for individual list item in RecyclerView

I am using two

ItemTouchHelper
objects, one for LEFT swiping and corresponding operations and another for RIGHT swiping and corresponding operations. In my list, few items must have only left swiping enabled and few must have only right swiping enabled.

But the problem is if I attach my
RecyclerView
to both the Helper objects, then each object is swipe-able on both the direction.

Is there any method to control individual list items for Swipe to Dismiss in ItemTouchListener?


P.S. not interested in external libraries

Answer

Use ItemTouchHelper.Callback.getMovementFlags(...) (or alternatively ItemTouchHelper.SimpleCallback.getSwipeDirs(...)). It gets the ViewHolder as parameter.

Set a flag on the ViewHolder in RecyclerView.Adapter.onBindViewHolder(...) if the represented item can be swiped left or right or none. Query that flag in the above mentioned method(s) and return an according value.

Comments