[英]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.