最近用到Fragment,又重新整理了一下,同时也准备再次开启博客之旅,希望能坚持去下。
一、引言
Fragment是Android 3.0 (API level 11)以后提出的系统组件,主要目的是在大屏幕设备上支持更加动态和灵活的UI设计,从而改善用户的体验。
Fragment中文意思为碎片or片段,简单地可以理解为是Activity(活动)的一部分,在它的上面可以显示各种控件,但是Fragment又不同于普通的View(视图),它直属于Object类,本人认为这是谷歌攻城师们抛开现有界面框架的束缚,设计的一个全新的视觉组件。
Fragment作为Activity的一部分,必须要依赖于Activity才能使用,并受到Activity的控制和影响。一个Activity中可以包括多个Fragment,这些Fragment之间既相互独立,又可以相互通信,而一个Fragment也可以被多个Activity重用,这给我们设计与开发带来了很大的灵活性。
二、生命周期
我们应该都已知道Activity有布局,有生命周期,而Fragment作为Activity的一部分,它也有自己的布局和生命周期,可以单独处理自己的输入,在Activity运行的时候可以动态地加载或者移除Fragment模块。官方文档中提供的生命周期图如下:
该流程图虽然涉及的方法众多,但是总得来讲还是比较简单,如果对Activity生命周期有很好的理解的话。在这里就不对每一个方法做阐述了,意思也是很显然的,大多数应用程序至少应该实现下面三个方法:
1.onCreate()
系统在创建Fragment的时候调用这个方法,这里应该初始化相关的组件,一些即便是被暂停或者被停止时依然需要保留的东西。
2.onCreateView()
用于创建Fragment的显示视图,当第一次绘制Fragment的UI时系统调用这个方法,返回值类型是一个View,如果Fragment不提供界面也可以返回null。
3.onPause()
当用户离开Fragment时第一个调用这个方法,需要提交一些变化,因为用户很可能一去不复回。
当然还有其它几个回调方法可应该按情况实现之。
三、派生类
1.DialogFragment
一个浮动在Activity上面的Fragment,以对话框的形式呈现。
2.ListFragment
显示一个列表控件,就像 ListActivity 类,它提供了很多管理列表的方法。
3.PreferenceFragment
是一个具有SharedPreferences功能的Fragment。
四、Fragment的使用
步骤一:添加支持库(可选)
如果您的项目支持3.0以下的版本,需要有支持库。支持库是一个提供了API库函数的JAR文件,这样就可以在旧版本的Android上使用一些新版本的API。
比如常用的android-support-v4.jar.它的完整路径是:
<sdk>/extras/android/support/v4/android-support-v4.jar.
它就提供了Fragment的API,使得在Android 1.6 (API level 4)以上的系统都可以使用Fragment。具体导入方法在此不再赘述,可以参考(http://www.cnblogs.com/kissazi2/p/3644848.html)。
此布局文件和Activity的布局文件一样,根据需要设计好界面,假设文件名为flagment_layout.xml,比如:
<span style="font-size:14px;"><span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#112233"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#ff0000"
android:layout_marginTop="50dp"
android:text="这是我的第一个Fragment" />
</LinearLayout></span>
</span>
步骤三:自定义Fragment类
此类需要继承Fragment类,同时重写相应的方法。假设文件保存为MyFlagment.java,代码如下:
<span style="font-size:12px;">package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.flagment_layout, container, false);
return view;
}
@Override
public void onPause() {
super.onPause();
}
}</span>
步骤四:添加Fragment并显示
这里有2种方法显示前面定义好的Fragment,一种是通过Activity布局中使用<fragment>标签,另一种是在代码中把Fragment对象添加到指定的ViewGroup中,具体实现如下。
1.XML布局方式
<span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下面是Fragment" />
<fragment
<span style="color:#FF0000;">android:name="com.example.fragment.MyFragment"</span>
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</span>
其中android:name属性填上自定义fragment的完整类名。当系统创建这个Activity的布局文件时,系统会实例化每一个fragment,并且调用它们的onCreateView()方法,来获得相应fragment的布局,并将返回值插入fragment标签所在的地方。2.代码方式
在这里可以在Activity的布局文件中定义一个容器布局(ViewGroup),例如是一个FrameLayout,指定一个ID,在代码中用于存放Fragment。
<span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下面是Fragment" />
<span style="color:#FF0000;"><FrameLayout
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/></span>
</LinearLayout></span>
Activity定义如下:
<span style="font-size:12px;">package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();//如果不用支持库则getFragmentManager获取对象
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment fragment = new MyFragment();
fragmentTransaction.add(<span style="color:#FF0000;">R.id.frameLayout</span>, fragment);
fragmentTransaction.commit();
}
}</span>
这里要注意的是,如果要支持3.0以下版本,相关的类都必须是支持库所在包中的,其中Activity要继承自FragmentActivity(如果是3.0以上版本直接可以继承Activity)。FragmentManager能够实现管理Activity中fragment,通过调用activity的getFragmentManager() 或 getSupportFragmentManager()取得它的实例。 FragmentTransaction表示事务,能对fragment进行添加、移除、替换、以及执行其他动作。
本次实验最终效果如下: