导航栏项目未显示

3

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

<?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>

感谢你的提前帮助。

嘿,能否请您发布处理选择菜单项时片段更改的代码? - Pabi Moloi
1个回答

0

我假设你有3个片段,并且你的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;
  }

只需将片段的名称和 ID 替换为您自己的即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接