用多个片段替换单个片段

时间:2017-08-11 14:51:53

标签: android android-fragments android-fragmentactivity fragmentmanager

我有一个控制多个片段的活动(导航控制器)。用户看到的初始屏幕是放在一个容器中的单个片段。当我单击一个按钮到另一个片段时,我需要将第一个片段添加到backstack,并将两个新片段添加到不同于第一个片段的容器中。这两个片段在顶部是一个地图片段,在底部是一个配置文件细节片段这是否可能?

代码如下。

导航控制器主页片段(这是应用启动时的主屏幕):

public void home(Bundle savedInstanceState){
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }
        // Create a new Fragment to be placed in the activity layout
        CurrentFriendsFragment firstFragment = new CurrentFriendsFragment();
        // Add the fragment to the 'fragment_container' FrameLayout
        fragmentTransaction
                .setCustomAnimations(R.animator.fade_in, R.animator.fade_out)
                .replace(R.id.fragment_container, firstFragment, "firstFragment")
                .commit();
    }
}

导航控制器配置文件片段:

@Override
public void onProfileButtonClicked() {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState1 != null) {
            return;
        }

        MapFragment mapFragment = new MapFragment();
        // Create a new Fragment to be placed in the activity layout
        ProfileFragment profileFragment = new ProfileFragment();
        // Add the fragment to the 'fragment_container' FrameLayout
        fragmentTransaction
                .setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up, R.animator.slide_in_down, R.animator.slide_out_down)
                .add(R.id.fragment_container_bottom_large, profileFragment)
                .add(R.id.fragment_container_top_small, mapFragment)
                .commit();

    }
}

这是xml文件的主要活动。注意三个不同的容器。 " fragment_container"对于全屏片段," fragment_container_top_small"," fragment_container_bottom_large"在一页上有多个片段。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/activity_navigation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.parse.starter.ViewControllers.NavigationController"
    android:background="@color/palette_darkwhite">

    <include layout="@layout/tool_bar"
        android:id="@+id/toolbar_layout" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/toolbar_layout"
        android:layout_above="@+id/bottom_navigation_navbar">

        <RelativeLayout
            android:id="@+id/fragment_container_top_small"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </RelativeLayout>

        <FrameLayout
            android:id="@+id/fragment_container_bottom_large"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3">
        </FrameLayout>

    </LinearLayout>

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar_layout"
            android:layout_above="@+id/bottom_navigation_navbar">
        </FrameLayout>

    <FrameLayout
        android:id="@+id/fragment_container_popup"
        android:layout_width="275dp"
        android:layout_height="275dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_navbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/palette_lightwhite">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center_horizontal"
            android:background="@null"
            app:srcCompat="@drawable/collpaseup" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical">

            <LinearLayout android:id="@+id/thumbnail2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="3dp"
                android:layout_alignParentLeft="true"
                android:layout_marginRight="5dp">


                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/profile_picture_navbar"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center_vertical"
                    app:civ_border_color="#FF000000"/>

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/thumbnail2"
                android:layout_toRightOf="@+id/thumbnail2"
                android:textColor="@color/palette_primarycolor"
                android:id="@+id/nameLabel"
                android:text="Name"
                android:textSize="15sp"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/nameLabel"
                android:layout_toRightOf="@+id/thumbnail2"
                android:textColor="@color/palette_primarycolor"
                android:id="@+id/locationLabel"
                android:text="Location"
                android:textSize="12sp"
                android:textStyle="bold"/>

        </RelativeLayout>

    </android.support.design.widget.BottomNavigationView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

有3个容器并使用View.GONE使第1个消失,然后使用View.VISIBLE使其他2个出现。

相关问题