滑动到回收视图解散

我曾经使用过 SwipeToDismiss库,但是现在我正在尝试迁移到 RevicleView,事情并不是那么明显,你知道有什么可以替代这个库吗?有什么办法可以从头开始实现它吗?

83526 次浏览

maybe you could try this library:

Https://github.com/daimajia/androidswipelayout

更新: 我刚刚发现了另一个可以用回收视图使用的好库:

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

这个库 可能会有帮助,您可以使用 < a href = “ https://github.com/JohnPersonano/Supertoast/”rel = “ nofollow”> supertoastOnDissmiss中实现 undo

从22.2.0版本开始,Android 支持团队已经包含了一个 ItemTouchHelper类,它使得滑动删除和拖放变得非常简单。这可能不像其他一些库那样功能齐全,但它直接来自 Android 团队。

  • Update your build.gradle to import v22.2.+ of the RecyclerView library

    compile 'com.android.support:recyclerview-v7:22.2.+'
    
  • 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 接受您想要启用拖放的方向和您想要启用滑动的方向。

  • 附加到回收视图

    itemTouchHelper.attachToRecyclerView(recyclerView);
    

我编写了 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);

就是这样!对于更高级的设置(即,为不同的项目设置不同的删除消息,临时和永久删除项目,...)请参考项目自述页面。

 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 将显示,如果用户选择 REMOVE,那么项目将从数据库中删除,回收视图将刷新,如果用户选择 CANCEL,那么回收视图将按原样进行。