Reputation: 193
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();
}
}));
Upvotes: 4
Views: 5337
Reputation: 476
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.
Upvotes: 3
Reputation: 392
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()
Upvotes: 2