How to detect if Recyclerview item is being swiped?

隐身守侯 提交于 2019-12-14 01:28:02

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/51617569/how-to-detect-if-recyclerview-item-is-being-swiped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!