按下导航菜单项将不响应 [英] Navigation view items will not respond when pressed

查看:106
本文介绍了按下导航菜单项将不响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有侧面导航抽屉的应用程序.抽屉可以很好地打开,但是应该可以单击的文本似乎没有响应.动画显示,轻敲抽屉时有反馈(您可以听到声音),但没有任何反应.我试图放置祝酒消息,以查看按钮是否注册了动作,但是当按下按钮时,没有祝酒出现. 代码如下(我已经实现了NavigationView.OnNavigationItemSelectedListener):

I am working on an app with a side Navigation drawer. The drawer opens fine, however the text that supposedly can be "clickable" does not seem to respond. The animation shows that there is feedback to when the drawer is tapped (you can hear the sound) however nothing results of it. I have tried to place toast messages to see if the button registers an action, but when pressed, no toast appears. The code goes as follows (I have implemented 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时,它将用设置为android:id="@+id/nav_settings"的那一个替换NavHostFragment中的Fragment).该侦听器会覆盖您设置的OnNavigationItemSelectedListener视图,这就是为什么您的自定义逻辑无法运行的原因.

Calls setNavigationItemSelectedListener internally to connect destinations to menu items (i.e., when you click on the R.id.nav_settings MenuItem, it'll replace the Fragment in your NavHostFragment with the one with android:id="@+id/nav_settings" set). This listener overrides the OnNavigationItemSelectedListener view you've set, which is why your custom logic doesn't run.

如果要将两组功能组合在一起,则需要调用navigationView.setNavigationItemSelectedListener(this); 之后 setupWithNavController,并使用

If you want to combine both sets of functionality together, you need to call navigationView.setNavigationItemSelectedListener(this); after setupWithNavController and trigger the default behavior with 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;
}

这篇关于按下导航菜单项将不响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆