Swipe to Dismiss for RecyclerView

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

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?

回答1:

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.

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

    ** 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.

  • Attach to your RecyclerView

    itemTouchHelper.attachToRecyclerView(recyclerView); 


回答2:

 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 

Here in Code if user swipe left then AlertDialog is displayed and if user select REMOVE then item is deleted from database and recyclerview is refreshed and if user select CANCEL then recyclerview is as it is.



回答3:

maybe you could try this library:

https://github.com/daimajia/AndroidSwipeLayout

Update: I've just found another good library that you can use with RecyclerView:

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



回答4:

This library may be helpful.You can implement undo in OnDissmiss use supertoast



回答5:

I wrote SwipeToDeleteRV library which supports swipe-to-delete-undo feature on recycler views. It is based on ItemTouchHelper and very easy to use.

Hope it may be helpful for someone facing the same issues.

As an example, you can define your recycler view in an XML layout as normal, plus some optional attributes:

... xmlns:stdrv="http://schemas.android.com/apk/res-auto" ... 

All stdrv attributes are optional. If you don't specify them, the default ones would be used.

Then create an adapter that subclasses STDAdapter, make sure you call the super class constructor. Something like this:

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

}

Next make sure you make a call to the setupSwipeToDelete method to set the swipe-to-delete feature up.

mRecyclerView.setupSwipeToDelete(your_adapter_instance, swipe_directions); 

swipe_directions is the direction you allow items to be swiped.

Example:

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

That's it! For more advanced settings (i.e., set different deletion messages for different items, temporarily and permanently remove items,...) please refer to the project readme page.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!