簡體   English   中英

動態地在FrameLayout中添加多個片段

[英]Dynamycally add multiple fragments in FrameLayout

我正在嘗試使用FragmentTransaction的add()方法將正在創建的多個片段動態地添加到幀布局中。 每次我嘗試這樣做時,Fragments都在相互替換。

這是我的代碼:

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>

您正在將片段添加到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>

只需更改此行:

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

對此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM