按下导航菜单项将不响应

我正在使用带有侧面导航抽屉的应用程序。抽屉打开正常,但是应该可以单击的文本似乎没有响应。动画显示在轻敲抽屉时有反馈(您可以听到声音),但没有任何反应。我试图放置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_settingsMenuItem时,它将用已android:id="@+id/nav_settings"设置的菜单项替换NavHostFragment中的Fragment

)。该侦听器将覆盖OnNavigationItemSelectedListener您设置的视图,这就是为什么您的自定义逻辑不会运行的原因。

如果要将两组功能组合在一起,则需要在navigationView.setNavigationItemSelectedListener(this);

调用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;

}

以上是 按下导航菜单项将不响应 的全部内容, 来源链接: utcz.com/qa/412419.html

回到顶部