我曾经使用SwipeToDismiss库,但现在我正在尝试迁移到RecyclerView,事情并不那么明显,你知道这个lib的替代品吗?任何想法如何从头开始实现它?
答案 0 :(得分:330)
从v22.2.0开始,Android支持团队已经包含了一个ItemTouchHelper
类,可以轻松进行轻扫和拖放操作。这可能不像其他一些库那样功能齐全,但它直接来自Android团队。
更新build.gradle以导入RecyclerView库的v22.2。+
compile 'com.android.support:recyclerview-v7:22.2.+'
使用适当的SimpleCallback
实例化ItemTouchHelperItemTouchHelper.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会引导您要启用拖放的路线以及要启用滑动的路线。
附加到您的RecyclerView
itemTouchHelper.attachToRecyclerView(recyclerView);
答案 1 :(得分:29)
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction) {
final int position = viewHolder.getAdapterPosition(); //get position which is swipe
if (direction == ItemTouchHelper.LEFT) { //if swipe left
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //alert for confirm to delete
builder.setMessage("Are you sure to delete?"); //set message
builder.setPositiveButton("REMOVE", new DialogInterface.OnClickListener() { //when click on DELETE
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.notifyItemRemoved(position); //item removed from recylcerview
sqldatabase.execSQL("delete from " + TABLE_NAME + " where _id='" + (position + 1) + "'"); //query for delete
list.remove(position); //then remove item
return;
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { //not removing items if cancel is done
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.notifyItemRemoved(position + 1); //notifies the RecyclerView Adapter that data in adapter has been removed at a particular position.
adapter.notifyItemRangeChanged(position, adapter.getItemCount()); //notifies the RecyclerView Adapter that positions of element in adapter has been changed from position(removed element index to end of list), please update it.
return;
}
}).show(); //show alert dialog
}
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recyclerView); //set swipe to recylcerview
如果用户向左滑动则显示在Code中,然后显示AlertDialog,如果用户选择REMOVE,则会从数据库中删除item并刷新recyclelerview,如果用户选择CANCEL,则回收视图就是原样。
答案 2 :(得分:14)
答案 3 :(得分:2)
This library可能会有帮助。您可以在undo
使用supertoast
OnDissmiss
答案 4 :(得分:2)
我写了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);
就是这样!对于更高级的设置(即,为不同的项目设置不同的删除消息,暂时和永久删除项目,......)请参阅项目自述页面。