简体   繁体   English

从 RecyclerView 中分离 ItemTouchHelper

[英]Detach ItemTouchHelper from RecyclerView

I have a RecyclerView with a working ItemTouchHelper .我有一个RecyclerView和一个有效的ItemTouchHelper Everything works great, but I am wondering if there is a way I can detach the ItemTouchHelper from the RecyclerView without re-creating the list?一切都很好,但我想知道是否有一种方法可以将ItemTouchHelperRecyclerView分离出来,而无需重新创建列表? For fun, this is the code I'm using to attach:为了好玩,这是我用来附加的代码:

ItemTouchHelper.Callback callback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
{
    ...
};
mItemTouchHelper = new ItemTouchHelper(callback);
mItemTouchHelper.attachToRecyclerView(mPasswordList);

Ideally, I'd like to check a preference in say onResume() of the Activity this RecyclerView lives in and detach the ItemTouchHelper based on that.理想情况下,我想检查这个RecyclerView所在的 Activity 的onResume()中的首选项,并基于此分离ItemTouchHelper

您可以通过将 recyclerview 设置为 null 来从 RecyclerView 分离 ItemTouchHelper:

mItemTouchHelper.attachToRecyclerView(null);

My original motivation for this was to allow the user the ability to disable swipe actions on list items if they so choose.我最初的动机是让用户能够在他们选择的情况下禁用列表项上的滑动操作。 I assumed the way to do this was to detach the ItemTouchHelper from the RecyclerView .我认为这样做的方法是将ItemTouchHelperRecyclerView分离。 I have now found the ItemTouchHelper.SimpleCallback has the following method available to override:我现在发现ItemTouchHelper.SimpleCallback有以下方法可以覆盖:

@Override
public boolean isItemViewSwipeEnabled()
{
    return mSwipable;
}

So, returning the correct state here effectively turns off the swipe handling.因此,在这里返回正确的状态可以有效地关闭滑动处理。 I hope this helps someone in the future.我希望这对未来的人有所帮助。

For me, to disable the entire ItemTouchHelper that attached to the RecyclerView just set it to null对我来说,要禁用附加到RecyclerView的整个ItemTouchHelper只需将其设置为null

mItemTouchHelper = null;

And to re-enable, set it to its value并重新启用,将其设置为它的值

mItemTouchHelper = new ItemTouchHelper(callback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM