示例代码:
示例代码:
public class MainActivity extends AppCompatActivity {
private NavController navController;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navController = Navigation.findNavController(this, R.id.main_fragment_container);
NavigationView navigationView = findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
navController.navigate(R.id.firstFragment);
break;
case R.id.nav_second_fragment:
navController.navigate(R.id.secondFragment);
break;
case R.id.nav_third_fragment:
navController.navigate(R.id.thirdFragment);
break;
}
return true;
}
});
}
}
示例代码:
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private NavController navController;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navController = Navigation.findNavController(this, R.id.main_fragment_container);
NavigationView navigationView = findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem