java – Android NavigationView无法响应项目上的Click事件

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

我的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();

    ...
}

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

最佳答案
有同样的问题,这是我的解决方案.

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

我叫了navigationView.bringToFront();

这是上下文的代码片段

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

Here is this code snippet英寸
GitHub Repo for my Project

转载注明原文:java – Android NavigationView无法响应项目上的Click事件 - 代码日志