16

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

Community
  • 1
  • 1
Mmohits
  • 201
  • 1
  • 2
  • 10

2 Answers2

9

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);
    }
Ken Ratanachai S.
  • 3,307
  • 34
  • 43
N J
  • 27,217
  • 13
  • 76
  • 96
  • Shows error on 'onSwiped' method on 'SimpleItemTouchHelperCallback' – Mmohits Jul 07 '15 at 03:32
  • onItemDismiss (int, RecyclerView) in ItemTouchHelper cannot be applied to (int) – Mmohits Jul 07 '15 at 03:38
  • In onSwiped() ViewHolder is passed as parameter. Is there a way to get View through ViewHolder? – Mmohits Jul 07 '15 at 03:42
  • can you post code where you are calling `onSwiped()`, you must pass position and rv as parameter – N J Jul 07 '15 at 03:43
  • I am calling it in SimpleItemTouchHelperCallback. You can see it in the article. My implementation is same. – Mmohits Jul 07 '15 at 03:47
  • Here is the link : https://gist.github.com/iPaulPro/79fa9c8ed00f64af690b#file-simpleitemtouchhelpercallback-java – Mmohits Jul 07 '15 at 03:48
  • You must call like this ` mAdapter.onItemDismiss(viewHolder.getAdapterPosition(), recyclerview );` – N J Jul 07 '15 at 03:50
  • Have already done that but in onSwiped(), there is no parameter of RecylerView. – Mmohits Jul 07 '15 at 03:52
  • You must pass from you activity where you define adapter and recyclerview , where you assigned adapter to recyclerview. – N J Jul 07 '15 at 03:54
  • you can modify you constructor as ` public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter,RecyclerVIew rv) { mAdapter = adapter; }` – N J Jul 07 '15 at 04:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82557/discussion-between-nilesh-and-mmohits). – N J Jul 07 '15 at 05:19
1

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
  • Why do you prefer this approach over the accepted answer? Are there benefits to this approach? Does it take advantage of new syntax, or solve any particular limitations? – Jeremy Caney Jul 01 '21 at 00:42
  • This is just an alternative method for getting view in RecylerView.Adapter . – Anand Kumar Jul 16 '21 at 06:29