Fragment

来源:互联网 发布:淘宝叶子类目是什么 编辑:程序博客网 时间:2024/04/29 14:11

一、 什么是Fragment

Fragment是一种可以嵌入在Activity中的 UI 片段,它能让程序更加合理和充分

地利用大屏幕的空间,因而在平板和目前的大屏手机上应用的非常广泛(只要屏幕大,都会用到Fragment)

     一个碎片在一个活动中代表一个行为或用户界面的一部分。 你可以在一个单一的活动中组合使用多个碎片以建立一个多窗格的UI,并且可以在多个活动中重用一个碎片。你可以认为是一个拥有独立生命周期、能够独立接受输入事件、并且可以在活动运行时添加或移除的碎片作为一个活动的模块化部分(有点像一个你可以在不同活动中重用的子活动)。

       Activity的频繁的创建和销毁比较消耗时间,使用Fragment可以避免这种情况。

Fragment轻量级的Activity: Fragment不需要在清单文件中注册。

二、 静态使用Fragment

这是使用Fragment最简单的一种方式,把Fragment当成普通的控件,直接写在Activity的布局文件中。步骤:

1、 在项目的资源中增加fragment的布局文件

2、 继承Fragment,重写onCreateView决定Fragemnt的布局

3、 在Activity的布局中添加Fragment(与添加普通的控件类似)。此处必须指明Fragment代表的哪个具体的Fragment类。(class属性)

注意:

1、 由于Fragment3.0新增,所以如果想兼容3.0以前的版本Fragment要使用V4包中的,使用FragmentActivity也要应用v4包中的FragmentActivity

2、 FragmentonCreateView必须实现,这个方法的返回视图决定了这个Fragment的显示视图。

 

3LayoutInflater对象的inflate()方法:        

    inflate()方法一般接收两个参数,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null

    inflate()方法还有个接收三个参数的方法重载

1. 如果rootnullattachToRoot将失去作用,设置任何值都没有意义。2. 如果root不为nullattachToRoot设为true,则会在加载的布局文件的最外层再嵌套一层root布局。3. 如果root不为nullattachToRoot设为false,则root参数失去作用。4. 在不设置attachToRoot参数的情况下,如果root不为nullattachToRoot参数默认为true

        所以在使用LayoutInflater填充布局的时候,要注意inflate()方法的参数。如果是两个参数,则第二个参数可以采用null;如果使用三个参数的方法,则要注意参数之间的搭配。

 

 

public class Fragment1 extends Fragment {

//覆写父类的一个方法:

/**

 * 该方法的返回值,就是这个fragment要显示的视图

 */

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) { 

//参数1:布局文件  参数二:容纳Fragment的容器  参数三:

/*参数三如果是true则返回参数那个容器

 * 参数三是false则返回R.layout.fragment1_layout的根标签

 * 

 * 如果参数二不是null,有什么作用?

 * 可以让R.layout.fragment1_layout里面的根标签的宽高属性有效

 * 如果参数二是null,则R.layout.fragment1_layout里面的根标签的宽高属性无效,参数三没有任何的作用。

 */

View view = inflater.inflate(R.layout.fragment1_layout, container, false);  

Button btn = (Button) view.findViewById(R.id.button1);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//getActivity()获得拥有这个Fragment的那个Activity对象

Toast.makeText(getActivity(), "被点击了", Toast.LENGTH_SHORT).show();

}

});

return view;

}

 

 

}

 

三、 动态使用Fragment

静态使用Fragment仅仅是Fragment最简单的功能而已。Fragment真正的强大之处在于可以动态地添加到Activity当中,在程序运行时向Activity添加Fragment,程序的界面就可以定制的更加多样化。

 

事务:

 

1、 创建Fragment(如果想兼容3.0以前的版本,也必须使用V4包中的)

2、 使用ActivitygetFragmentManager()获得FragmentManager对象.(如果是使用的v4包中的FragmentActivity,则调用getSupportFragmentManager()来获得FragmentManager对象)

3、 获得事务对象:FragmentTransaction transaction = manager.beginTransaction();

4、 调用事务的一系列方法:

transaction.add(containerViewId, fragment, tag);  //在指定的容器中添加Fragment

transaction.remove(fragment) ;//移除指定的fragment

transaction.replace(containerViewId, fragment, tag)//替换掉指定容器中的fragment  。相当于先removeadd

5、 如果想按下返回键之后能够回到替换前的状态,则可以把fragment添加到Back栈中。transaction.addToBackStack(name);

6、 最后提交事务(这步必须做)transaction.commit();  //只有这个方法执行之后上述动作才可以生效。

 

 

四、 Fragment的生命周期

Fragment有自己独立的生命周期,但是它有是依托于Activity的,所以Fragment的生命周期直接受Activity的影响。理解Fragment的生命周期非常重要

 

 

 

Fragment存在如下状态:

1、运行状态   当前Fragment位于前台,用户可见,可以获得焦点

2、暂停状态   其他Activity位于前台,该Fragment依然可见,只是不能获得焦点

3、停止状态   该Fragment不可见,失去焦点

4、销毁状态    该Fragment完全被删除,或该Fragment所在的Activity被结束(Fragment被销毁后完全不可用,官方文档并没有提到这个状态)

 

在fragment的生命周期中,如下方法会被系统回调。

1onAttach()   当该Fragment被添加到Activity时被回调。该方法只会被调用一次。

2onCreate()    创建Fragment时被回调。该方法只会被回调一次

3onCreateView()   每次创建、绘制该FragmentView组件时回调该方法,Fragment会显示该方法返回的View对象

4onActivityCreated()  Fragment所在的Activity被启动完成后回调该方法。ActivityonCreate()方法返回之后调用该方法。

5onStart()  启动Fragment时被回调

6onResume()   恢复Fragment时回调该方法,在onStart()方法后一定会回调该方法。

7onPause()  暂停Fragment时回调该方法

8onStop()    停止Fragment时被回调

9onDestroyView()   销毁该Fragment所包含的View组件时调用

10onDestroy()   销毁Fragment时被回调

11onDetach()   将该FragmentActivity中删除、替换完成时回调该方法,在onDestroy()方法后一定会回调onDetach()方法。该方法只会被回调一次。

 

 

通常情况下会实现如下三个方法:

onCreate()

The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

创建fragment的时候,系统会调用这个方法.在你实现过程中,fragment暂停(pause),停止(stop)然后恢复(resume),你应该初始化你想要保持的,fragment的必要的组件.

onCreateView()

The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

fragment第一次绘制他的用户界面的时候系统会调用这个方法.如果你想为你的fragment绘制界面,你必须从这个方法中返回一个View,这个View是你fragment布局的基础.如果这个Fragment不提供UI,你可以返回空.

onPause()

The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

系统调用这个方法作为用户离开这个fragment的第一标志(虽然这不总是意味着这个Fragment被摧毁了).通常是你需要做一些改变,这些改变超出了当前的用户会话(因为用户有可能不会回到这个界面来).

 

 

 

五、 ActivityFragment之间传值

5.1 分别获取到对方

1、 Fragment获取它所在的Activity调用FragmentgetActivity()方法,即可返回它所在的Activity

2、 Activity获取它包含的Fragment:调用Activity关联的FragmentManagerfindFragmentById(ind id)findFragmentByTag(String tag)方法即可获取指定的Fragment

注意:id或者tag都是在布局文件中是使用<fragment … />标签时指定的。

5.2 传递数据

1、 ActivityFragment传递数据:Activity中创建Bundle数据包,并调用Fragment对象的setArguments(Bundle bundle)方法即可将Bundle数据包传给Fragment。在Fragment中使用getArguments()方法来获得Activity传入的值。

2、 FragmentActivity传递数据或Activity需要在Fragment运行中进行实时通信:Fragment中定义一个内部回调接口,再让包含该FragmentActivity实现该回调接口,这样Fragment即可调用该回调方法将数据传递给Activity

六、 Fragment的一些常用子类

6.1 ListFragment

1、 ListFragmentFragment的子类,是一个可以显示列表的Fragment。默认包含一个ListView控件。因此,在ListFragment对应的布局文件中,必须指定一个 android:id 为 “@android:id/list” 的ListView控件! 若用户需要修改list view的,可以在onCreateView(LayoutInflater, ViewGroup, Bundle)中进行修改。当然,用户也可以在ListFragment的布局中包含其它的控件。

2、 由于内部有ListView,所以需要给ListView来绑定数据。注意:是调用ListFragment.setListAdapter()方法来绑定数据,而不是使用ListView.setAdapter() 或其它方法!

3、 使用ListFragment一般覆写他的onCreate()方法和onCreateView()方法。在onCreate()方法中加载数据,在onCreateView ()方法中返回这个ListFragment的视图

注意:1、在ListFragment的视图中至少有一个id是:@android:id/listListView,来作为ListFragment的显示列表

2、可以添加一个id@id/android:emptyTextView,那么当ListView的数据时空的时候,这个TextView会自动显示,当ListVIew有数据的时候,这个TextView会自动隐藏。

3、其余控件根据业务需要随意添加。

 

6.2 DialogFragment

1、 DialogFragmentandroid 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。

2、 使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其生命周期,它和Fragment有着基本一致的生命周期。且 DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragment(可以在大屏幕和小屏幕显示出不同的效果)。

注意:DialogFragment产生之前,我们创建对话框:一般采用AlertDialog但是从3.0之后,官方不推荐直接使用AlertDialog创建对话框,而是推荐使用DialogFragment。使用FragmentManager管理对话框,可使用更多配置选项来显示对话框。另外,如果设备发送旋转,独立配置使用AlterDialog会在旋转后消失,而配置在FragmentAlterDialog则不会有此问题。

 

使用DialogFragment的步骤:

1、 创建一个类来继承DialogFragment

2、 覆写onCreateDialog()这个方法返回一个Dialog对象。可以在这个方法内部创建AlertDialog,然后并返回。

3、 在Activity中通过FragmentManager在屏幕上显示对话框。dialogFragment.show(FragmentManager, "aa");

 

 

 

 

1 0
原创粉丝点击