简体   繁体   中英

Snackbar from custom class not showing

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM