如何导航到导航抽屉项目上的活动单击

时间:2019-12-23 06:16:24

标签: java android androidx android-jetpack

这是我的主要活动,我想导航到我的导航抽屉项目单击上的一个活动。我已经检查了实现此目标的旧方法,即使用switch语句和onNavigationItemSelected方法。 我知道这是一个经常被问到的问题,但是在阅读了许多关于堆栈溢出的问题和解决方案后,我感到很困惑。对于片段以及单击导航抽屉中的项目来启动活动所需的条件,我感到困惑。

MainActivity.java


import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.navigation.NavigationView;

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        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_gallery, R.id.nav_slideshow,
                R.id.nav_tools, 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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

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.vdosol.navigationdrawer.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_gallery"
        android:name="com.vdosol.navigationdrawer.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow"
        android:name="com.vdosol.navigationdrawer.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />

    <fragment
        android:id="@+id/nav_tools"
        android:name="com.vdosol.navigationdrawer.ui.tools.ToolsFragment"
        android:label="@string/menu_tools"
        tools:layout="@layout/fragment_tools" />

    <fragment
        android:id="@+id/nav_share"
        android:name="com.vdosol.navigationdrawer.ui.share.ShareFragment"
        android:label="@string/menu_share"
        tools:layout="@layout/fragment_share" />

    <fragment
        android:id="@+id/nav_send"
        android:name="com.vdosol.navigationdrawer.ui.send.SendFragment"
        android:label="@string/menu_send"
        tools:layout="@layout/fragment_send" />
</navigation>

2 个答案:

答案 0 :(得分:0)

  1. 在您的“活动”中实现侦听器:

    public class HomeActivity extends AppCompatActivity implements 
                  NavigationView.OnNavigationItemSelectedListener
    
  2. Activity的onCreate中的
  3. setNavigationItemSelectedListener

    NavigationView mNavigationView = (NavigationView) findViewById(R.id.account_navigation_view);
    
    if (mNavigationView != null) {
            mNavigationView.setNavigationItemSelectedListener(this);
    }
    
  4. 覆盖方法

    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
    
        if (id == R.id.nav_account) {//DO your stuff }
        ...
    }
    

答案 1 :(得分:0)

您可以执行以下操作进行导航:

MenuItem logOutItem = navigationView.getMenu().findItem(R.id.nav_logout);
        logOutItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
//do your stuff
                return true;
            }
        });

您的AppBarConfiguration和Nav Controller不应包含上述ID,即“ nav_logout”(在我的情况下):

mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_plan, R.id.navigation_history, R.id.navigation_about, R.id.navigation_feedback, R.id.navigation_notifications)
                .setDrawerLayout(drawer)
                .build();

您的菜单应仅包含在菜单文件中。...无需将其添加到AppBarConfiguration和NavController中。...

希望这会有所帮助。...编码愉快