简体   繁体   中英

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

I am using Xamarin Android in my apllication. I created RecyclerView, and I created a contextual action bar in it. 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

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

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

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.

<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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM