18

我正在尝试NavigationView在我的应用程序中使用 Android 支持设计库。由于某种原因,OnNavigationItemSelected没有调用侦听器。这是我的代码

活动布局

<?xml version="1.0" encoding="utf-8"?>
<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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu" />

            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>

活动 onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutID());

    toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
    setSupportActionBar(toolbar);
    toolbar.inflateMenu(R.menu.common_menu);
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(final MenuItem menuItem) {
            Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
            menuItem.setChecked(true);
            // allow some time after closing the drawer before performing real navigation
            // so the user can see what is happening
            drawerLayout.closeDrawer(GravityCompat.START);
            mDrawerActionHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    navigate(menuItem.getItemId());
                }
            }, DRAWER_CLOSE_DELAY_MS);
            drawerLayout.closeDrawers();
            return true;
        }

    });
    usernameTextView = (TextView) findViewById(R.id.drawer_header_username);                  
    usernameTextView.setText(getAppDContext().getAccount().getUsername());
   }
4

3 回答 3

69

当你做 XML 布局时,你应该NavigationView在 BaseLayout ( FrameLayout,LinearLayout等)之后写下

<DrawerLayout>
    <FrameLayout />
    <NavigationView />
</DrawerLayout>
于 2015-07-16T06:46:44.420 回答
14

对我来说,这成功了!

navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
于 2017-03-20T15:19:24.390 回答
7

您的活动主布局应如下所示:

<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/navigationDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/activity_main_content" />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        style="@style/NavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        app:headerLayout="@layout/header"
        app:menu="@menu/menu_drawer"/>

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

在此 NavigationView 中,我链接了 header.xml 和 menu_drawer.xml (来自菜单文件夹),例如 menu_drawer.xml :

<menu xmlns:android="http://schemas.android.com/apk/res/android">
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav1"
                android:checked="true"
                android:icon="@drawable/logo"
                android:title="Navigation item 1"/>
            <item
                android:id="@+id/nav2"
                android:icon="@drawable/logo"
                android:title="Navigation item 2"/>
        </group>
    </menu>

比你的java代码:

public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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

        setUpToolbar();
        setUpNavDrawer();
    }

 private void setUpNavDrawer() {
        NavigationView view = (NavigationView) findViewById(R.id.navigationView);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
        view.setNavigationItemSelectedListener(this);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
    }

检查这是否适合您。在我的项目中,它就像一个魅力。

于 2015-07-14T09:17:05.383 回答