android fragment的使用

来源:互联网 发布:阿里云招聘官网首页 编辑:程序博客网 时间:2024/06/08 15:31

一.Fragment 是什么

Android Fragment最初是为适应大屏幕只需要更新一部分视图的情况而产生的, Fragment可以当作一个界面的一个组成部分,它小于Activity,一个Activity可以包含多个Fragment,且不同的时刻可以自由地控制一个Activity中呈现出哪些Fragment。就像Linux中进程与线程的关系一样。

这就意味着Fragment可以有自己的生命周期,并能独立地接收和处理用户的事件。在一个Activity的生命周期中,你可以动态地添加,移除,修改和替换某个Fragment,从而为复杂多变的布局提供了方便。

 

二.Fragment怎么用

既然fragment如此灵活,那么它肯定可以动态的操作,也可以静态的操作。

所以Fragment为动态的使用和静态的使用。

1.      静态的使用 Fragment

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

a.      继承Fragment,重写onCreateView决定Fragemnt的布局

b.      在Activity中声明此Fragment,就当和普通的View一样

具体:

1.      在Activity中创建一个fragment

getFragmentManager().beginTransaction()

                                       .add(R.id.container,new PlaceholderFragment()).commit();

2.      创建一个类PlaceholderFragment并继承自Fragment, 重写onCreateView方法:

3.      在onCreateView中使用inflator指定一个fragment布局。

publicclass PlaceholderFragment extends Fragment {

 

public PlaceholderFragment() {

}

 

@Override

public View onCreateView(LayoutInflaterinflater, ViewGroup container,

                    Bundle savedInstanceState) {

           View rootView =inflater.inflate(R.layout.fragment_main, container,

                             false);

           return rootView;

}

}

4.      创建一个Fragment布局文件,并使用上面的方法将类与layout绑定。

 

这样一个静态的fragment就创建完成了。


2. 动态的使用Fragment

说到动态使用Fragment, 就少不了一个FragmentManager, 它就是androiid 中Fragment的管理者,程序中fragment的生死存亡都受它管理, 可见其权利之大。

这里先把fragment的动态使用方法说完,再具体了解FragmentManager.

a. 动态替换一个Fragment

这里就要使用到FragmentManager来操作, 具体的过程是

getFragmentManager() //获取到FragmentManager
.beginTransaction()        //执行改变
.addToBackStack(null)  //将这个Fragment添加到回退栈中
.replace(R.id.container, new AnotherFragment()) //使用replace方法替换一个Fragment
.commit();                        //提交变更。

这里讲一下FragmentManager

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()

你可以用FragmentManager来做以上事情:

1使用方法findFragmentById()findFragmentByTag(),获取activity中已存在的fragment们。

2使用方法addToBackStack和popBackStack()将一Fragment在其所在的Activity中动态地增加和回退。

3用方法addOnBackStackChangedListerner()注册一个侦听器以监视后退栈的变化。

三. Fragment的生命周期

费话少说, 直接上官方图:


可以看到Fragment比Activity的生命周期还要复杂,不过我们主要使用的只有下面这三个:

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.
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.
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).

带上官方解释更原汁原味。


1 0