简体   繁体   English

当我单击上下文操作栏上的项目时如何插入菜单

[英]How to insert a menu when i click in a item on the contextual action bar

I am using Xamarin Android in my apllication. 我在应用程序中使用Xamarin Android。 I created RecyclerView, and I created a contextual action bar in it. 我创建了RecyclerView,并在其中创建了一个上下文操作栏。 I wonder how can I show a menu when someone clicks on an item on the contextual action bar like this: https://i.stack.imgur.com/nQSM0.png 我想知道当有人单击上下文操作栏上的某个项目时如何显示菜单: https : //i.stack.imgur.com/nQSM0.png

my contextual action bar code: 我的上下文操作条形码:

 public class MyActionMode : Java.Lang.Object, ActionMode.ICallback
    {
        private RecyclerViewHolder holder;
        private Context mContext;
        private RecyclerView.Adapter mAdapter;
        private int currentPosition;
        public MyActionMode(Context context) : this(context, null, 0)
        {

        }

        public MyActionMode(Context context, RecyclerView.Adapter adapter, int position)
        {
            mContext = context;
            mAdapter = adapter;
            currentPosition = position;
        }

        public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.itemOneId:

                    // do Delete
                    // mAdapter.RemoveAt(currentPosition);
                    //mAdapter.FinishActionMode();
                    return true;
                case Resource.Id.itemTwoId:
                    // do Update
                    return true;
                default:
                    return false;
            }
        }

        public bool OnCreateActionMode(ActionMode mode, IMenu menu)
        {
            mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu);

            return true;
        }

        public void OnDestroyActionMode(ActionMode mode)
        {
            mode.Dispose();
        }

        public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
        {
            return false;
        }
    }

You can check this Article out which does the same thing with a pop-up menu in Xamarin.Android 您可以检查此文章哪些做同样的事情,在Xamarin.Android弹出菜单

It's easy, first, you create a menu item 很简单,首先,您创建一个菜单项

<?xml version="1.0" encoding="utf-8" ?>  
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">  
<!--///showAsAction="always" ///-->  
<item android:id="@+id/action_settings" android:title="Share" showAsAction="always" />  
<item android:id="@+id/action_settings1" android:title="Bluetooth" showAsAction="always" />  
<item android:id="@+id/action_settings2" android:title="Exit" showAsAction="always" />  
<!--/android:showAsAction="ifRoom"/-->  
<item android:id="@+id/action_settings3" android:title="Share" android:showAsAction="ifRoom" />  
<item android:id="@+id/action_settings4" android:title="Bluetooth" android:showAsAction="ifRoom" />   
</menu>  

Then override the OnCreateOptionsMenu in your activity 然后在您的活动中覆盖OnCreateOptionsMenu

public override bool OnCreateOptionsMenu(IMenu menu)  
{  
   MenuInflater.Inflate(Resource.Menu.option_menu, menu);  
   return true;  
}  

Check the article out for a detailed answer 查看文章以获取详细答案

i find you post another thread: 我发现你发布了另一个主题:

Popup Menu on Contextual Action Bar its not working 上下文操作栏上的弹出菜单不起作用

I have workaround you can take a look: 我有解决方法,您可以看一下:

Firstly, you need to change your Contextualmenu, change item actionviewclass as android.widget.Button. 首先,您需要更改Contextual菜单,将项目actionviewclass更改为android.widget.Button。

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/itemOneId"

    android:title="Delete"/>
<item android:id="@+id/itemTwoId"
    android:title="Update"
    android:actionViewClass="android.widget.Button"
    />

Secondly, fire this button click event in OnCreateActionMode 其次,在OnCreateActionMode中触发此按钮单击事件

 public bool OnCreateActionMode(ActionMode mode, IMenu menu)
    {
        mode.MenuInflater.Inflate(Resource.Menu.ContextualMenu, menu);
        button =(Button)menu.FindItem(Resource.Id.itemTwoId).ActionView;
        button.Background = null;
        button.Text = "UPDATE";
        button.Click += delegate {
            PopupMenu menu1 = new PopupMenu(mContext, button);
            menu1.Inflate(Resource.Menu.popup_menu);
            menu1.Show();
        };
        return true;
    }

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 上下文操作栏菜单项的奇怪行为 - Contextual action bar weird behavior for menu item 如何使用Espresso框架单击上下文操作栏项 - How to click contextual action bar item using Espresso framework 如何删除上下文操作栏(CAB)中的默认项目? - How do I delete a default item in the contextual action bar (CAB)? WebView在上下文操作栏中添加自定义菜单项并覆盖现有项 - WebViewAdd Custom Menu Item in Contextual Action Bar with overriding existing 有空间时,上下文操作栏菜单会将项目强制进入溢出菜单 - Contextual Action Bar menu forces items into overflow menu when there is room 使用actionLayout属性时,为什么不能单击我的操作栏菜单项? - Why can't I click my action bar menu item when using actionLayout property? Android:单击文本视图时显示上下文菜单栏 - Android: Show a contextual menu bar when click on a textview 单击菜单后打开上下文菜单栏 - Open a contextual menu bar upon menu click 当选择了操作栏中的其他菜单项时,需要单击“溢出菜单项”两次 - Need to click Overflow Menu Item twice when other menu item in Action bar is selected 如何从上下文操作模式处理recyclerView项目的单击侦听器? - How handle click listener for recyclerView item from contextual action mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM