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