抽屉项目中的导航抽屉活动设置单击事件

时间:2019-12-07 04:15:30

标签: java android

嗨,我正在尝试在抽屉中的“导航视图”中放置一个事件项单击,它工作正常,但是问题是抽屉中的其他项停止工作,似乎在我的导航视图中放置一个项单击事件会影响我的NavigationController

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bundy_clock);
    dBhelper = new DBhelper(this);

    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_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.nav_logout){
            AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
            conDialog.setTitle("Confirm");
            conDialog.setMessage("Are you sure you want to log out?");
            conDialog.setCancelable(false);

            conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    startActivity(new Intent(BundyClock.this, MainActivity.class));
                    Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
                    finish();
                }
            });

            conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            final AlertDialog sdialog = conDialog.create();
            sdialog.show();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
        }
    });

}

1 个答案:

答案 0 :(得分:2)

您必须使用NavigationUI.onNavDestinationSelected来处理此问题。检查以下内容:

boolean handled = NavigationUI.onNavDestinationSelected(menuItem, navController);
if (!handled) {
    if(id == R.id.nav_logout){
        AlertDialog.Builder conDialog = new AlertDialog.Builder(BundyClock.this);
        conDialog.setTitle("Confirm");
        conDialog.setMessage("Are you sure you want to log out?");
        conDialog.setCancelable(false);

        conDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(BundyClock.this, MainActivity.class));
                Toast.makeText(getApplicationContext(),"Successfully Logout",Toast.LENGTH_SHORT).show();
                finish();
            }
        });

        conDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        final AlertDialog sdialog = conDialog.create();
        sdialog.show();
    }
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return handled;