【问题标题】:Android NavigationView Not Responding to Click Events on ItemsAndroid NavigationView不响应项目上的点击事件
【发布时间】:2019-02-19 15:04:42
【问题描述】:

我正在尝试向我的应用程序添加导航视图,但由于某种原因,我无法让它响应它包含的任何项目上的任何点击事件。

我的 activity_main.xml 文件如下所示:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navheader"
        app:menu="@menu/menu_navigation" />

    <LinearLayout
        ...
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

在我的 MainActivity.java 中,我有这段代码用于创建 NavigationView,它是项目选择侦听器:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                // This method will trigger on item Click of navigation menu
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    if(menuItem.isChecked()) menuItem.setChecked(false);
                    else menuItem.setChecked(true);
                    mDrawerLayout.closeDrawers();
                    switch(menuItem.getItemId()) {
                        case R.id.nav_item_fragment:
                            // open a fragment
                            Toast.makeText(getApplicationContext(),"Items Selected",Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.nav_logs_fragment:
                            // open a fragment
                            Toast.makeText(getApplicationContext(),"Logs Selected",Toast.LENGTH_SHORT).show();
                            break;
                        case R.id.nav_settings_fragment:
                            // open a fragment
                            Toast.makeText(getApplicationContext(),"Settings Selected",Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            return true;
                    }
                    return true;
                }
            });

    final ActionBar actionBar = getSupportActionBar();

    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };

    fragmentManager = getSupportFragmentManager();

    ...
}

这个问题真的让我摸不着头脑,因为我在我编写的旧应用程序中使用了完全相同的代码,而且它似乎工作正常。我正在使用带有 Build Tools 22.0.1 的 Android SDK 22。

【问题讨论】:

  • 显示你的 menuLayout "menu_navigation"

标签: java android


【解决方案1】:

遇到了同样的问题,这是我的解决方案。

我以编程方式将HeaderView 添加到NavigationView 因此我已经有了NavigationView

我打电话给navigationView.bringToFront();

这里是上下文的代码 sn-p

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.bringToFront();

Here is this code snippetGitHub Repo for my Project

【讨论】:

  • 你应该穿披风,因为你是超级英雄。
  • 3 小时的 android 类调试,但未找到解决方案。我爱你!
  • 我花了两天时间才弄明白,很高兴看到它对你们有所帮助。
  • 在我升级到 AndroidX 之前,我的代码在没有这个的情况下工作,我想知道原因是什么?无论如何,这解决了我的问题。
  • 我改变(和设置)了多少次听众...... :(你救了一条命!
【解决方案2】:

我刚刚遇到了同样的问题,这个解决方案 https://stackoverflow.com/a/24536187/166365 对我有用,我立即收到了选定的项目事件。您需要覆盖 ActionBarToggle 构造函数中的 onDrawerOpened 和 onDrawerClosed 事件,然后应用所选答案中建议的更改。对我来说,我只需要像这样调用触发事件的视图:

@Override
public void onDrawerOpened(View drawerView) {
    super.onDrawerOpened(drawerView);
    drawerView.bringToFront();
    drawerView.requestLayout();
}

希望这会有所帮助!如果我可以进一步澄清,请告诉我!

【讨论】:

    【解决方案3】:

    尝试添加navigationView.bringToFront(); 在你之前

    navigationView.setNavigationItemSelectedListener(new 
    navigationView.OnNavigationItemSelectedListener() {...........});
    

    我试过了,它对我有用。

    一切顺利

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      相关资源
      最近更新 更多