How to fix Android studio 3.5 navigation activity template onNavigationItemSelected not working

后端 未结 5 435
自闭症患者
自闭症患者 2020-12-16 23:16

I created a new navigation drawer activity from Android Studio 3.5 templates, but menu items are not clickable even though I\'m listening for NavigationItemSelected

相关标签:
5条回答
  • 2020-12-16 23:54

    **

    For Menu Items Clickable

    ** NavController offers an OnDestinationChangedListener interface that is called when the NavController's current destination or its arguments change. A new listener can be registered via the addOnDestinationChangedListener() method. Note that when calling addOnDestinationChangedListener(), if the current destination exists, it is immediately sent to your listener.

    ////////////////////////////////////////////////////////////////////////

    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
       @Override
       public void onDestinationChanged(@NonNull NavController controller,
               @NonNull NavDestination destination, @Nullable Bundle arguments) {
           if(destination.getId() == R.id.gallery) {
               //statements
           } else {
           //statements
    }
       }
    });
    
    0 讨论(0)
  • 2020-12-16 23:56

    Inside XML the tag <androidx.drawerlayout.widget.DrawerLayout> in the activity Rearrangement first and <com.google.android.material.navigation.NavigationView> Second . Inside java onCreate example :

    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
                @Override
                public void onDestinationChanged(@NonNull NavController controller,
                                                 @NonNull NavDestination destination, @Nullable Bundle arguments) {
                    if(destination.getId() == R.id.nav_home) {
                        Toast.makeText(Permissions_Display.this,"home",Toast.LENGTH_LONG).show();
                    } if(destination.getId() == R.id.nav_gallery) {
                        Toast.makeText(Permissions_Display.this,"nav_gallery",Toast.LENGTH_LONG).show();
                    }
                    if(destination.getId() == R.id.nav_slideshow) {
                        Toast.makeText(Permissions_Display.this,"nav_slideshow",Toast.LENGTH_LONG).show();
                    } if(destination.getId() == R.id.nav_tools) {
                        Toast.makeText(Permissions_Display.this,"nav_tools",Toast.LENGTH_LONG).show();
                    }
                    if(destination.getId() == R.id.nav_share) {
                        Toast.makeText(Permissions_Display.this,"nav_share",Toast.LENGTH_LONG).show();
                    } if(destination.getId() == R.id.nav_send) {
                        Toast.makeText(Permissions_Display.this,"nav_send",Toast.LENGTH_LONG).show();
                    }
                }
            });
    
    0 讨论(0)
  • 2020-12-17 00:01

    this problem is caused by the ordering issue of xml file from main content

    the default order:

    <com.google.android.material.navigation.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/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
    
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    change to following order:

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <com.google.android.material.navigation.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/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
    
    0 讨论(0)
  • 2020-12-17 00:02

    When you call NavigationUI.setupWithNavController(navigationView, navController), you're saying that you want NavController to handle click events from your NavigationView, navigating to the related screen as per the NavigationUI documentation. This, by necessity, calls setNavigationItemSelectedListener() internally, overriding the setNavigationItemSelectedListener() you call earlier in onCreate(). If you've hooked up your NavigationView to fragments in your app (as the template does out of the box), then there is no need to call setNavigationItemSelectedListener yourself.

    Of course, you should ensure that your layout has not changed from what the Navigation Drawer Activity gives you by default - there is a known issue with Android Studio 3.5 that can lead to the order of views being changed which would break cases like DrawerLayout (where the order of children matters greatly)

    0 讨论(0)
  • 2020-12-17 00:03
    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller,
                                         @NonNull NavDestination destination, @Nullable Bundle arguments) {
            if(destination.getId() == R.id.nav_cart) {
                Toast.makeText(HomeActivity.this,"nav_cart",Toast.LENGTH_LONG).show();
            } if(destination.getId() == R.id.nav_orders) {
                Intent in =new Intent(HomeActivity.this, OrdersActivity.class);
                startActivity(in);
                Toast.makeText(HomeActivity.this,"nav_gallery",Toast.LENGTH_LONG).show();
            }
            if(destination.getId() == R.id.nav_categories) {
                Toast.makeText(HomeActivity.this,"nav_categories",Toast.LENGTH_LONG).show();
            }
            if(destination.getId() == R.id.nav_settings) {
                Intent in =new Intent(HomeActivity.this, SettingsActivity.class);
                startActivity(in);
                Toast.makeText(HomeActivity.this,"nav_settings",Toast.LENGTH_LONG).show();
            } if(destination.getId() == R.id.nav_logout) {
                Toast.makeText(HomeActivity.this,"nav_logout",Toast.LENGTH_LONG).show();
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题