简体   繁体   中英

Detach ItemTouchHelper from RecyclerView

I have a RecyclerView with a working 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? 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 设置为 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 . I have now found the ItemTouchHelper.SimpleCallback has the following method available to override:

@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

mItemTouchHelper = null;

And to re-enable, set it to its value

mItemTouchHelper = new ItemTouchHelper(callback);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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