簡體   English   中英

如何在Android中的卡中添加溢出圖標項

[英]How to add overflow icon items in a card in Android

嗨,我正在嘗試使用支持庫https://developer.android.com/reference/android/support/v7/widget/CardView.html創建這樣的布局,但我沒有找到一種方法來添加溢出圖標卡。

我不想使用這個庫https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARDGRID.md 我想使用Google最近推出的支持庫來做同樣的事情。

是不是有辦法實現使用支持庫或我必須使用gabrielemariotti庫在卡片視圖中添加溢出項目。

在此輸入圖像描述

更新

伙計們我現在編輯了問題,我想要的更清楚。

首先, 你不應該 使用這個lib 來實現卡內的溢出菜單。

據說,支持庫中的CardView是一個FrameLayout ,后面有任何模型。 實現它的最佳方法是使用CardView布局中的新Toolbar

您還可以在CardView布局中添加ImageView ,並執行以下操作:

PopupMenu popup = new PopupMenu(getContext(), mImageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.your_menu, popup.getMenu());

最后(但不是那么重要)如果還不夠,新的cardlib(即將推出)將在支持庫中使用CardView。

以下是如何在android.support.v7.widget.CardView正確執行此操作。 在我的例子中,它是RecyclerView內部的cardview,所以我在適配器中,在onBindViewHolder方法中設置它:

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());
    }

    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM