Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get View in RecyclerView.Adapter for Snackbar?

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().

like image 340
Mmohits Avatar asked Jul 07 '15 03:07

Mmohits


People also ask

How do I call a snackbar in adapter?

Snackbar snackbar1 = Snackbar. make(view, "Image Deleted!",Snackbar. LENGTH_SHORT);

What is Item view in RecyclerView?

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.


2 Answers

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);
    }
like image 125
N J Avatar answered Oct 08 '22 17:10

N J


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
like image 32
Anand Kumar Avatar answered Oct 08 '22 17:10

Anand Kumar