Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Swipe for position in RecyclerView using ItemTouchHelper.SimpleCallback

I am using recyclerview 22.2.0 and the helper class ItemTouchHelper.SimpleCallback to enable swipe-to-dismiss option to my list. But as I have a type of header on it, I need to disable the swipe behavior for the first position of the adapter. As RecyclerView.Adapter doesn't have a isEnabled() method, I tried to disable the view interaction through the methods isEnabled() and isFocusable() in the ViewHolder creation itself, but had no success. I tried to adjust the swipe threshold to a full value, like 0f ot 1f in the SimpleCallback's method getSwipeThreshold(), but no success too.

Some fragments of my code to help you to help me.

My Activity:

@Override protected void onCreate(Bundle bundle) {     //... initialization     ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0,             ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {          @Override         public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,                           RecyclerView.ViewHolder target) {             return false;         }          @Override         public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {             if (viewHolder instanceof CartAdapter.MyViewHolder) return 1f;             return super.getSwipeThreshold(viewHolder);         }          @Override         public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {          }     };      ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);     itemTouchHelper.attachToRecyclerView(recyclerView); } 

And I have a common adapter with two view types. In the ViewHolder that I want to disable swiping, I did:

public static class MyViewHolder extends RecyclerView.ViewHolder {     public ViewGroup mContainer;      public MyViewHolder(View v) {         super(v);         v.setFocusable(false);         v.setEnabled(false);         mContainer = (ViewGroup) v.findViewById(R.id.container);           } } 
like image 358
Rafael Toledo Avatar asked Jun 08 '15 15:06

Rafael Toledo


People also ask

How to Disable Swipe in RecyclerView?

If you want to disable swiping conditional upon the data in the item, use the position value to get data from the adapter for the item being swiped and disable accordingly. If you already have specific holder types which need to not swipe, the accepted answer will work.

How do you stop swiping the item in recycler view after reaching some distance?

Replace this line in the onChildDraw: super. onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); float newDx = dX; if (newDx >= 100f) { newDx = 100f } super.

What is viewType in onCreateViewHolder?

onCreateViewHolder(parent: ViewGroup, viewType: Int) Parent is the ViewGroup into which the new View will be added after it is bound to an adapter position and viewType is the type of the new View. This method will return a new ViewHolder that holds a View of the given view type.


1 Answers

After playing a bit, I managed that SimpleCallback has a method called getSwipeDirs(). As I have a specific ViewHolder for the not swipable position, I can make use of instanceof to avoid the swipe. If that's not your case, you can perform this control using the position of ViewHolder in the Adapter.

Java

@Override public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {     if (viewHolder instanceof CartAdapter.MyViewHolder) return 0;     return super.getSwipeDirs(recyclerView, viewHolder); } 

Kotlin

override fun getSwipeDirs (recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {     if (viewHolder is CartAdapter.MyViewHolder) return 0     return super.getSwipeDirs(recyclerView, viewHolder) } 
like image 136
Rafael Toledo Avatar answered Oct 26 '22 23:10

Rafael Toledo