【问题标题】:Dynamycally add multiple fragments in FrameLayoutFrameLayout中动态添加多个片段
【发布时间】:2018-07-13 06:03:00
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

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


    【解决方案1】:

    您在 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>
    

    【讨论】:

    • 非常感谢它有效!我以为我只能将片段添加到 FrameLayout。
    • 很高兴为您提供帮助。 FrameLayoutRelativeLayout 一样工作:你必须说出每个孩子的去向。否则,他们将全部在左上角。 LinearLayout 将一个置于另一个之下。如果可以,标记为答案 =)
    【解决方案2】:

    只需更改此行:

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

    到这里:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多