底部导航栏切换Fragment异常:仅显示HomeFragment无法切换到NotificationsFragment
问题描述
在Android项目中使用BottomNavigationView实现Fragment切换功能,MainActivity中的replaceFragment函数及导航监听逻辑已编写,但点击底部导航的通知按钮时,页面始终停留在HomeFragment,无法切换到NotificationsFragment。
相关代码
MainActivity中的replaceFragment函数
private fun replaceFragment(fragment: Fragment){ val fragmentManager = supportFragmentManager val fragmentTransaction = fragmentManager.beginTransaction() fragmentTransaction.replace(R.id.frameLayout,fragment) fragmentTransaction.commit() Log.e("navR","hereeee!!!!333") }
MainActivity的onCreate方法代码
replaceFragment(HomeFragment()) binding.bottomNavigationView.setOnItemSelectedListener { when(it.itemId){ R.id.home_page-> { replaceFragment(HomeFragment()) Log.e("nav","Home") } R.id.notifications_page-> { replaceFragment(NotificationsFragment()) Log.e("nav","Notifications") } else->{} } true }
MainActivity的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" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <!-- Main content --> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_marginStart="0dp" android:layout_marginTop="0dp" android:layout_marginEnd="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="67dp" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/bottom_nav" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="0dp" android:layout_height="0dp" android:layout_above="@+id/bottomNavigationView" android:layout_below="@+id/toolbar" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/toolbar"> </FrameLayout> </RelativeLayout> <!-- Navigation view --> <com.google.android.material.navigation.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/side_nav" app:menu="@menu/nav" /> </androidx.drawerlayout.widget.DrawerLayout>
截图说明
- 截图1:当前正常显示HomeFragment的页面
- 截图2:点击底部导航的通知按钮后,页面仍停留在HomeFragment(预期应显示NotificationsFragment)
解决方案
1. 检查底部导航菜单的ID匹配
打开res/menu/bottom_nav.xml,确认通知项的android:id是否为@+id/notifications_page,必须与MainActivity监听逻辑中的R.id.notifications_page完全一致。如果菜单中的ID不同,修改为匹配的ID即可。
2. 修复FrameLayout的布局参数
父布局是RelativeLayout,但FrameLayout同时使用了ConstraintLayout的约束属性(app:layout_constraintXXX),且宽高设为0dp,这会导致Fragment容器尺寸异常。修改FrameLayout的代码如下:
<FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottomNavigationView" android:layout_below="@+id/toolbar" android:layout_alignParentStart="true" android:layout_alignParentEnd="true"> </FrameLayout>
3. 验证NotificationsFragment的正确性
检查NotificationsFragment的布局文件是否存在内容,避免因布局为空导致切换后视觉上无变化。同时确认Fragment的构造函数及初始化逻辑无异常。
内容的提问来源于stack exchange,提问作者user24936837
相关产品推荐
相关产品推荐

