在FrameLayout中动态添加多个片段

时间:2018-02-02 17:14:40

标签: android android-layout android-fragments dynamic android-framelayout

我正在尝试使用FragmentTransaction的add()方法将我创建的多个片段添加到framelayout。每当我尝试这样做时,片段只会替换另一个。

这是我的代码:

public class FragmentMerchandSearch extends Fragment {


    public FragmentMerchandSearch() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_merchand_search, container, false);
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        for(int i = 0; i < 10; i++){
            Fragment newFragment = new FragmentMerchandPresentation();

            ft.add(R.id.container_merchand_presentation_for_search, newFragment);

        }
        ft.commit();

        return view;
    }
}

这是FragmentMerchandPresentation的代码:

public class FragmentMerchandPresentation extends Fragment {

    public FragmentMerchandPresentation(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_presentation_merchand, container, false);


        return view;
    }
}

这是fragment_merchand_search的XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/container_merchand_presentation_for_search">

        </FrameLayout>

    </LinearLayout>

</ScrollView>

fragment_presentation_merchand的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hey !"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:3)

您正在FrameLayout中添加您的片段,这意味着一个在另一个之上。

要垂直添加它们,请使用LinearLayout作为容器。

只需将fragment_merchand_search.xml更改为:

即可
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/container_merchand_presentation_for_search"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</ScrollView>

答案 1 :(得分:1)

只需更改此行:

ft.add(R.id.container_merchand_presentation_for_search, newFragment);

到此:

ft.replace(R.id.container_merchand_presentation_for_search, newFragment);