PopupMenu 不会弹出 onclick

PopupMenu doesnt pop up onclick

我有这个 ImageButton,它在我的 RecyclerviewItems 内部,通常应该调用 method showPopup onclick。 我的计划是当这个 Imagebutton 被点击时会有一个 PopupMenu,您可以从中选择重命名项目或删除它。 但不幸的是,事情并没有按预期进行。 感谢您的帮助。

<ImageButton
            android:id="@+id/img_btn_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/white"
            android:src="@drawable/ic_more"
            android:onClick="showPopup"/>
public void showPopup(View v){
        PopupMenu popup=new PopupMenu(MyApplication.getAppContext(),v);
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.item1: Toast.makeText(MyApplication.getAppContext()," Random text1",Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.item2: Toast.makeText(MyApplication.getAppContext()," Random text2",Toast.LENGTH_SHORT).show();
                        return true;
                    default: return false;
                }
            }
        });
        popup.inflate(R.menu.popup_menu);
        popup.show();
    }

如果您在 RecyclerView 中,则必须在 ViewHolder 并传递构建 RecyclerView 适配器时获得的上下文

holder.*your_image_view*.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
        PopupMenu popup=new PopupMenu(mContext,v);
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.item1: Toast.makeText(mContext," Random text1",Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.item2: Toast.makeText(mContext," Random text2",Toast.LENGTH_SHORT).show();
                    return true;
                default: return false;
            }
        }
    });
    popup.inflate(R.menu.popup_menu);
    popup.show();
    }
});