Snackbar from custom class not showing

▼魔方 西西 提交于 2020-07-10 06:43:47

问题


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


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



回答2:


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.



来源:https://stackoverflow.com/questions/51002457/snackbar-from-custom-class-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!