导航视图项在按下时不会响应

Navigation view items will not respond when pressed

我正在开发一个带有侧边导航抽屉的应用程序。抽屉可以正常打开,但是应该是 "clickable" 的文本似乎没有响应。动画显示抽屉被轻敲时有反馈(您可以听到声音),但没有任何结果。我试图放置 toast 消息以查看按钮是否注册了一个动作,但是按下时,没有出现 toast。 代码如下(我已经实现了NavigationView.OnNavigationItemSelectedListener):

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        // 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_history, R.id.nav_settings,
                R.id.nav_help, R.id.nav_signout)
                .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 onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_history:
    Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
                break;
            case R.id.nav_help:

                break;
            case R.id.nav_settings:

                break;
            case R.id.nav_signout:
                signOut();
                break;
        }

        DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

谢谢

NavigationUI.setupWithNavController(navigationView, navController);

在内部调用 setNavigationItemSelectedListener 以将目的地连接到菜单项(即,当您单击 R.id.nav_settings MenuItem 时,它会将 NavHostFragment 中的 Fragment 替换为 android:id="@+id/nav_settings" 放)。此侦听器会覆盖您设置的 OnNavigationItemSelectedListener 视图,这就是为什么您的自定义逻辑不会 运行.

如果您想将两组功能组合在一起,您需要在 setupWithNavController 之后调用 navigationView.setNavigationItemSelectedListener(this); 并使用 [=19 触发默认行为=]:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driver_home);
    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_history, R.id.nav_settings,
            R.id.nav_help, R.id.nav_signout)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
    // This line needs to be after setupWithNavController()
    navigationView.setNavigationItemSelectedListener(this);

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    switch (menuItem.getItemId()){
        case R.id.nav_history:
            Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
            break;
        case R.id.nav_signout:
            signOut();
            break;
        default:
            // Trigger the default action of replacing the current
            // screen with the one matching the MenuItem's ID
            NavigationUI.onNavDestinationSelected(menuItem, navController);
    }

    DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

只是基于已接受答案的一个小样本,但在片段内,没有覆盖注释并使用 Kotlin。

 bottomNav.setOnNavigationItemSelectedListener {

        when (it.itemId) {
            R.id.share -> {
                shareViaWhatsApp()
            }
            else -> {
                NavigationUI.onNavDestinationSelected(it, navController!!)
            }
        }

        true

    }