How to detect if Recyclerview item is being swiped?

Deepak Gautam :

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();
        }
    }));
Jan Stoltman :

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()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=79747&siteId=1