4

I am trying to add functionality of swipe to delete as well as to show bottom sheet pop-up if RecyclerView item is long pressed. I am using ItemTouchHelper.SimpleCallback for swipe to delete and ItemTouchListener for showing pop-up on long press of item. Problem is that when I am swiping the item to delete its also detecting long press. What I want is that it should ignore long press when item is being swiped. I have ItemTouchHelper class which extends Simplecallback for swipe to delete. Following is the code to attach recyclerview for swipe to delete.

 ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new RecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, this);
    new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);

Follwing is code to add listener for long click event.

 recyclerView.addOnItemTouchListener(new NotesRecyclerTouchListener(getApplicationContext(), recyclerView, new NotesRecyclerTouchListener.ClickListener() {
        @Override
        public void onLongClick(View view, int position) {
                Note note = notesList.get(position);
                Toast.makeText(getApplicationContext(), note.getTitle() + " is log pressed!", Toast.LENGTH_SHORT).show();
                View sheetView = MainActivity.this.getLayoutInflater().inflate(R.layout.view_bottom_sheet_dialog, null);
                BottomSheetDialog dialog = new BottomSheetDialog(MainActivity.this);
                dialog.setContentView(sheetView);
                dialog.show();
        }
    }));
mark922
  • 1,136
  • 2
  • 11
  • 20
Deepak Gautam
  • 185
  • 4
  • 11
  • What about adding a flag? When `onSwiped()` event is called, set a `swiped` flag to `true`. Then if the long click event is also fired, check the flag and if it's true, then it's during a swipe event, so do not execute the code in the long click, just exit the method. When the swipe event completes, reset the flag to false. – David Velasquez Jul 31 '18 at 16:10
  • onSwiped() will be called after swiping is finished but I need to check if swiping is undergoing. – Deepak Gautam Jul 31 '18 at 16:17
  • Oops, I meant `onChildDraw()` as suggested in the below answer :) – David Velasquez Jul 31 '18 at 18:45

2 Answers2

3

onSwiped will be called when item will be completely swiped. If you will start to swipe element but then move it back, the method will not be called. So you shouldn't use this method to mark the end of swiping. You can use isCurrentlyActive from onChildDraw like this:

var swiping: Boolean = false

override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        swiping = isCurrentlyActive
    }
}

It will be true when you move element and false after swipe or cancelation of swipe.

Wojtek Okoński
  • 448
  • 1
  • 4
  • 12
2

As @DavidVelasquez has suggested you should set up a flag when swipe begins and act depending on it's state in your onLongClick() But onSwiped() is not the way to go. Instead you should use ItemTouchHelper.SimpleCallback#onChildDraw() method to detect when the swipe beings and onSwiped() method to detect when it ends.

Eg.

override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int,isCurrentlyActive: Boolean) {
    if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
        setupMyFlag()
    }
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
    clearMyFlag()
}

And then just check this flag in your onLongClick()

Jan Stoltman
  • 394
  • 3
  • 15
  • 1
    I did that in slightly different way. As my recyclerview is in MainActivity so I will not have access to childDraw() so I declare an abstract method in ItemTouchHelper class and implement that method in mainActivity to get value of dX that is swipe displacement and then I used inside onLongClick() function if(dX <0){//do something on doubletap}. Thanks your answer has helped. – Deepak Gautam Aug 01 '18 at 12:19
  • @Jan Stoltman This is not working both the methods are called onChildDraw() and also onSwiped. Any other solution? – rajlaxmi_jagdale Mar 07 '19 at 12:10