1,Fragment的作用
Fragment作为Activity的一部分有自己的生命周期方法,但是会受Activity的影响。主要作用可以用来做屏幕适配,一套代码可以用于平板电视已经手机上,只需更改不同的布局即可。Fragment可以自己接受处理用户的点击触摸事件,你可以动态的添加移除替换Fragment,来达到你的要求。主要用来一个Activity中有多个模块的时候讲不通的模块交给不通的Fragment来处理,避免在Activity中写一大堆代码,有利于项目后期的维护。
2,Fragment的生命周期
上面既然说了Fragment有自己的生命周期,那么下面通过两张图片来看一下Fragment的生命周期是怎样的。
Fragment的生命周期方法
Fragment的生命周期方法与Activity的生命周期的关系,Activity的生命周期会直接影响Fragment的生命周期
相对Activity来说,这里我们来谈一谈几个Activity中没有的方法
onAttach(),当fragment与Activity产生关联时调用
onCreateView(),用来产生view的方法,返回的view对象即Fragment的视图,一般使用View.inflate()方法将xml转化为view
onActivityCreated(), 当Activity的oncreate()方法返回时调用
onDestoryView(),与onCreateView对应,当该Fragment的视图被移除时调用
onDetach()与onAttach对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现
声明周期回调的具体时机:
场景演示 : 切换到该Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume
屏幕灭掉:
11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解锁
11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume
切换到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切换回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到应用
11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出应用
11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach
3,Fragmend的使用
- 静态使用
静态使用Fragment是比较简单的,相当与把Fragment当做一个控件来使用
使用步骤:
- 1 继承Fragment,重写onCreateView()来构建Fragment的视图
ContentFragment布局文件中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:textSize="25sp"
android:text="Fragment当作主面板"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
TitleFragment的布局文件中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@android:color/darker_gray"
android:layout_height="45dp">
<ImageButton
android:id="@+id/im_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/star_on"
/>
<TextView
android:textSize="25sp"
android:textColor="@android:color/holo_red_dark"
android:gravity="center"
android:text="Fragment制作标题栏"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
2 在Activity的布局文件中声明
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<fragment
android:id="@+id/fg_title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:name="app.linfeng.com.myapplication.TitleFragment"
/>
<fragment
android:layout_below="@id/fg_title"
android:id="@+id/fg_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="app.linfeng.com.myapplication.ContentFragment"
/>
</RelativeLayout>
3.在各自的Fragment中处理点击事件
- 动态的使用
在项目中一般常用的还是动态的使用Fragment来添加移除替换视图
1.Activity的布局文件
<RelativeLayout 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" >
<fragment
android:id="@+id/id_fragment_title"
android:name="com.zhy.zhy_fragments.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp" />
<include
android:id="@+id/id_ly_bottombar"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
layout="@layout/bottombar" />
<FrameLayout
android:id="@+id/id_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/id_ly_bottombar"
android:layout_below="@id/id_fragment_title" />
</RelativeLayout>
Activity中的代码
package com.zhy.zhy_fragments;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener
{
private LinearLayout mTabWeixin;
private LinearLayout mTabFriend;
private ContentFragment mWeixin;
private FriendFragment mFriend;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 初始化控件和声明事件
mTabWeixin = (LinearLayout) findViewById(R.id.tab_bottom_weixin);
mTabFriend = (LinearLayout) findViewById(R.id.tab_bottom_friend);
mTabWeixin.setOnClickListener(this);
mTabFriend.setOnClickListener(this);
// 设置默认的Fragment
setDefaultFragment();
}
private void setDefaultFragment()
{
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
mWeixin = new ContentFragment();
transaction.replace(R.id.id_content, mWeixin);
transaction.commit();
}
@Override
public void onClick(View v)
{
FragmentManager fm = getFragmentManager();
// 开启Fragment事务
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId())
{
case R.id.tab_bottom_weixin:
if (mWeixin == null)
{
mWeixin = new ContentFragment();
}
// 使用当前Fragment的布局替代id_content的控件
transaction.replace(R.id.id_content, mWeixin);
break;
case R.id.tab_bottom_friend:
if (mFriend == null)
{
mFriend = new FriendFragment();
}
transaction.replace(R.id.id_content, mFriend);
break;
}
// transaction.addToBackStack();
// 事务提交
transaction.commit();
}
}
使用的是替换的方式实现的,replace(),相当于把Fragment的onCreateView返回的view添加到FrameLayout这一容器中显示
效果图如下:
Fragment是3.0之后出现的,在3.0以下的版本中需要引入v4包,通过 getSupportFragmentManager获得FragmentManager。不过现在3.0以下的系统占比很小,近乎不用考虑这个问题,而且最新的v7包中getSupportFragmentManager就可以兼容低版本
使用Fragment常见的API
Fragment 用来定义Fragment
FragmentManager 用来在Activity中管理和操作Fragment
FragmentTransaction 事务
a,获取FragmentManeger 在Activity中getFragmentManager,向下兼容getSupportFragmentManager;
b,获取事务,主要操作时靠事务来完成的
FragmentTransaction transaction = fm.benginTransatcion();
transaction.add();向Activity中加入一个Fragment
transaction.remove();移除一个Fragment,如果一个Fragmetn没有加入回退栈,这个Fragment的实例会被销毁
transaction.replace();使用一个新的Frament替换掉另外一个Fragment,实际就是先remove后add;
transaction.hide(); 隐藏当前的Fragment,只是不可见,实例并不会销毁
transaction.show();显示之前隐藏的Fragment
transaction.detach();将Fragment中的view冲UI中移除
transaction.attach();重新创建view视图,并显示在UI
transaction.commit();提交事务
commit方法一定要在Activity.onSaveInstance()之前调用
常见的事例
a、比如:我在FragmentA中的EditText填了一些数据,当切换到FragmentB时,如果希望会到A还能看到数 据,则适合你的就是hide和show;也就是说,希望保留用户操作的面板,你可以使用hide和show,当然了不要使劲在那new实例,进行下非 null判断。
b、再比如:我不希望保留用户操作,你可以使用remove(),然后add();或者使用replace()这个和remove,add是相同的效果。
c、 remove和detach有一点细微的区别,在不考虑回退栈的情况下,remove会销毁整个Fragment实例,而detach则只是销毁其视图结 构,实例并不会被销毁。那么二者怎么取舍使用呢?如果你的当前Activity一直存在,那么在不希望保留用户操作的时候,你可以优先使用detach。