java - 导航 View 项目按下时不会响应

标签 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);

然后我实现了该方法:

    @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 View ,这就是您的自定义逻辑无法运行的原因。

如果要将两组功能组合在一起,需要调用 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;
}

关于java - 导航 View 项目按下时不会响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61145886/

相关文章:

android - future 已经完成的 flutter 错误

android - java.lang.SecurityException : caller uid XXXXX lacks any of android. 权限.GET_ACCOUNTS

android - 如果该 fragment 已打开,如何防止从抽屉导航中打开该 fragment

android - 获取 Toolbar 的导航图标 View 引用

java - 如何在 Android 上创建渐进式 JPEG 图像

java - 为什么要解释java字节码?

java - 如何将 Option<Try<Foo>> 翻转为 Try<Option<Foo>>

java - 无需使用外部库即可获取 Json 响应中的特定元素

android - android 中的 SQLiteDatabase 查询

android - 如何在没有支持库的情况下创建抽屉导航