在 FrameLayout 中动态添加多个片段

Nof*_*fix 1 android dynamic android-layout android-fragments android-framelayout

我正在尝试使用 FragmentTransaction 的 add() 方法将我正在创建的动态多个片段添加到框架布局中。每次我尝试这样做时,片段只是相互替换。

这是我的代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 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>
Run Code Online (Sandbox Code Playgroud)

和 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>
Run Code Online (Sandbox Code Playgroud)

Edu*_*zer 6

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

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

只需将您的更改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>
Run Code Online (Sandbox Code Playgroud)