0
votes

Implémentation de Clicklistners dans le menu de navigation

J'ai créé une activité de navigation Android à partir d'un studio Android. Maintenant, dans cette activité, comment dois-je implémenter l'écouteur de clic dans le menu du tiroir de navigation? Comme sur un clic sur l'élément de menu, comment puis-je effectuer une tâche L'activité est: La classe publique MainActivity étend AppCompatActivity implémente NavigationView.OnNavigationItemSelectedListener {

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    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);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

    switch (menuItem.getItemId()){
            case R.id.nav_home:
                Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}


1 commentaires

vous devez utiliser onNavigationItemSelected et différencier les éléments de menu par leurs identifiants dans menu.xml et effectuer la tâche que vous voulez


3 Réponses :


1
votes

utilisez ceci comme exemple.

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()){
     case R.id.nav_home:
        Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
        break;
     case R.id.nav_otherId:
        //your action
        Toast.makeText(this, "Gallery", Toast.LENGTH_SHORT).show();
        break;
     case R.id.nav_otherId2:
        //your action
        Toast.makeText(this, "Next", Toast.LENGTH_SHORT).show();
        break;


}
return true;

}


0 commentaires

1
votes

La mise à niveau 3.5 entraîne une réorganisation incorrecte de XML, et elle déplace le contenu ci-dessus (et donc derrière) le contenu principal du. Vous devrez déplacer le vers après le (ou tout ce que vous pourriez avoir pour le contenu principal). Et boum vous êtes prêt à partir

et en java

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull
                    NavDestination destination, @Nullable Bundle arguments) {
                if (destination.getId() == R.id.nav_home) {
                    Toast.makeText(MainActivity.this, "home", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_gallery) {
                    Toast.makeText(MainActivity.this, "nav_gallery", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_slideshow) {
                    Toast.makeText(MainActivity.this, "nav_slideshow", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_tools) {
                    Toast.makeText(MainActivity.this, "nav_tools", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_share) {
                    Toast.makeText(MainActivity.this, "nav_share", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_send) {
                    Toast.makeText(MainActivity.this, "nav_send", Toast.LENGTH_LONG).show();
                }
            }
        });


0 commentaires

0
votes

Voici ce que vous recherchez:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if(id == R.id.hello){
        Intent intent = new Intent(MainMenu.this, HelloActivity.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}


0 commentaires