案例
fragment_f1.xml碎片布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="F1" />
</LinearLayout>
fragment_f2.xml碎片布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="F2" />
</LinearLayout>
F1碎片java文件
package com.hzj163.myfragmentdynamic;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class F1 extends Fragment {
View view;
public F1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_f1, container, false);
}
Log.i("hzj", "f1:onCreateView");
return view;
}
}
F2碎片java文件
package com.hzj163.myfragmentdynamic;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class F2 extends Fragment {
View view;
public F2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.fragment_f2, container, false);
}
Log.i("hzj", "f2:onCreateView");
return view;
}
}
activity_main.xml活动布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<!--加载碎片的ViewGroup-->
<LinearLayout
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn1"
android:text="F1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btn2"
android:text="F2" />
</LinearLayout>
</LinearLayout>
MainActivity活动java文件
package com.hzj163.myfragmentdynamic;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
F1 f1;
F2 f2;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f1 = new F1();
f2 = new F2();
fragmentManager = getSupportFragmentManager();
openFragment(f1, "f1");
}
/**
* @param fragment 碎片对象
* @param tag 该碎片的标签
*/
private void openFragment(Fragment fragment, String tag) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.linear1, fragment, tag);
//添加到返回按键堆栈,这样就可以执行碎片的第二条周期,也就是不会每次replace的时候都去执行onAttach
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
public void btn1(View view) {
openFragment(f1, "f1");
}
public void btn2(View view) {
openFragment(f2, "f2");
}
}