一、概述
Fragment已经出现了好几年了,首次接触是在替代TabHost配合RadioButton实现底部多页导航功能时,现在已经基本都改为ViewPager+Fragment实现。但之后自己在开发功能时都不会下意识去使用这些技术,导致到现在还一知半解,刚好现在的项目同事用了很多Fragment,我自己也好好学习下,以下为记录。
二、Fragment生命周期
首先,Fragment是需要依存于Activity的,Activity的生命周期会直接影响到Fragment,借用很多大神博客中官网的图来展示。
可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现(也就是调用super.xxxxx)
三、Fragment的基础使用方式
3.1 静态使用Fragment
这种方式是最基础简单的使用方式,目前也就在书上和博客上看到过,自己写的时候还没用过(不太清楚这种方式的应用场景,还需要学习)。步骤:
1、创建layout布局文件,与普通的Activity布局文件一样
2、新建一个Fragment类,继承Fragment(注意:导入Fragment包时采用support包中的类,兼容性更好,bug较少),重写onCreateView()将步骤1的布局文件引入
3、在Activity布局文件中申明fragment标签,引入步骤2中的Fragment类
具体步骤代码如下:
Fragment布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="@+id/id_title_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/showleft_selector" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="静态Fragment布局文件"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
Fragment类文件代码:
public class StaticFragment extends Fragment
{
private ImageButton mLeftMenu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_static, container, false);
mLeftMenu = (ImageButton) view.findViewById(R.id.id_title_left_btn);
mLeftMenu.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getActivity(),
"这是一个静态Fragment",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
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_static"
android:name="com.rxx.test.fragment.StaticFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- 此处可以加入多个fragment -->
</RelativeLayout>
最后就可以在Activity中根据id获取后进行操作。
3.2 动态使用Fragment
下面介绍如何动态的添加、更新、以及删除Fragment,通常会在activity布局文件中留有一个FrameLayout作为Fragment展示的父控件。
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" >
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="下面是加载Fragment的ViewGroup"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_below="@+id/tv_id"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Activity主要调用代码:replace方法
// 获取FragmentManager,support包,利用replace方法将实例化后的Fragment对象传入下面的函数
private void loadFragment(Fragment f) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, f);
fragmentTransaction.commit();
}
// 移除Fragment
private void removeFragment(Fragment f) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.remove(f);
fragmentTransaction.commit();
}
注:目前Activity基本都继承AppCompatActivity,可以直接导入support包中的FragmentManager,如果不是support包则通过getFragmentManager()获取管理器对象
四、Fragment常用的Api
Fragment常用的三个类:
android.support.v4.app.Fragment 主要用于定义Fragment
android.support.v4.app.FragmentManager 主要用于在Activity中操作Fragment
android.support.v4.app.FragmentTransaction 处理Fragment操作的事务
a、获取FragmentManage的方式:
getFragmentManager() // v4中,getSupportFragmentManager
b、主要的操作都是FragmentTransaction的方法
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个事务
transaction.add()
往Activity中添加一个Fragment
transaction.remove()
从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈下节记录),这个Fragment实例将会被销毁。
transaction.replace()
使用另一个Fragment替换当前的,实际上就是remove()然后add()的合体~
transaction.hide()
隐藏当前的Fragment,仅仅是设为不可见,并不会销毁,相当于view的setVisibility()方法
transaction.show()
显示之前隐藏的Fragment
detach()
会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
attach()
重建view视图,附加到UI上并显示。
transatcion.commit()//提交一个事务
注:初用会经常遇到Activity状态不一致:State loss这样的错误。主要是因为:commit方法一定要在Activity.onSaveInstance()之前调用。
在一个事务开启到提交可以进行多个的添加、移除、替换等操作。
值得注意的是:如果喜欢使用Fragment,一定要清楚这些方法,哪个会销毁视图,哪个会销毁实例,哪个仅仅只是隐藏,这样才能更好的使用它们。
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。
五、总结
这篇主要是Fragment的基础知识,下篇会记录一些实际开发的技巧和api使用场景等。