xamarin的.android导航抽屉列表不检测ItemClick事件

本文关键字:检测 ItemClick 事件 列表 android 导航 xamarin | 更新日期: 2023-09-27 18:14:33

我已经成功创建了一个带有图标的自定义侧边导航菜单。我现在想检测列表上的项目单击事件,以便从仪表板活动开始新的活动,但是SelectItem方法没有在项目单击时触发。

这是我的SideMenu方法,用于创建导航抽屉

private void SideMenu()
{
 string[] appActions = new string[]{"Transactions", "Budgeting","Invoicing", "Bills", "'n", "Reminders", "'n", "Reports", "Accounts", "'n", "Logout" };
    int[] icons = new int[]{Resource.Drawable.sidenavtransactions, Resource.Drawable.sidenavbudget, Resource.Drawable.sidenavinvoicing, Resource.Drawable.sidenavbills, Resource.Drawable.trans, Resource.Drawable.sidenavreminders, Resource.Drawable.trans, Resource.Drawable.trans, Resource.Drawable.trans, Resource.Drawable.trans, Resource.Drawable.trans };
    actions = new List<Actions> ();
    for (int i = 0; i < icons.Length; i++) {
        Actions action = new Actions ();
        action.actionName = appActions[i];
        action.actionIcon = icons[i];
        actions.Add (action);
    }
    drawer = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
    drawerList = FindViewById<ListView> (Resource.Id.left_drawer);
    drawerList.Adapter = new NavigationDrawerAdapter (this, actions);
    drawerList.ItemClick += (sender, e) => SelectItem(e.Position);

    ActionBar.SetDisplayHomeAsUpEnabled (true);
    ActionBar.SetHomeButtonEnabled (true);
    drawerToggle = new MyActionBarDrawerToggle (this, drawer, Resource.Drawable.hamburger, Resource.String.DrawerOpen, Resource.String.DrawerClose);
    drawerToggle.DrawerClosed += delegate {
        ActionBar.Title = title;
        InvalidateOptionsMenu ();
    };
    drawerToggle.DrawerOpened += delegate {
        ActionBar.Title = drawerTitle;
        drawerList.BringToFront();
        drawer.RequestLayout();
        InvalidateOptionsMenu();
    };
    drawer.SetDrawerListener (drawerToggle);
}

这是SelectItem方法,应该在项目点击

时调用
private void SelectItem(int position)
    {
        switch (position) {
        case 0: 
            Toast.MakeText (this, actions [0].actionName, ToastLength.Short).Show ();
            break;
        case 1:
            Toast.MakeText (this, actions [1].actionName, ToastLength.Short).Show ();
            break;
        case 2:
            Toast.MakeText (this, actions [2].actionName, ToastLength.Short).Show ();
            break;
        case 3:
            Toast.MakeText (this, actions [3].actionName, ToastLength.Short).Show ();
            break;
        case 4:
            Toast.MakeText (this, actions [4].actionName, ToastLength.Short).Show ();
            break;
        default:
            break;
        }
    }

xamarin的.android导航抽屉列表不检测ItemClick事件

on SelectItem(),你检查过这是否被触发了吗?
在我的情况下,点击不工作,当其他东西是在这个视图蒸汽焦点,例如,如果你做一个自定义的ListView与一个复选框,然后复选框可能会窃取焦点和改变预期的行为。
然后必须在此复选框中设置focusable, focusableintouchmodeclickable为false。检查您的问题是否与此相关