问题
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.
- Elevation of other views is too high for snackbar to be displayed. Check elevation of all the views in the activity.
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