Android--Fragment(1)——比Activity小一级的组件

一、Fragment概述

1.Fragment可以作为Activity界面的一部分组成出现;

2.可以在一个Activity中同时出现多个Fragment,并且一个Fragment也可以在多个Activity中使用;

3.在Activity运行过程中,可以添加、移除或替换Fragment;

4.Fragment可以响应自己的输入事件,并且有自己的声明周期,它们的生命周期会受宿主Activity的生命周期影响。

二、如何创建一个Fragment?

(1)必须创建一个Fragment的子类,或继承自已经存在的某个Fragment子类;

导入的包是import android.app.Fragment; 而不是 //import android.support.v4.app.Fragment; 后者在Debug时有问题。

(2)重写onCreateView()方法中加载属于Fragment自己的布局文件。Fragment第一次绘制它的用户界面的时候,系统会调用此方法,为了绘制Fragment的UI,此方法必须返回一个View,如果不显示,返回null即可。

package com.example.fragmenttest;

import android.app.Fragment;
//import android.support.v4.app.Fragment;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//layout布局文件转View对象
		/**
		 * 参数1:Fragment需要加载的布局文件
		 * 参数2:加载layout的父ViewGroup,要想把fragment添加到Activity中,
		 *      需要把View对象放入ViewGroup中来,所以这里需要给它传递一个ViewGroup对象
		 * 参数3:false——不返回父ViewGroup
		 */
		View view= inflater.inflate(R.layout.fragment, container, false);
		TextView text=(TextView) view.findViewById(R.id.text);
		text.setText("静态加载Fragment");
		return view;
	}

}

 //在Activity的类中,可以直接访问当前Activity所包含的Fragment的下的组件的ID


三、Fragment的加载方式

1.静态加载

在Activity的layout文件中声明Fragment,需要特别注意的是<fragment>中的android:name属性指定了在layout中实例化的Fragment类。

标识Fragment的方法:android:id属性提供一个唯一ID(必须加ID,否则报错)、 android:tag属性提供一个唯一字符串。

Activity与Fragment的逻辑关系:

Activity的onCreate()中调用setContentView(R.layout.main2); //main2.xml中含有fragment标签,其android:name属性中指定了Fragment派生类对象名,在Fragment派生类的onCreateView()中inflater.inflate(R.layout.fragment, container, false)中指定了对应的fragment.xml。


2.动态加载

(1)撰写代码将Fragment添加到一个Activity layout中

    add():添加一个Fragment(指定要添加的fragment和要插入的View),与次类似的还有remove()、替换()

(2)要动态加载fragment需处理Fragment事务:根据用户的交互情况,对Fragment进行添加、移除、替换、以及执行其他动作,提交给Activity的每一套变化被称为一个事务。

获取Fragment事务的方法:

FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); //开启一个事务,在事务里去添加、移除fragment等动作
 每一个事务都是同时执行一套变化,可以再一个事务中设置你所有想执行的变化,包括add()、remove()、replace(),然后提交给Activity,必须调用commit()方法。

 如果允许用户通过按下BACK按键返回到前一个Fragment状态,调用commit()之前可以加入addToBackStack(null)方法。
动态添加

fragment:  beginTransaction.add(R.id.frame,fragment2);   //R.id.frame对应要加入的Activity的XML文件的某个标签的id,如<LinearLayout   android:id="@+id/frame" > </LinearLayout>  ,fragment2是创建的Fragment子类对象,如静态加载一样,在onCreateView()方法中会指定fragment2对应的Layout XML文件。


实例代码

MyFragment2 fragment2=new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.frame, fragment2);
beginTransaction.addToBackStack(null);
beginTransaction.commit();

四、Fragment的生命周期

1.void onAttach(Activity activity) //当Fragment被添加到Activity的时候会回调这个方法,并且只调用一次;

2.void onCreate(Bundle savedInstanceState)  //创建Fragment时会回调,只会调用一次;

3.View onCreateView(***)  //每次创建都会绘制Fragment的View组件时回调该方法

4.void onActivityCreated(Bundle savedInstanceState) //Fragment所在的Activity启动完成后调用

5.void onStart()  //启动Fragment

6.void onResume() //回复Fragment时会回调,调用onStart()方法后面一定会调用onResume()方法。

7.void onPause() //暂停Fragment

8.void onStop() //停止Fragment

9. void onDestroyView()  //销毁Fragment所包含的View组件时调用,与onCreateView(***)对应

10.void Destroy() //销毁Fragment时会被回调

11.void onDetach()  //与onAttach()对应,Fragment从Activity中删除时会回调该函数,并且这个方法只会调用一次


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章