1

I've got a simple left/right swipe listener for my RecyclerView, so I created the following function in my fragment.

ItemTouchHelper itemTouchHelper;
public void setDeleteUpdateSwipeListener() {
      ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0,
            ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }
        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
           //do stuff
           Log.e("i still", "get called");
        }


    };

    itemTouchHelper = new ItemTouchHelper(simpleCallback);
    itemTouchHelper.attachToRecyclerView(rvListDriver);
}

If the user clicks on an item for longer period of time, I don't want to allow any wipes, so I did this in my adapter:

@Override
public void onBindViewHolder(@NonNull DriverListAdapter.ViewHolder holder, int position) {
    final ViewHolder recyclerViewHolder = holder;
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
          
            drListFrag.setNoSwipeListener();
            return true;
        }
    });
}

And the corresponding function in my fragment:

public void setNoSwipeListener() {
    itemTouchHelper = new ItemTouchHelper(null);
    itemTouchHelper.attachToRecyclerView(null);
}

However, swipes still are happening and the trace Log.e also gets called. I made sure that the function noSwipe gets called. I also tried other solutions but none of them work for me.

May the issue be cause I don't have the swipelistener in my RecycerViewAddapter but rather in my Fragment/RecycerView object.

Zain
  • 37,492
  • 7
  • 60
  • 84
Funny Moments
  • 421
  • 2
  • 13

2 Answers2

1

You can use a custom ItemTouchHelper.Callback and override isItemViewSwipeEnabled() method.

The return value of this method determines whether the swipe is enabled or not. If true then it's enabled.

So, you can have a local boolean field in the customized ItemTouchHelper.Callback with a setter, and toggle its value to false within setNoSwipeListener()

isItemViewSwipeEnabled() should return this local field

And when the touch event is over, then reset this boolean to true:

holder.listItem.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // Reset the boolean to true
        }
        return false;
    }
});
Zain
  • 37,492
  • 7
  • 60
  • 84
0

Try this

public void setNoSwipeListener() {
    // itemTouchHelper = new ItemTouchHelper(null);
    itemTouchHelper.attachToRecyclerView(null);
}
aiqency
  • 1,015
  • 1
  • 8
  • 22
  • I also tried it before. For some reason it only works in the setDeleteUpdateSwipeListener() function. If I put the code in the setNoSwipeListener() function it doesn't work. – Funny Moments Aug 27 '21 at 00:02
  • Not sure I should see the full code. Are you sure your long click is handled. Are you sure you are referencing the correct `itemTouchHelper` and` drListFrag` instance? – aiqency Aug 27 '21 at 00:10
  • yes I am, I always traces the call that are made. I enter the onlonglistener event and also the function where i remove the slider listener (in theory). Idc, maybe I need some fresh air and try it later to see where the issue might be – Funny Moments Aug 27 '21 at 01:19