android - 导航栏中的项目未显示

标签 android android-xml

我的 Activity 有一个导航栏菜单。问题是,每当我从导航栏中选择一个 fragment 时,标题都会正确更改,但相应的页面不会显示。我猜我的 xml 文件有问题。在这里发布我的 xml 文件。导航栏中的 fragment 从另一个 Activity 中正确加载。请帮忙。

<?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:id="@+id/drawer_layout_machine_category_child"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/forgotPassword"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MachineCategoryActivity">

            <include layout="@layout/content_main" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/machine_category_recyclerview"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@color/colorAccent"
                android:elevation="3dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/other_category_machine_recyclerview"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/colorAccent"
                android:elevation="3dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/machine_category_recyclerview" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view_machine_category"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

提前致谢。

最佳答案

我假设您有 3 个 fragment ,并且您的 content_main 包含一个 CoordinatorLayout。 在您的 MainActivity.java 中:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    View coordinatorLayout = findViewById(R.id.main_coordinator_layout);
    coordinatorLayout.setVisibility(View.VISIBLE);

 //first fragment
        if (id == R.id.fragment_one) {
        fragment = new FragmentOne();

//second fragment
        } else if (id == R.id.fragment_two) {
        fragment = new FragmentTwo();

 //third fragment
        } else if (id == R.id.fragment_three) {
        fragment = new FragmentThree();
   }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer != null) {
        drawer.closeDrawer(GravityCompat.START);
    }

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container, fragment);
    ft.commit();

    return true;
  }

只需将 fragment 的名称和 ID 替换为您的即可。

关于android - 导航栏中的项目未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57338671/

相关文章:

php - 在 PHP 中更新 JSON 数组

android - 如何从左到右工具栏api 16

Android XML 有没有办法将工具命名空间与自定义属性一起使用?

android - 以编程方式更改图层列表中形状的颜色

android - 如何从 Android SDK 中内置的 Eclipse UI 发出 adb 命令

android - 在外部应用程序中启动 Activity 是否有区别(并且是否有首选方法)?

java - Android手持设备和Wear通信不一致

Android 服务不是从 Activity 开始的

android - 异构网格布局

java - 向 java 类添加 2 个实现?