OnNavigationItemSelectedListener

OnNavigationItemSelectedListener

本文介绍了NavigationView OnNavigationItemSelectedListener没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用来自我应用中的Android支持设计库。由于某些原因,监听器没有被调用。这是我的代码

I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code

活动布局

<?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()

Activity 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());
   }


推荐答案

进行XML布局时,您应该在BaseLayout( FrameLayout LinearLayout 之后记下 NavigationView 等等。)

When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)

<DrawerLayout>
    <FrameLayout />
    <NavigationView />
</DrawerLayout>

这篇关于NavigationView OnNavigationItemSelectedListener没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:05