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

时间:2015-09-29 22:22:57

标签: android android-fragments android-viewpager fragmentpageradapter fragmentmanager

我有一个由4个标签组成的视图寻呼机。每个标签都有自己的片段。

我如何能够使用片段事务将标签3中的片段替换为新片段?

我已经尝试了很多东西,但我根本无法提出解决方案。我试过在google和stackoverflow上四处寻找,但没有成功。

1 个答案:

答案 0 :(得分:1)

我认为你的片段有一个放在中心的按钮。通过单击它,您可以更改此按钮下的布局。您提到的第一个片段的内容/布局应该用wrapperA替换,第二个片段的内容/布局应该用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直接更改片段: Replace fragment with another fragment inside ViewPager