什么是Fragment
Fragment代表的是在一个Activity里面用户界面的一个行为或者一个部分。可以在一个Activity中组合多个Fragment或者在多个Activity中使用同一个Fragment。Fragment拥有自己的生命周期和事件体系,但位Activity之下,所以我们可以把它看做是一个子Activity。
Fragment受限于Activity
Fragment必须嵌入在Activity,并且Fragment的生命周期受其Host Activity直接影响,也就是Activity执行onPause()、onDestroy()等,Fragment随即也会执行。但在Activity正常运作中,Fragment则独立了。
添加方式
Fragment可以添加到Activity或者Viewgroup中,也可以没有界面而为Activity服务。
创建Fragment
要创建一个Fragment,必须先创建Fragment的子类(或它的一个现有的子类)。它含有类似Activity的回调方法,如oncreate(),onstart(),onpause(),和onstop()。
生命周期如图所示,通常至少需要实现以下几种方法:
- onCreate():最先调用,执行一些初始化操作;
- onCreateView():在需要绘制界面时调用此方法,该方法需要返回一个View到该Fragment的根布局,不需要用户界面时返回null;
- onPause():当用户离开该Fragment时调用,并非destroyed,通常执行提交保存等操作,因为用户可能不再回到这个界面。
>**注意:** 通常来说,实现以上三个方法就够了,如果要实现更多方法,参见Fragment生命周期图。还有一些可能用到的父类: - DialogFragment
Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the Activity class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment. - ListFragment
Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events. - PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a “settings” activity for your application.添加用户界面
为了提供用户界面,需要实现onCreateView()方法,并且返回一个View给根布局。1
2
3
4
5
6
7
8public static class ExampleFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
参数
- R.layout.example_fragment: Fragment的布局文件
- container: 需要传递到的根布局
- false: 是否将examlep_fragment附着到container上,此处已经附着于container故为false
将Fragment添加到Activity
有两种方式可以添加:
Layout布局文件方式
1 |
|
注意: fragment 标签中
android:name
属性指定的是Fragment类,系统在解析主布局xml时,依据该属性找到onCreateView()方法,获取View。
Java代码添加到ViewGroup方式
此方法可以在Activity运作中任意添加,即动态添加。在进行添加、删除或替换操作中,为了安全需要使用事务来进行操作。
1 | // 开启事务 |
添加没有UI界面的Fragment: 调用add(Fragment, String)来添加,String标识唯一的ID来代替资源ID,并且系统将不会调用onCreate()方法,所以无须实现该方法。对于找到该Fragment,可以使用findFragmentByTag()来实现。
管理Fragment
在Activity中调用 getFragmentManager()
获得FragmentManager,其中包含方法 findFragmetById()
或者 findFragmentByTag()
用于获得Fragment,然后可以通过 popBackStack()
来弹出Fragment(模拟用户命令实现),还包含 addOnBackStackChangedListener()
方法。
addToBackStack()后允许用户点击返回键销毁该Fragment
执行其它事务操作
1 | // 创建事务 |
Fragment与Activity之间的通信
两行代码看破一切:
Fragment中访问Activity
1 | View listView = getActivity().findViewById(R.id.list); |
Activity中访问Fragment
1 | ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); |
回调法通信
1 | public static class FragmentA extends ListFragment { |
然后让Activity实现该接口以达到通知回调目的,为了确保Activity实现该接口,需要重写onAttach(activity)方法,这个方法在添加Fragment到Activity时调用,在该方法中实例化接口,如果Activity没有实现接口则会报错。然后,Fragment可以通过mListener发送消息回调到Activity了。
1 | public static class FragmentA extends ListFragment { |