0

MyActivity coord.xml:

<CoordinatorLayout>
   id:coordID
   <RelativeL>
      ...
   <FAB>

RVAdapter.java

...
@Override
    public void onItemDismiss(int position) {
        ...
        notifyItemRemoved(position);

        LayoutInflater inflater = (LayoutInflater) MyActivity.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.coord, null);

        Snackbar.make(v.findViewById(R.id.coordID), "TEXT", Snackbar.LENGTH_LONG).show();
    }

This is a method for swiping to delete an item from recycler view list, in a custom class (RVAdapter.java) and after its deleted, Snackbar should appear and set action for UNDO. But, when I delete an item, nothing happends. Snackbar doesnt show up. Im not so sure if I set views correctly, but I dont know how to do it differently

Nemanja
  • 211
  • 6
  • 16
  • use this Snackbar.make(v, "TEXT", Snackbar.LENGTH_LONG).show(); –  Jun 23 '18 at 15:34
  • 1
    That `View v` you inflate right there isn't attached to anything on-screen, so the `Snackbar` isn't going find a proper `View` to anchor to. You need to pass it some `View` that's in the `Activity`'s hierarchy. It can be anything, really; e.g., the `RecyclerView`. `Snackbar` will search up to find the `CoordinatorLayout` itself. – Mike M. Jun 24 '18 at 00:55
  • @MikeM. Im I not passing view with View v = inflater...??? There is a 2nd and third argument in that method, but for 2nd I need a ViewGroup and I dont know where to get it? – Nemanja Jun 24 '18 at 08:53
  • 1
    No, I'm saying that the `LayoutInflater` and `View v` there are useless, since it's never attached to the `Activity`. Get rid of those, and pass `Snackbar.make()` some `View` that is already in the `Activity`. I simply suggested the `RecyclerView`, since it's about the only `View` I'm sure that you have. – Mike M. Jun 24 '18 at 09:04

2 Answers2

1

Thx to Mike M. I solved it like this:
In MyActivity added

private static CoordinatorLayout mCoord;
mCoord = findViewById(R.id.coordID);
public static View getCoord() { return mCoord; }

and in RVAdapter.java

Snackbar.make(MyActivity.getCoord(), ...
Nemanja
  • 211
  • 6
  • 16
0

In my opinion, I think there are two possible reasons for not showing snackbar.

  1. Elevation of other views is too high for snackbar to be displayed. Check elevation of all the views in the activity.
  2. Reference of your activity that you are providing as parameter view of Snackbar.make() might not be proper. If this is the case then I will suggest you pass a reference of activity to Adapter via the constructor and then storing it as follow.

    public class MoviesAdapter extends RecyclerView.Adapter<MyViewHolder> {
    
        private List<Movie> moviesList;
        private MainActivity activity;
    
        MoviesAdapter(MainActivity activity){
            super();
            this.activity = activity;
        }
    
        //continue your remaining work from here
    
    }
    

I would have liked to comment and ask for more details but because of my low reputation I wasn't allowed to do so. Hope these suggestions work for you.

Mandar Sadye
  • 689
  • 2
  • 9
  • 30
  • 1. is not the case 2. I tried that and then I used `Snackbar.make(activity.findViewById(R.id.coordID), ...` and I recevived error that Im tried to read field `activity.findViewById(R.id.coordID)` on a null object reference!? – Nemanja Jun 24 '18 at 08:51