java - 如何在 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>
这是可能出现错误的大部分代码。
解决方案
推荐阅读
- html - 如何使用 css flex 保持一个盒子为空
- python - TypeError: 'dict' 对象在 Python3 或 Python2 中不可调用,导致内存错误
- database - OLAP 系统中的星形 snd 雪花模式
- java - 如何实现多语言安卓应用?
- sql - 从 JSON 对象获取 JSON 数组并计算对象数量
- objectlistview - C# Objectlistview 仅对特定行进行分组
- html - 如何使用 JQuery 在 div 中显示当前时间
- bash - 当 bash 中的另一个文本文件中也出现相同的数字时,更改文本文件中的字符串
- docker - 将 Jenkins 凭证(文件)传递给 Docker Build Arg
- python - 如何在不定义元素的情况下定义数组?