我如何参考 MenuItem 点击?
How i can take reference of MenuItem click?
我有问题。我不知道如何在我的 DrawerLayot 中获取我的 ListView 的引用。
我有一个 drawerLayout,它有一个 NavigationView 和一个 Menu。
单击时如何显示布局相关的通知?
navView = (NavigationView) findViewById(R.id.nav_view);
View navHeader = navView.getHeaderView(0);
TextView usuario_header = (TextView) navHeader.findViewById(R.id.username);
TextView correo_header = (TextView) navHeader.findViewById(R.id.emailNavegador);
布局删除activity_principal.
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".PrincipalActivity">
<!-- Contenido Principal -->
<include layout="@layout/main_content" />
<!-- Menú Deslizante -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu" /></android.support.v4.widget.DrawerLayout>
布局main_content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<RelativeLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:text="Holis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="148dp"
android:layout_marginTop="58dp"
android:id="@+id/textMain" />
</RelativeLayout></LinearLayout>
布局nav_header
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:background="@drawable/material_background3"
android:gravity="bottom"
android:orientation="vertical"
android:padding="@dimen/header_left_padding"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/header">
<!-- Imagen de perfil -->
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circle_image"
android:layout_width="@dimen/perfil_image_size"
android:layout_height="@dimen/perfil_image_size"
android:layout_marginBottom="16dp"
android:scaleType="centerCrop"
android:src="@drawable/pacoalba"
app:border_color="@android:color/white"
app:border_width="1dp" />
<!-- Nombre de Usuario -->
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textStyle="bold" />
<!-- Correo de la cuenta -->
<TextView
android:id="@+id/emailNavegador"
style="@style/Widget.AppCompat.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" /></LinearLayout>
菜单
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_noticias"
android:checked="true"
android:icon="@drawable/ic_libro"
android:title="@string/noticias_item" />
<item
android:id="@+id/nav_sesion"
android:icon="@drawable/ic_sesion"
android:title="@string/sesion_item" />
<item
android:id="@+id/nav_mapa"
android:icon="@drawable/ic_mapa"
android:title="@string/mapa_item" />
<item
android:id="@+id/nav_rutas"
android:icon="@drawable/ic_rutas"
android:title="@string/rutas_item" />
<item
android:id="@+id/nav_eventos"
android:icon="@drawable/ic_eventos"
android:title="@string/eventos_item" />
<item
android:id="@+id/nav_alertas"
android:icon="@drawable/ic_alertas"
android:title="@string/alertas_item" />
<item
android:id="@+id/nav_multimedia"
android:icon="@drawable/ic_multimedia"
android:title="@string/multimedia_item" />
<!-- Sección de configuración -->
<item
android:id="@+id/configuration_section"
android:title="@string/configuracion_item">
<menu>
<item
android:id="@+id/nav_log_out"
android:icon="@android:drawable/ic_lock_power_off"
android:title="@string/log_out_item" />
</menu>
</item>
</group>
非常感谢!
要获取导航抽屉中菜单项的引用,您必须重写
这个方法
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_noticias:
Toast.makeText(getApplicationContext(),"Noticias",Toast.LENGTH_SHORT).show;
break;
case R.id. nav_sesion:
Toast.makeText(getApplicationContext(),"nav_sesion",Toast.LENGTH_SHORT).show;
break;
......
}
return false;
}
});
这就是您在导航菜单中实现点击事件的方式..
我有问题。我不知道如何在我的 DrawerLayot 中获取我的 ListView 的引用。
我有一个 drawerLayout,它有一个 NavigationView 和一个 Menu。
单击时如何显示布局相关的通知?
navView = (NavigationView) findViewById(R.id.nav_view);
View navHeader = navView.getHeaderView(0);
TextView usuario_header = (TextView) navHeader.findViewById(R.id.username);
TextView correo_header = (TextView) navHeader.findViewById(R.id.emailNavegador);
布局删除activity_principal.
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".PrincipalActivity">
<!-- Contenido Principal -->
<include layout="@layout/main_content" />
<!-- Menú Deslizante -->
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu" /></android.support.v4.widget.DrawerLayout>
布局main_content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<RelativeLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:text="Holis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="148dp"
android:layout_marginTop="58dp"
android:id="@+id/textMain" />
</RelativeLayout></LinearLayout>
布局nav_header
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:background="@drawable/material_background3"
android:gravity="bottom"
android:orientation="vertical"
android:padding="@dimen/header_left_padding"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/header">
<!-- Imagen de perfil -->
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circle_image"
android:layout_width="@dimen/perfil_image_size"
android:layout_height="@dimen/perfil_image_size"
android:layout_marginBottom="16dp"
android:scaleType="centerCrop"
android:src="@drawable/pacoalba"
app:border_color="@android:color/white"
app:border_width="1dp" />
<!-- Nombre de Usuario -->
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textStyle="bold" />
<!-- Correo de la cuenta -->
<TextView
android:id="@+id/emailNavegador"
style="@style/Widget.AppCompat.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" /></LinearLayout>
菜单
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_noticias"
android:checked="true"
android:icon="@drawable/ic_libro"
android:title="@string/noticias_item" />
<item
android:id="@+id/nav_sesion"
android:icon="@drawable/ic_sesion"
android:title="@string/sesion_item" />
<item
android:id="@+id/nav_mapa"
android:icon="@drawable/ic_mapa"
android:title="@string/mapa_item" />
<item
android:id="@+id/nav_rutas"
android:icon="@drawable/ic_rutas"
android:title="@string/rutas_item" />
<item
android:id="@+id/nav_eventos"
android:icon="@drawable/ic_eventos"
android:title="@string/eventos_item" />
<item
android:id="@+id/nav_alertas"
android:icon="@drawable/ic_alertas"
android:title="@string/alertas_item" />
<item
android:id="@+id/nav_multimedia"
android:icon="@drawable/ic_multimedia"
android:title="@string/multimedia_item" />
<!-- Sección de configuración -->
<item
android:id="@+id/configuration_section"
android:title="@string/configuracion_item">
<menu>
<item
android:id="@+id/nav_log_out"
android:icon="@android:drawable/ic_lock_power_off"
android:title="@string/log_out_item" />
</menu>
</item>
</group>
非常感谢!
要获取导航抽屉中菜单项的引用,您必须重写 这个方法
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.nav_noticias:
Toast.makeText(getApplicationContext(),"Noticias",Toast.LENGTH_SHORT).show;
break;
case R.id. nav_sesion:
Toast.makeText(getApplicationContext(),"nav_sesion",Toast.LENGTH_SHORT).show;
break;
......
}
return false;
}
});
这就是您在导航菜单中实现点击事件的方式..