如何检测Recyclerview项目是否被刷卡? [英] How to detect if Recyclerview item is being swiped?

查看:55
本文介绍了如何检测Recyclerview项目是否被刷卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加滑动功能,以便在长按RecyclerView项目时删除并显示底页弹出窗口.我正在使用ItemTouchHelper.SimpleCallback进行滑动删除并使用ItemTouchListener进行显示,长按该项目会弹出窗口. 问题是,当我滑动该项目以删除它时,还会检测到长按. 我想要的是在滑动项目时应该忽略长按. 我有ItemTouchHelper类,该类扩展了Simplecallback以便将其删除.以下是附加recyclerview以便滑动删除的代码.

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是为长时间单击事件添加侦听器的代码.

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

推荐答案

正如@DavidVelasquez所建议的那样,您应在滑动开始时设置一个标志,并根据您onLongClick()中的状态进行操作,但onSwiped()并非如此去.相反,您应该使用ItemTouchHelper.SimpleCallback#onChildDraw()方法检测滑动的时间,并使用onSwiped()方法检测滑动的时间.

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.

例如.

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

然后只需在您的onLongClick()中检查此标志

And then just check this flag in your onLongClick()

这篇关于如何检测Recyclerview项目是否被刷卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆