我正在关注Developer Android Guide并已搜索并找到this,this和this等问题。
我只想要一些简单的东西,当选择导航抽屉菜单中的某个项目时,打开另一个活动(或进行任何操作)。
使用FAB以正确的行为打开菜单。
但是当我点击我的模拟器它关闭但没有进入OnNavigationItemSelectedListener / onNavigationItemSelected时,我调试并且声明正在工作,但是当我点击它时不会进入侦听器。
我已经尝试在活动中实现侦听器并拆分方法,但是没有用。
也许可以提供帮助的东西,在菜单中我用红色暗示我的图标,但它们仍然是灰色的。
我已经尝试清理,重建,打开/关闭Android Studio所有这些基础知识,有时Android Studio无法处理。
XML活动
<?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"
tools:context=".screens.intro.MainActivity"
android:id="@+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="@+id/main_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/main_drawer_view"
app:headerLayout="@layout/main_drawer_header" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/main_user_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
app:srcCompat="@drawable/ic_menu_black_24dp"
android:tint="@color/icon_red"
app:backgroundTint="@color/button_white"
app:fabSize="normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//My layout/interface
</android.support.constraint.ConstraintLayout>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
XML菜单
<?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/main_drawer_home"
android:icon="@drawable/ic_home_black_24dp"
android:iconTint="@color/icon_red"
android:title="@string/home_screen" />
<item
android:id="@+id/main_drawer_heal"
android:icon="@drawable/ic_healing_black_24dp"
android:iconTint="@color/icon_red"
android:title="@string/healing" />
<item
android:id="@+id/main_drawer_chance"
android:icon="@drawable/ic_warning_black_24dp"
android:iconTint="@color/icon_red"
android:title="@string/chance_of_accident" />
<item
android:id="@+id/main_drawer_avoid"
android:icon="@drawable/ic_play_circle_outline_black_24dp"
android:iconTint="@color/icon_red"
android:title="@string/avoid_accident" />
<item
android:id="@+id/main_drawer_chart"
android:icon="@drawable/ic_pie_chart_black_24dp"
android:iconTint="@color/icon_red"
android:title="@string/accident_char" />
<item
android:id="@+id/main_drawer_about"
android:icon="@drawable/ic_info_outline_black_24dp"
android:iconTint="@color/icon_red"
android:title="@string/about" />
<item
android:id="@+id/main_drawer_sign_out"
android:icon="@drawable/ic_logout_black_24dp"
android:iconTint="@color/icon_red"
android:title="@string/sign_out" />
</group>
</menu>
Code Java
public class MainActivity extends BaseActivity {
//In Layout
//navigator drawer
private DrawerLayout mainDrawerLayout;
private NavigationView mainNavigationView;
private FloatingActionButton mainUserMenu;
@Override
protected void assignViews() {
mainDrawerLayout = findViewById(R.id.main_drawer_layout);
mainNavigationView = findViewById(R.id.main_navigation_view);
mainUserMenu = findViewById(R.id.main_user_menu);
}
@Override
protected void prepareViews() {
mainUserMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Utils.isDoubleClick()) return;
mainDrawerLayout.openDrawer(GravityCompat.START);
}
});
mainNavigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// set item as selected to persist highlight
menuItem.setChecked(true);
// close drawer when item is tapped
mainDrawerLayout.closeDrawers();
//Do something
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
return true;
}
});
答案 0 :(得分:1)
您需要在NavigationView和FrameLayout之间更改位置。
您的xml活动需要像这样。
<?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"
tools:context=".screens.intro.MainActivity"
android:id="@+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/main_user_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
app:srcCompat="@drawable/ic_menu_black_24dp"
android:tint="@color/icon_red"
app:backgroundTint="@color/button_white"
app:fabSize="normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//My layout/interface
</android.support.constraint.ConstraintLayout>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/main_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/main_drawer_view"
app:headerLayout="@layout/main_drawer_header" />
</android.support.v4.widget.DrawerLayout>