public class FragmentActivity extends Activity {
List<Fragment> list = new ArrayList<Fragment>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
list.add(new Fragment1());
list.add(new Fragment2());
FragmentManager fm = getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
for (int i = 0;i < list.size(); i++)
ft.add(R.id.frame, list.get(i));
ft.commit();
((Button)findViewById(R.id.btn1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
ft.replace(R.id.frame, list.get(0));
ft.commit();
}
});
((Button)findViewById(R.id.btn2)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
ft.replace(R.id.frame, list.get(1));
ft.commit();
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2" />
</LinearLayout>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</LinearLayout>
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_framgment1, null);
return view;
}
}
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_framgment2, null);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5EFF00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="fragment1" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0D79FF">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="fragment2" />
</RelativeLayout>