Fragment
在 Android
开发中非常常用。今天,我将讲解关于Fragment
的使用
Activity
界面中的一部分,可理解为模块化的Activity
Fragment
不能独立存在,必须嵌入到Activity
中Fragment
具有自己的生命周期,接收它自己的事件,并可以在Activity
运行时被添加或删除Fragment
的生命周期直接受所在的Activity
的影响。如:当Activity
暂停时,它拥有的所有Fragment
们都暂停支持动态、灵活的界面设计
Fragment
从 Android 3.0
后引入Android 3.0
前使用 Fragment
,需要采用android-support-v4.jar
兼容包1.2可以理解为从创建到显示(或切换)
Fragment
作为Activity
一部分,所以Fragment
的使用一般是添加到Activity
中Fragment
添加到Activity
中一般有2种方法: Activity
的layout.xml
布局文件中静态添加Activity
的.java
文件中动态添加Activity
的layout.xml
布局文件中静态添加Activity
的布局文件fragment_layout_test.xml
<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" >
// 该fragment类定义在包名为"com.skywang.app"中的FragmentLayoutTest类的内部类ExampleFragment中
<fragment android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Fragment
的布局文件example_fragment.xml
<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" >
<TextView
android:text="@string/example_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Activity
的.java文件FragmentLayoutTest.java
// 在Activity使用Fragment时,需要考虑版本兼容问题
// 1. Android 3.0后,Activity可直接继承自Activity,并且在其中嵌入使用Fragment
// 2. Android 3.0前,Activity需FragmentActivity(其也继承自Activity),同时需要导入android-support-v4.jar兼容包,这样在Activity中才能嵌入Fragment
public class FragmentLayoutTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout_test);
// 设置上述布局文件
}
// 继承自Fragment
// 布局文件中的Fragment通过该FragmentLayoutTest的内部类ExampleFragment实现
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_fragment, container, false);
// 将example_fragment.xml作为该Fragment的布局文件
// 即相当于FragmentLayoutTest直接调用example_fragment.xml来显示到Fragment中
}
}
}
至此,方法1讲解完毕。
Activity
的布局文件定义1占位符(FrameLayout
)
这样做的好处是:可动态在Activity
中添加不同的 Fragment
,更加灵活fragment_transaction_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/about_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Fragment
的布局文件example_fragment.xml
<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" >
<TextView
android:text="@string/example_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Activity
的.java
文件中动态添加Fragment
FragmentTransactionTest
public class FragmentTransactionTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_transaction_test);
// 步骤1:获取FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// 步骤2:获取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 步骤3:创建需要添加的Fragment :ExampleFragment
ExampleFragment fragment = new ExampleFragment();
// 步骤4:动态添加fragment
// 即将创建的fragment添加到Activity布局文件中定义的占位符中(FrameLayout)
fragmentTransaction.add(R.id.about_fragment_container, fragment);
fragmentTransaction.commit();
}
// 继承与Fragment
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.example_fragment, container, false);
// 将example_fragment.xml作为该Fragment的布局文件
}
}
}
至此,方法2讲解完毕
Fragment
进行了全面介绍和分析Carson带你学Android:页面活动-Activity