android - 导航栏中的项目未显示
问题描述
我的活动有一个导航栏菜单。问题是,每当我从导航栏中选择一个片段时,标题都会正确更改,但相应的页面不会显示。我想我的 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>
提前致谢。
解决方案
我假设您有 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 替换为您的名称即可。
推荐阅读
- google-chrome - IIS 项目 ASP.NET Core 上本地网络中的自证书 - 不安全(net::ERR_CERT_COMMON_NAME_INVALID)
- sql-server - 无法从 Visual Studio 中的 SSMS 正确打开基于 SSIS 包的作业
- javascript - 如何仅处理弹出窗口
使用柏树的按钮? - swashbuckle - OSS 规范中的服务器元素
- javascript - 如何定期检查状态并与请求同步?
- ssl - 使用 HAProxy 入口控制器时,TLS 握手间歇性失败
- c - 为什么 glibc 中 x86 的读写屏障不使用 __volatile asm?
- javabeans - 服务任务中的 Flowable delegateExpression 不起作用
- microsoft-graph-api - 如何从 OneNote API 中检索按标签过滤的页面?
- r - R furrr:在运行计算之前对每个未来进程的 API 进行身份验证