问题
Hi I am trying to create a layout like this using support library https://developer.android.com/reference/android/support/v7/widget/CardView.html but i didn't found a way to add overflow icon in a card.
I don't want to use this library https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARDGRID.md. I want to do same using support library that Google introduced recently.
Isn't there is way to achieve using support library or I have to use gabrielemariotti library to add overflow items in a card view.
Update
Guys I have edited question now it is more clear what i want.
回答1:
First of all you shouldn't use this lib only to achieve an overflow menu inside a card.
Said that,the CardView in support Library is a FrameLayout with any model behind.
The best way to achieve it, now is to use the new Toolbar
inside the CardView layout.
Also you can add a ImageView
inside the CardView
layout and do something like this:
PopupMenu popup = new PopupMenu(getContext(), mImageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.your_menu, popup.getMenu());
Finally (but it is not so important) if it isn't enough, the new cardslib (coming soon) will use the CardView in support library.
回答2:
Here's how to do it correctly in an android.support.v7.widget.CardView
. In my example, it's a cardview inside of a RecyclerView, so I set this up in the adapter, inside the onBindViewHolder method:
public class DashboardItemAdapter extends RecyclerView.Adapter<DashboardItemAdapter.ViewHolder> {
ArrayList<Item> mItems = new ArrayList<>();
private Context mContext;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_dashboard, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder vh, int position) {
final Item item = mItems.get(position);
...
PopupMenu popup = new PopupMenu(mContext, vh.btnMenu);
popup.inflate(R.menu.dashboard_card_waiting);
popup.setOnMenuItemClickListener(item -> {
switch (item.getItemId()) {
case R.id.action_cancel:
cancelItem();
return true;
default:
return true;
}
});
vh.btnMenu.setOnClickListener(btn -> popup.show());
}
...
}
来源:https://stackoverflow.com/questions/26628652/how-to-add-overflow-icon-items-in-a-card-in-android