1

I have created a simple RecyclerView and i am using the below Swipe Listener:

//swipe items
            new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

                @Override
                public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                    //do nothing, we only care about swiping
                    return false;
                }

                @Override
                public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
                    if(swipeDir == ItemTouchHelper.RIGHT){
                        Toast.makeText(getContext(), "Swiped right", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getContext(), "Swiped left", Toast.LENGTH_SHORT).show();
                    }

                }
            }).attachToRecyclerView(recyclerView);

I want to allow only right swipe and prevent left swipe. So when the user tries to swipe left i want the item NOT to disappear.

How can i do that?

mike_x_
  • 1,900
  • 3
  • 35
  • 69
  • 2
    Have you tried removing `ItemTouchHelper.LEFT` from `new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)`? So that it is `new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT)` – SteveEdson Jan 05 '17 at 13:18
  • http://stackoverflow.com/questions/30713121/disable-swipe-for-position-in-recyclerview-using-itemtouchhelper-simplecallback – Amit Vaghela Jan 05 '17 at 13:22

1 Answers1

4

If you want to enable only some swipe/drag movements you need to override the method @Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) of ItemTouchHelper.Callback (superclass of ItemTouchHelper.SimpleCallback)

For example if you want to enable only right swipe:

@Override public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    int dragFlags = 0;
    int swipeFlags = ItemTouchHelper.RIGHT;

    return makeMovementFlags(dragFlags, swipeFlags);
}
Marco Righini
  • 506
  • 2
  • 12