Android Studio中向Fragment容器添加子Fragment的实现问题
问题描述
我有一个带底部导航栏的Activity,导航栏由Fragment构成。点击「Friends」Fragment时,需要在其顶部添加两个切换标签(「Pending requests」和「Your Friends」),实现效果如下:
- 「Add friends」使用原Friends Fragment的布局(包含SearchBar和RecyclerView)
- 「Pending requests」和「Your Friends」使用自定义新布局
当前遇到的问题:切换到「Pending requests」页面时,原Friends Fragment的SearchBar仍然显示,不符合预期。尝试过在Friends Fragment中添加FragmentContainer承载新Fragment,但未解决问题。
当前代码
Friends.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/fragment_friends" tools:context=".Fragments.UsersFragment" android:background="@color/purple_500"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/addfriends" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Add friends" /> <Button android:id="@+id/pending" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Pending requests" /> <Button android:id="@+id/yourfriends" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Your Friends" /> </LinearLayout> <androidx.appcompat.widget.SearchView android:layout_width="350dp" android:layout_height="wrap_content" android:id="@+id/searchView" android:layout_marginTop="60dp" android:layout_marginStart="25dp" android:iconifiedByDefault="false" app:defaultQueryHint="Search Here" android:layout_alignParentTop="true" android:background="@drawable/rounded_buttons" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="120dp" > <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/item" /> </RelativeLayout> </FrameLayout>
FriendsFragment.java 核心代码
Button Badd = (Button) v.findViewById(R.id.addfriends); Button Bpending = (Button) v.findViewById(R.id.pending); Button Bfriends = (Button) v.findViewById(R.id.yourfriends); Badd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment friendsfragment = new FriendsFragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_friends, friendsfragment, "findThisFragment") .addToBackStack(null) .commit(); } }); Bpending.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment pendingFragment = new PendingFragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_friends, pendingFragment, "findThisFragment") .addToBackStack(null) .commit(); } }); Bfriends.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment yourFriendsFragment = new YourFriendsFragment(); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_friends, yourFriendsFragment, "findThisFragment") .addToBackStack(null) .commit(); } });
解决方案
1. 重构FriendsFragment的布局(仅保留顶部标签栏+子Fragment容器)
将原FriendsFragment的布局拆分为容器布局,只保留顶部的三个切换按钮,下方使用FragmentContainerView作为子Fragment的承载容器:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/purple_500" tools:context=".Fragments.FriendsFragment"> <!-- 顶部切换标签栏 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/addfriends" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Add friends" /> <Button android:id="@+id/pending" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Pending requests" /> <Button android:id="@+id/yourfriends" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Your Friends" /> </LinearLayout> <!-- 子Fragment容器 --> <androidx.fragment.app.FragmentContainerView android:id="@+id/friends_child_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
2. 新建AddFriendsFragment(复用原FriendsFragment的布局)
创建AddFriendsFragment,将原FriendsFragment中的SearchBar和RecyclerView布局移到这个新Fragment的布局文件中(命名为fragment_add_friends.xml):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/purple_500"> <androidx.appcompat.widget.SearchView android:id="@+id/searchView" android:layout_width="350dp" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:layout_marginStart="25dp" android:iconifiedByDefault="false" app:defaultQueryHint="Search Here" android:background="@drawable/rounded_buttons" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="120dp"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/item" /> </RelativeLayout> </FrameLayout>
3. 修改FriendsFragment的Java代码(使用子Fragment管理器)
使用getChildFragmentManager()替代getActivity().getSupportFragmentManager()来管理内部子Fragment,避免干扰Activity的Fragment栈,同时初始加载默认显示AddFriendsFragment:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_friends, container, false); Button Badd = v.findViewById(R.id.addfriends); Button Bpending = v.findViewById(R.id.pending); Button Bfriends = v.findViewById(R.id.yourfriends); // 初始加载AddFriendsFragment if (savedInstanceState == null) { getChildFragmentManager().beginTransaction() .replace(R.id.friends_child_container, new AddFriendsFragment()) .commit(); } Badd.setOnClickListener(v1 -> { getChildFragmentManager().beginTransaction() .replace(R.id.friends_child_container, new AddFriendsFragment()) .commit(); }); Bpending.setOnClickListener(v1 -> { getChildFragmentManager().beginTransaction() .replace(R.id.friends_child_container, new PendingFragment()) .commit(); }); Bfriends.setOnClickListener(v1 -> { getChildFragmentManager().beginTransaction() .replace(R.id.friends_child_container, new YourFriendsFragment()) .commit(); }); return v; }
4. 保留PendingFragment和YourFriendsFragment的自定义布局
这两个Fragment的自定义布局保持不变,它们会被加载到friends_child_container中,不会显示原SearchBar。
内容的提问来源于stack exchange,提问作者Daiana
相关产品推荐
相关产品推荐

