在FrameLayout中动态添加多个片段
Dynamycally add multiple fragments in 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;
}
}
这里是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);
我正在尝试使用 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;
}
}
这里是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);