-1
当你点击一个列表视图项目并且在你点击的项目下面有菜单弹出菜单时,你如何使菜单弹出窗口出现?如何在ListView项目上创建一个弹出菜单?
是否有可能使用ListView.ItemClick事件来做到这一点?
此问题适用于api 22+。
当你点击一个列表视图项目并且在你点击的项目下面有菜单弹出菜单时,你如何使菜单弹出窗口出现?如何在ListView项目上创建一个弹出菜单?
是否有可能使用ListView.ItemClick事件来做到这一点?
此问题适用于api 22+。
这个问题已经有很多答案,但我发现的所有东西都在旧的API上,所以这就是我做到的。
使用ListView.ItemClick事件可以做到这一点,并实现如图所示的菜单。
(名称删除)
[Activity(Label = "Employee Management", Theme = "@android:style/Theme.Material")]
public class EmpMgmtActivity : Activity
{
ListView empListView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.EmpMgmtLayout);
empListView = FindViewById<ListView>(Resource.Id.EmpMgmtList);
GenerateEmpList(EmployeeStorage.employeeList);
empListView.ItemClick += EmpListView_ItemClick;
}
private void EmpListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var menu = new PopupMenu(this, empListView.GetChildAt(e.Position));
menu.Inflate(Resource.Layout.popup_menu);
menu.MenuItemClick += (s, a) =>
{
switch (a.Item.ItemId)
{
case Resource.Id.pop_button1:
// update stuff
break;
case Resource.Id.pop_button2:
// delete stuff
break;
}
};
menu.Show();
}
大多数信息是很容易找到的一部分,我曾与心不是麻烦的菜单项,弹出,但把它弹出右侧订单项。对我来说,关键是从列表中查找单个视图。
var menu = new PopupMenu(this, empListView.GetChildAt(e.Position));
如果使用 (视图)发件人 其中谈到在偶数指定参数时,它将会把菜单附近的页面,这是不理想的顶部。
因此使用 ListView.GetChildAt(e.Position) 它返回列表项目的实际视图,您可以在正确的位置弹出菜单。
的弹出式菜单XML代码:
<?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">
<item android:id="@+id/pop_button1" android:title="Edit Employee" showAsAction="always" />
<item android:id="@+id/pop_button2" android:title="Delete Employee" showAsAction="always" />
</menu>
希望这有助于!