如何用 ViewPager 中的另一个片段替换 Android 片段?

How to replace an Android fragment with another fragment inside a ViewPager?

我有一个包含 4 个选项卡的视图寻呼机。每个选项卡都有自己的片段。

我如何才能使用片段事务将选项卡 3 中的片段替换为新片段?

我已经尝试了很多东西,但我一直无法想出一个解决方案。我试过查看 google 和 Whosebug,但没有成功。

我假设您的片段有一个位于中心的按钮。通过单击它,您可以更改此按钮下方的布局。您提到的第一个片段的 content/layout 应替换为 wrapperA,第二个片段的 content/layout 应替换为 wrapperB。我给 wrapperA 放了一个简单的红色背景来和 wrapperB 区分开来,wrapperB 也是绿色的,同理。我希望这就是你想要的:

public class SwitchingFragment extends Fragment {

    Button switchFragsButton;
    RelativeLayout wrapperA, wrapperB;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.layout_switch, container, false);
        wrapperA = (RelativeLayout) rootView.findViewById(R.id.wrapperA);
        wrapperB = (RelativeLayout) rootView.findViewById(R.id.wrapperB);

        switchFragsButton = (Button) rootView.findViewById(R.id.switchFragsButton);
        switchFragsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (wrapperB.getVisibility() == View.GONE) {
                    wrapperB.setVisibility(View.VISIBLE);
                    // There is no need to change visibility of wrapperA since it stays behind when wrapperB is visible.
                    // wrapperA.setVisibility(View.GONE);
                }
                else {
                    wrapperB.setVisibility(View.GONE);
                    // Again, there is no need.
                    // wrapperA.setVisibility(View.VISIBLE);
                }
            }
        });

        return rootView;
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical">

    <!-- The content of first fragment should be in "wrapperA". -->
    <RelativeLayout
        android:id="@+id/wrapperA"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"
        android:visibility="visible">

    </RelativeLayout>

    <!-- The content of second fragment should be in "wrapperB". -->
    <RelativeLayout
        android:id="@+id/wrapperB"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/green"
        android:visibility="gone">

    </RelativeLayout>

    <!-- As I said, I assume that the layout switcher button is stable
         and so it should be in front of the switching layouts. -->
    <Button
        android:id="@+id/switchFragsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@color/black"
        android:text="Switch"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/white"/>

</RelativeLayout>

或者,您可以尝试通过通知您的 FragmentPagerAdapter 直接更改片段,如 link 中所述: Replace fragment with another fragment inside ViewPager