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

Str*_*dog 4 java android navigation-drawer

我正在开发一个带有侧边导航抽屉的应用程序。抽屉打开得很好,但是据称可以“单击”的文本似乎没有响应。动画显示当点击抽屉时有反馈(您可以听到声音),但没有任何结果。我试图放置 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);
Run Code Online (Sandbox Code Playgroud)

然后我实现了这个方法:

    @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;
    }
Run Code Online (Sandbox Code Playgroud)

谢谢

ian*_*ake 12

线

NavigationUI.setupWithNavController(navigationView, navController);
Run Code Online (Sandbox Code Playgroud)

setNavigationItemSelectedListener内部调用以将目标连接到菜单项(即,当您单击R.id.nav_settingsMenuItem 时,它将用带android:id="@+id/nav_settings"set的片段替换 NavHostFragment 中的片段)。此侦听器会覆盖OnNavigationItemSelectedListener您设置的视图,这就是您的自定义逻辑不运行的原因。

如果要将两组功能组合在一起,则需要调用navigationView.setNavigationItemSelectedListener(this); after setupWithNavController并触发默认行为NavigationUI.onNavDestinationSelected()

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;
}
Run Code Online (Sandbox Code Playgroud)