滑动以关闭 RecyclerView [英] Swipe to Dismiss for RecyclerView
问题描述
我曾经使用 SwipeToDismiss 库,但现在我正在尝试迁移到 RecyclerView 并且事情不是那么明显,你知道吗这个库的任何替代品?任何想法如何从头开始实施它?
I used to SwipeToDismiss library but now I'm trying to migrate to RecyclerView and things are not so obvious, do you know any replacements for this lib? Any ideas how to implement it from the scratch?
推荐答案
从 v22.2.0 开始,Android 支持团队已经包含了一个 ItemTouchHelper
类,它可以让滑动关闭和拖拽-drop 很简单.这可能不像某些库那样功能齐全,但它直接来自 Android 团队.
As of v22.2.0, the Android support team has included an ItemTouchHelper
class that makes swipe-to-dismiss and drag-and-drop pretty simple. This may not be as full-featured as some of the libraries out there, but it comes directly from the Android team.
更新您的 build.gradle 以导入 RecyclerView 库的 v22.2.+
Update your build.gradle to import v22.2.+ of the RecyclerView library
compile 'com.android.support:recyclerview-v7:22.2.+'
使用适当的 SimpleCallback 实例化 ItemTouchHelper
Instantiate an ItemTouchHelper with an appropriate SimpleCallback
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
[...]
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
//Remove swiped item from list and notify the RecyclerView
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
** 请注意,SimpleCallback 接受要启用拖放的方向和要启用滑动的方向.
** Note that the SimpleCallback takes in the directions that you want to enable drag-and-drop and the directions that you want to enable swiping.
附加到您的 RecyclerView
Attach to your RecyclerView
itemTouchHelper.attachToRecyclerView(recyclerView);
这篇关于滑动以关闭 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!