首页 > 解决方案 > 如何在 NavigationView 中创建 clickListener?

问题描述

我有一个具有 4 个按钮的 NavigationView,但我无法单击它们。当任何这些按钮被点击但它不起作用时,我已经做了这个方法。

private DrawerLayout aDrawerLayout;
private ActionBarDrawerToggle aToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pagina_inicial);

    //creating the dropdown menu
    aDrawerLayout = findViewById(R.id.dropdown_menu);
    aToggle = new ActionBarDrawerToggle(this, aDrawerLayout, R.string.abrir, R.string.fechar);
    aDrawerLayout.addDrawerListener(aToggle);
    aToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //associating the listener to the navigation menu
    NavigationView navView = findViewById(R.id.menu_cair);
    navView.setNavigationItemSelectedListener(this);


    BottomNavigationView navigationView = (BottomNavigationView) findViewById(R.id.navigationView);

}
//what to do when one of the options gets clicked
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.paginaPerfil: {
            Toast.makeText(getApplicationContext(), "Perfil", Toast.LENGTH_LONG).show();
            break;
        }
        case R.id.paginaAjuda:{
            Toast.makeText(getApplicationContext(), "Ajuda", Toast.LENGTH_LONG).show();
            break;
        }
    }
    return true;
}}

我已经尝试将这个方法放在 onCreate 中,但它没有任何变化。

我也有这个 navigationView 值

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PaginaInicial"
android:background="#717171"
android:id="@+id/dropdown_menu">

<com.google.android.material.navigation.NavigationView
    android:id="@+id/menu_cair"
    app:menu="@menu/menu_navegacao"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/cinzentoClaro"
    app:itemTextColor="@color/azulEscuro"
    app:itemIconTint="@color/azulEscuro"
    />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/cinzentoEscuro"
        android:outlineSpotShadowColor="@color/azulClaro"
        app:itemBackground="@color/azulClaro"
        app:itemIconTint="@animator/selector"
        app:itemTextColor="#ffffff"
        app:menu="@menu/menu_navegacao_baixo"/>
</RelativeLayout>

</androidx.drawerlayout.widget.DrawerLayout>

这是可能出现错误的大部分代码。

标签: javaandroidnavigationview

解决方案


推荐阅读