滑动以closuresRecyclerView

我曾经SwipeToDismiss库,但现在我试图迁移到RecyclerView,事情并不那么明显,你知道这个库的任何替代? 任何想法如何从零开始实施它?

从v22.2.0开始,Android支持团队已经包含了一个ItemTouchHelper类,可以轻松刷卡和拖放。 这可能不像一些图书馆那样function全面,但是它直接来自Android团队。

  • 更新你的build.gradle来导入RecyclerView库的v22.2。+

     compile 'com.android.support:recyclerview-v7:22.2.+' 
  • 使用适当的SimpleCallback实例化一个ItemTouchHelper

     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接受您希望启用拖放的方向以及要启用滑动的方向。

  • 附加到您的RecyclerView

     itemTouchHelper.attachToRecyclerView(recyclerView); 
  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 

在代码中,如果用户向左滑动,则显示AlertDialog,如果用户selectREMOVE,则从数据库中删除项目,并刷新recyclerview,如果用户select取消,则回收站视图将保持原样。

也许你可以试试这个库:

https://github.com/daimajia/AndroidSwipeLayout

更新:我刚刚发现了另一个好的库,你可以使用RecyclerView:

https://github.com/hudomju/android-swipe-to-dismiss-undo

这个库可能会有所帮助。您可以在OnDissmiss使用supertoast实现undo

我写了SwipeToDeleteRV库,它支持回收器视图上的刷卡 – 删除 – 撤消function。 它基于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属性都是可选的。 如果你不指定它们,将使用默认值。

然后创build一个适配器,该类适配器STDAdapter的子类,请确保您调用超类的构造函数。 像这样的东西:

 public class SampleAdapter extends STDAdapter<String> { public SampleAdapter(List<String> versionList) { super(versionList); } 

}

接下来,请确保您调用了setupSwipeToDelete方法来设置滑动删除function。

 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); 

而已! 对于更高级的设置(例如,为不同的项目设置不同的删除信息,临时和永久删除项目,…)请参考项目自述页面。