我写了SwipeToDeleteRV 库,它支持在回收站视图上滑动到删除-撤消功能。它基于 ItemTouchHelper 并且非常易于使用。
希望对面临同样问题的人有所帮助。
例如,您可以在 XML 布局中正常定义回收站视图,以及一些可选属性:
...
xmlns:stdrv="http://schemas.android.com/apk/res-auto"
...
<io.huannguyen.swipetodeleterv.STDRecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
stdrv:border_color="@android:color/darker_gray" // specify things like border color, border width, etc.
stdrv:delete_view_background="#cccccc"
stdrv:delete_icon="@drawable/ic_archive"
stdrv:delete_icon_height="24dp"
stdrv:delete_icon_width="24dp"
stdrv:left_delete_icon_margin="32dp"
stdrv:delete_message="@string/delete_message"
stdrv:right_delete_icon_margin="32dp"
stdrv:delete_icon_color="#000000"
stdrv:has_border="true"/>
所有 stdrv 属性都是可选的。如果您不指定它们,将使用默认值。
然后创建一个子类 STDAdapter 的适配器,确保调用超类构造函数。像这样的:
public class SampleAdapter extends STDAdapter<String> {
public SampleAdapter(List<String> versionList) {
super(versionList);
}
}
接下来确保调用setupSwipeToDelete 方法来设置滑动删除功能。
mRecyclerView.setupSwipeToDelete(your_adapter_instance, swipe_directions);
swipe_directions 是您允许滑动项目的方向。
例子:
// Get your recycler view from the XML layout
mRecyclerView = (STDRecyclerView) findViewById(R.id.recycler_view);
LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new SampleAdapter(versions);
// allow swiping in both directions (left-to-right and right-to-left)
mRecyclerView.setupSwipeToDelete(mAdapter, ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
就是这样!更高级的设置(例如,为不同的项目设置不同的删除消息,临时和永久删除项目,...)请参考项目自述页面。