Android Fragments 详细使用



 Fragments 诞生初衷

   自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其上的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件,而多出来的空间存放UI使其会产生更多的交互,从而诞生了fragments 。fragments 的设计不需要你来亲自管理view hierarchy 的复杂变化,通过将Activity 的布局分散到frament 中,可以在运行时修改activity 的外观,并且由activity 管理的back stack 中保存些变化。


其中大多数程序必须使用Fragments 必须实现的三个回调方法分别为:

onCreate

 系统创建Fragments 时调用,可做执行初始化工作或者当程序被暂停或停止时用来恢复状态,跟Activity 中的onCreate相当。 

onCreateView

 用于首次绘制用户界面的回调方法,必须返回要创建的Fragments 视图UI。假如你不希望提供Fragments 用户界面则可以返回NULL。 

onPause 

 

Fragments 的类别

 

DialogFragment

对话框式的Fragments,可以将一个fragments 对话框并到activity 管理的fragments back stack 中,允许用户回到一个前曾摒弃fragments.

ListFragments

 类似于ListActivity 的效果,并且还提供了ListActivity 类似的onListItemCLick和setListAdapter等功能。

PreferenceFragments

 类似于PreferenceActivity .可以创建类似IPAD的设置界面。 

==================================================================

通常地 fragment做为宿主activity UI的一部分, 被作为activity整个view hierarchy的一部分被嵌入. 有2种方法你可以添加一个fragment到activity layout:
  一、在activity的layout文件中声明fragment
  你可以像为View一样, 为fragment指定layout属性(sdk3.0以后).
  例子是一个有2个fragment的activity:

代码片段,双击复制

 <?xml version="1.0" encoding="utf-8"?>            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                                       android:orientation="horizontal"                                      
android:layout_width="match_parent"                                      android:layout_height="match_parent">              
<fragment android:name="com.example.news.ArticleListFragment"                             
                                         android:id="@+id/list"                             
                                                                 android:layout_weight="1"                             
                                        android:layout_width="0dp"                             
                                                                 android:layout_height="match_parent"             
<fragment android:name="com.example.news.ArticleReaderFragment"                             
                                                                 android:id="@+id/viewer"                             
                                                                android:layout_weight="2"                             
                                                                android:layout_width="0dp"                             
                                                                 android:layout_height="match_parent" />            </LinearLayout>



  <fragment> 中的 android:name 属性指定了在layout中实例化的Fragment类.

   当系统创建这个activity layout时, 它实例化每一个在layout中指定的fragment,并调用每一个上的onCreateView()方法,来获取每一个fragment的layout. 系统将从fragment返回的 View 直接插入到<fragment>元素所在的地方.

   注意: 每一个fragment都需要一个唯一的标识, 如果activity重启,系统可以用来恢复fragment(并且你也可以用来捕获fragment来处理事务,例如移除它.)

  有3种方法来为一个fragment提供一个标识:

  为 android:id 属性提供一个唯一ID.

  为 android:tag 属性提供一个唯一字符串.

  如果以上2个你都没有提供, 系统使用容器view的ID.

 二、使用FragmentManager将fragment添加到一个已存在的ViewGroup.

  当activity运行的任何时候, 都可以将fragment添加到activity layout.只需简单的指定一个需要放置fragment的ViewGroup.为了在你的activity中操作fragment事务(例如添加,移除,或代替一个fragment),必须使用来自 FragmentTransaction 的API.

  可以按如下方法,从你的Activity取得一个 FragmentTransaction 的实例:

代码片段,双击复制

 FragmentManager fragmentManager = getFragmentManager();            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();



  然后你可以使用 add() 方法添加一个fragment, 指定要添加的fragment, 和要插入的view.

代码片段,双击复制

 ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment);            fragmentTransaction.commit();


  add()的第一个参数是fragment要放入的ViewGroup, 由resource ID指定, 第二个参数是需要添加的fragment.一旦用FragmentTransaction做了改变,为了使改变生效,必须调用commit().