【问题标题】:NavigationView OnNavigationItemSelectedListener not being calledNavigationView OnNavigationItemSelectedListener 未被调用
【发布时间】:2015-10-02 13:39:07
【问题描述】:

我正在尝试在我的应用中使用来自 Android 支持设计库的 NavigationView。出于某种原因,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());
   }

【问题讨论】:

  • 是否有理由在 NavigationView 上设置两次监听器?
  • 糟糕。在这里粘贴了错误的代码。已更正。感谢您指出。
  • 发布您的完整版面

标签: android navigationview android-support-design


【解决方案1】:

做XML布局时,要在BaseLayout后面写下NavigationViewFrameLayoutLinearLayout等)

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

【讨论】:

  • 这解决了我在使用后退按钮在WebView 中退出全屏视频时遇到的问题。 FrameLayout 在我的 XML 文件中列在 NavigationView 之后,即使它被设置为可见性 GONE,它仍然掩盖了 NavigationView 并阻止触摸被注册。
【解决方案2】:

对我来说,这成功了!

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

【讨论】:

  • 拯救了我的一天 - 非常感谢!
【解决方案3】:

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

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

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

【讨论】:

  • 选择汉堡图标时是否调用onOptionsItemSelected?不。此外,当我选择导航项时,它不会调用侦听器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
  • 2019-02-07
  • 2016-05-21
相关资源
最近更新 更多