导航栏中的项目未显示

时间:2019-08-03 13:16:43

标签: android android-xml

我的活动上有一个导航栏菜单。问题是,每当我从导航栏中选择一个片段时,标题都会正确更改,但是不会显示相应页面。我猜我的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>

谢谢。

1 个答案:

答案 0 :(得分: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替换片段的名称和ID。