Android:导航组件不能同时使用: 和NavigationItemSelectedListener

问题描述 投票:1回答:1

我做了什么:

我已经创建了Navigation Drawer Activity,作为Navigation Drawer Activity的新格式,根据新的Android体系结构,我使用Navigation Component结构获得了它。

带有下面NavigationViewNavControllerNavigationUI代码,它是当我单击任何导航项时打开的片段。

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_profile, R.id.nav_privacy_policy,
            R.id.nav_terms, R.id.nav_contact_us, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

这是nav_host_fragment

<fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

导航正在使用此navigation/mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.sohamerp.marsremedies.fragment.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_profile"
        android:name="com.sohamerp.marsremedies.fragment.ProfileFragment"
        android:label="@string/menu_my_profile"
        tools:layout="@layout/fragment_profile" />

    <fragment
        android:id="@+id/nav_privacy_policy"
        android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
        android:label="@string/menu_privacy_policy"
        tools:layout="@layout/fragment_privacy_policy" />

    <fragment
        android:id="@+id/nav_terms"
        android:name="com.sohamerp.marsremedies.fragment.TermsConditionFragment"
        android:label="@string/menu_terms"
        tools:layout="@layout/fragment_terms_condition" />

    <fragment
        android:id="@+id/nav_contact_us"
        android:name="com.sohamerp.marsremedies.fragment.ContactUsFragment"
        android:label="@string/menu_contact_us"
        tools:layout="@layout/fragment_terms_condition" />

</navigation>

我遇到问题的地方:

Android Studio为所有6个菜单创建了片段,但是我不想在单击最后两个项目时打开片段。 为此,我从<fragment>中删除了最后两个mobile_navigation.xml标签>

enter image description here

我尝试添加下面的setNavigationItemSelectedListener

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.nav_share)
                Toast.makeText(NavigationMenuActivity.this, "Sharing...", Toast.LENGTH_SHORT).show();
            else if (menuItem.getItemId() == R.id.nav_send)
                Toast.makeText(NavigationMenuActivity.this, "Rating...", Toast.LENGTH_SHORT).show();
            return false;
        }
    });

当我单击最后两个菜单但前四个菜单不起作用时,正在显示Toast。

我该怎么做才能使其正常工作?

build.gradle依赖项:
implementation 'androidx.navigation:navigation-fragment:2.1.0'
implementation 'androidx.navigation:navigation-ui:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

我做了什么:我创建了Navigation Drawer Activity,作为Navigation Drawer Activity的更新新格式,根据新的Android体系结构,我使用了Navigation Component结构。 ...

android android-fragments navigation-drawer android-architecture-navigation android-navigationview
1个回答
0
投票
You can make a switch statement and for 4 other fragments you should return `true`.

switch(menuItem.getItemId) {
  case R.id.nav_share:
  case R.id.nav_gallery:
  case R.id.nav_slideshow:
  case R.id.nav_tools:
      return true
  case R.id.nav_share:
                Toast.makeText(NavigationMenuActivity.this, "Sharing...", Toast.LENGTH_SHORT).show();
      return false;
  case R.id.nav_send: 
                Toast.makeText(NavigationMenuActivity.this, "Rating...",Toast.LENGTH_SHORT).show();
      return false;
  default:
      return false;
}
© www.soinside.com 2019 - 2024. All rights reserved.