我试图将导航 View 添加到我的应用程序中,但是由于某种原因,我无法使它响应它包含的任何项目上的任何单击事件。

我的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与构建工具22.0.1结合使用。

最佳答案

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

我是通过编程方式将HeaderView添加到NavigationView的,因此我已经有了NavigationView
我叫navigationView.bringToFront();
这是上下文的代码 fragment

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

Here is this code snippet
GitHub Repo for my Project

关于java - Android NavigationView不响应项目的点击事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36816305/

10-12 01:46