I am implementing Swipe and Drag in RecyclerView
through the help of this article. On removing the item I want to show a SnackBar
but showing SnackBar
needs View. I don't know how to get View
inside the function of a RecyclerAdapter
.
My Code:
public void onItemDismiss(int position) {
notes.remove(position);
notifyItemRemoved(position);
/* Show SnackBar */
}
Edit: My question is different from this question. I am not having any problem in implementing SwipetoDismiss.I have successfully implemented it. But I want to show a SnackBar so that user can be notfied and Undo. I am having problem in showing SnackBar not in implementing onSwiped().
Snackbar snackbar1 = Snackbar. make(view, "Image Deleted!",Snackbar. LENGTH_SHORT);
A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.
Modify your method and pass RecyclerView
as Parameter so you will get view
public void onItemDismiss(int position,RecyclerView rv) {
notes.remove(position);
notifyItemRemoved(position);
/* Show SnackBar */
Snackbar.make(rv, R.string.snackbar_text, Snackbar.LENGTH_LONG).show();
}
EDIT
private final ItemTouchHelperAdapter mAdapter;
private final RecyclerView rv;
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter,RecyclerView rv) {
mAdapter = adapter;
this.rv=rv;
}
And then pass
@Override
public void onSwiped(ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition(),rv);
}
you can use Android's content view by typecasting your context
Activity activity=(Activity) context;
and use in snackbar to get view
Snackbar.make(activity.findViewById(android.R.id.content),"Hello",BaseTransientBottomBar.LENGTH_LONG).show
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With