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?
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);
viewHolder
. –
Maloriemalory 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.
if (direction == ItemTouchHelper.LEFT) // if swipe left
as the ItemTouchHelper.SimpleCallback
is limited to just that swipe direction. If you want left and right swipes then ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
and then you would need to check the direction. –
Detailed AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // stuff to put the item back } });
–
Detailed adapter.notifyItemChanged(position);
brought the swiped item back, rather than notifyItemRemoved
- which is more logical imho. –
Bold 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:
This library may be helpful.You can implement undo
in OnDissmiss
use supertoast
OnTouchListener
inspire by this –
Casket 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"
...
<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"/>
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<String> {
public SampleAdapter(List<String> 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.
© 2022 - 2024 — McMap. All rights reserved.