Android Fragment

Fragment是activity的界面中的一部分或一种行为。你能够把多个Fragment们组合到一个activity中来建立一个多面界面而且你能够在多个activity中重用一个Fragment。你能够把Fragment认为模块化的一段activity,它具备本身的生命周期,接收它本身的事件,并能够在activity运行时被添加或删除。 html

Fragment不能独立存在,它必须嵌入到activity中,并且Fragment的生命周期直接受所在的activity的影响。例如:当activity暂停时,它拥有的全部的Fragment们都暂停了,当activity销毁时,它拥有的全部Fragment们都被销毁。然而,当activity运行时(在onResume()以后,onPause()以前),你能够单独地操做每一个Fragment,好比添加或删除它们。当你在执行上述针对Fragment的事务时,你能够将事务添加到一个棧中,这个栈被activity管理,栈中的每一条都是一个Fragment的一次事务。有了这个栈,就能够反向执行Fragment的事务,这样就能够在Fragment级支持“返回”键(向后导航)。 java

当向activity中添加一个Fragment时,它须置于ViewGroup控件中,而且需定义Fragment本身的界面。你能够在layoutxml文件中声明Fragment,元素为:<fragment>;也能够在代码中建立Fragment,而后把它加入到ViewGroup控件中。然而,Fragment不必定非要放在activity的界面中,它能够隐藏在后台为actvitiy工做。 android

本章描述如何使用fragment,包括fragment在加入activity的后退棧中时如何保持本身的状态,如何与activity以及其它fragment们共享事件,如何显示在activity的动做栏,等等。 数据库

设计哲学

Android从3.0开始引入fragment,主要是为了支持更动态更灵活的界面设计,好比在平板上的应用。平板机上拥有比手机更大的屏幕空间来组合和交互界面组件们。Fragment使你在作那样的设计时,不需应付view树中复杂的变化。经过把activity的layout分红fragment,你能够在activity运行时改变它的样子,而且能够在activity的后退栈中保存这些改变。 api

例如:写一个读新闻的程序,能够用一个fragment显示标题列表,另外一个fragment显示选中标题的内容,这两个fragment都在一个activity上,并排显示。那么这两个fragment都有本身的生命周期并响应本身感兴趣的事件。因而,不需再像手机上那样用一个activity显示标题列表,用另外一个activity显示新闻内容;如今能够把二者放在一个activity上同时显示出来。以下图: app


Fragment必须被写成可重用的模块。由于fragment有本身的layout,本身进行事件响应,拥有本身的生命周期和行为,因此你能够在多个activity中包含同一个Fragment的不一样实例。这对于让你的界面在不一样的屏幕尺寸下都能给用户完美的体验尤为重要。好比你能够在程序运行于大屏幕中时启动包含不少fragment的activity,而在运行于小屏幕时启动一个包含少许fragment的activity。 ide

举个例子--仍是刚才那个读新闻的程序-当你检测到程序运行于大屏幕时,启动activityA,你将标题列表和新闻内容这两个fragment都放在activityA中;当检测到程序运行于小屏幕时,仍是启动activityA,但此时A中只有标题列表fragment,当选中一个标题时,activityA启动activityB,B中含有新闻内容fragment。 模块化


建立Fragment

    要建立fragment,必须从Fragment或Fragment的派生类派生出一个类。Fragment的代码写起来有些像activity。它具备跟activity同样的回调方法,好比 onCreate(),onStart(),onPause()和onStop()。实际上,若是你想把老的程序改成使用fragment,基本上只须要把activity的回调方法的代码移到fragment中对应的方法便可。 函数

一般须要实现以上生命周期函数: 动画

onCreate():

当建立fragment时系统调用此方法。在其中你必须初始化fragment的基础组件们。可参考activity的说明。

onCreateView():

系统在fragment要画本身的界面时调用(在真正显示以前)此方法。这个方法必须返回frament的layout的根控件。若是这个fragment不提供界面,那它应返回null。

onPause():

不废话了,跟activity同样。

大多数程序应最少对fragment实现这三个方法。固然还有其它几个回调方法可应该按状况实现之。全部的生命周期回调函数在“操控fragment的生命周期”一节中有详细讨论。

下图为fragment的生命周期(它所在的activity处于运行状态)。已经画得很明白了,不用再解释了吧?


 

还有几个现成的fragemtn的派生类,你可能须要从它们派生,以下所列:

DialogFragment

显示一个浮动的对话框。使用这个类建立对话框是替代activity建立对话框的最佳选择.由于你能够把fragmentdialog放入到activity的返回栈中,使用户能再返回到这个对话框

ListFragment

显示一个列表控件,就像ListActivity类,它提供了不少管理列表的方法,好比onListItemClick()方法响应click事件。

PreferenceFragment

显示一个由Preference对象组成的列表,与PreferenceActivity相同。它用于为程序建立“设置”activity

为fragment添加用户界面

    fragment通常做为activity的用户界面的一部分,把它本身的layout嵌入到activity的layout中。    一个

    要为fragment提供layout,你必须实现onCreateView()回调方法,而后在这个方法中返回一个View对象,这个对象是fragment的layout的根。

    注:若是你的fragment是从ListFragment中派生的,就不须要实现onCreateView()方法了,由于默认的实现已经为你返回了ListView控件对象。

    要从onCreateView()方法中返回layout对象,你能够从layoutxml中生成layout对象。为了帮助你这样作,onCreateView()提供了一个LayoutInflater对象。

举例:如下代码展现了一个Fragment的子类如何从layoutxml文件example_fragment.xml中生成对象。

publicstaticclassExampleFragmentextendsFragment{
   @Override
  publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
       //Inflate the layout for this fragment
       returninflater.inflate(R.layout.example_fragment,container,false);
   }
}

    onCreateView()参数中的container是存放fragment的layout的ViewGroup对象。savedInstanceState参数是一个Bundle,跟activity的onCreate()中Bundle差很少,用于状态恢复。可是fragment的onCreate()中也有Bundle参数,因此此处的Bundle中存放的数据与onCreate()中存放的数据仍是不一样的。至于详细信息,请参考“操控fragment的生命周期”一节。

Inflate()方法有三个参数:

1layout的资源ID。

2存放fragment的layout的ViewGroup。

3布尔型数据表示是否在建立fragment的layout期间,把layout附加到container上(在这个例子中,由于系统已经把layout插入到container中了,因此值为false,若是为true会导至在最终的layout中建立多余的ViewGroup(这句我看不明白,但我翻译的应该没错))。

如今你看到如何为fragment建立layout了,下面讲述如何把它添加到activity中。

把fragment添加到activity

    通常状况下,fragment把它的layout做为activitiy的loyout的一部分合并到activity中,有两种方法将一个fragment添加到activity中:

方法一:在activity的layoutxml文件中声明fragment

    以下代码,一个activity中包含两个fragment:

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

<fragment>中声明一个fragment

    当系统建立上例中的layout时,它实例化每个fragment,而后调用它们的onCreateView()方法,以获取每一个fragment的layout。系统把fragment返回的view对象插入到<fragment>元素的位置,直接代替<fragment>元素。

    注:每一个fragment都须要提供一个ID,系统在activity从新建立时用它来恢复fragment们,你也能够用它来操做fragment进行其它的事物,好比删除它。有三种方法给fragment提供ID:

1 为android:id属性赋一个数字。

2 为android:tag属性赋一个字符串。

3若是你没有使用上述任何一种方法,系统将使用fragment的容器的ID。

方法二:在代码中添加fragment到一个ViewGroup

     这种方法能够在运行时,把fragment添加到activity的layout中。你只需指定一个要包含fragment的ViewGroup。

    为了完成fragment的事务(好比添加,删除,替换等),你必须使用FragmentTransaction的方法。你能够从activity获取到FragmentTransaction,以下:

FragmentManagerfragmentManager =getFragmentManager()
FragmentTransactionfragmentTransaction =fragmentManager.beginTransaction();

    而后你能够用add()方法添加一个fragment,它有参数用于指定容纳fragment的ViewGroup。以下:

ExampleFragmentfragment =newExampleFragment();
fragmentTransaction.add(R.id.fragment_container,fragment);
fragmentTransaction.commit();

    Add()的第一个参数是容器ViewGroup,第二个是要添加的fragment。一旦你经过FragmentTransaction对fragment作出了改变,你必须调用方法commit()提交这些改变。

不只在无界面的fragment中,在有界面的fragment中也可使用tag来做为为一标志,这样在须要获取fragment对象时,要调用findFragmentTag()。

添加一个没有界面的fragment

    上面演示了如何添加fragment来提供界面,然而,你也可使用fragment为activity提供后台的行为而不用显示fragment的界面。


管理fragment


    要添加一个没有界面的fragment,需在activity中调用方法add(Fragment,String)(它支持用一个惟一的字符串作为fragment的”tag”,而不是viewID)。这样添加的fragment因为没有界面,因此你在实现它时不需调用实现onCreateView()方法。

    使用tag字符串来标识一个fragment并非只能用于没有界面的fragment上,你也能够把它用于有界面的fragment上,可是,若是一个fragment没有界面,tag字符串将成为它惟一的选择。获取以tag标识的fragment,需使用方法findFragmentByTab()。

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

你能够用FragmentManager来作以上事情:

 

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

2使用方法popBackStack()从activity的后退栈中弹出fragment们(这能够模拟后退键引起的动做)。

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

更多关于以上方法的信息,请参考“FragmentManager”文档。

就像前面章节所演示的,你还可使用FragmentManager打开一个FragmentTransaction来执行fragment的事务,好比添加或删除fragment。

执行Fragment的事务

在activity中使用fragment的一个伟大的好处是能跟据用户的输入对fragment进行添加、删除、替换以及执行其它动做的能力。你提交的一组fragment的变化叫作一个事务。事务经过FragmentTransaction来执行。你还能够把每一个事务保存在activity的后退栈中,这样就可让用户在fragment变化之间导航(跟在activity之间导航同样)。

你能够经过FragmentManager来取得FragmentTransaction的实例,以下:

FragmentManagerfragmentManager=getFragmentManager();
FragmentTransactionfragmentTransaction=fragmentManager.beginTransaction();

一个事务是在同一时刻执行的一组动做(很像数据库中的事务)。你能够用add(),remove(),replace()等方法构成事务,最后使用commit()方法提交事务。

在调用commint()以前,你能够用addToBackStack()把事务添加到一个后退栈中,这个后退栈属于所在的activity。有了它,就能够在用户按下返回键时,返回到fragment们执行事务以前的状态。

以下例:演示了如何用一个fragment代替另外一个fragment,同时在后退栈中保存被代替的fragment的状态。

//Create new fragment and transaction
FragmentnewFragment=newExampleFragment();
FragmentTransactiontransaction=getFragmentManager().beginTransaction();

//Replace whatever is in the fragment_container view with thisfragment,
//and add the transaction to the backstack
transaction.replace(R.id.fragment_container,newFragment);
transaction.addToBackStack(null);

//Commit the transaction
transaction.commit();

解释:newFragment代替了控件IDR.id.fragment_container所指向的ViewGroup中所含的任何fragment。而后调用addToBackStack(),此时被代替的fragment就被放入后退栈中,因而当用户按下返回键时,事务发生回溯,原先的fragment又回来了。

若是你向事务添加了多个动做,好比屡次调用了add(),remove()等以后又调用了addToBackStack()方法,那么全部的在commit()以前调用的方法都被做为一个事务。当用户按返回键时,全部的动做都被反向执行(事务回溯)。

事务中动做的执行顺序可随意,但要注意如下两点:

1你必须最后调用commit()。

2若是你添加了多个fragment,那么它们的显示顺序跟添加顺序一至(后显示的覆盖前面的)。

若是你在执行的事务中有删除fragment的动做,并且没有调用addToBackStack(),那么当事务提交时,那些被删除的fragment就被销毁了。反之,那些fragment就不会被销毁,而是处于中止状态。当用户返回时,它们会被恢复。

密技:对于fragment事务,你能够应用动画。在commit()以前调用setTransition()就行。――通常银我不告诉他哦。

可是,调用commit()后,事务并不会立刻执行。它会在activity的UI线程(其实就是主线程)中等待直到线程能执行的时候才执行(废话)。若是必要,你能够在UI线程中调用executePendingTransactions()方法来当即执行事务。但通常不需这样作,除非有其它线程在等待事务的执行。

警告:你只能在activity处于可保存状态的状态时,好比running中,onPause()方法和onStop()方法中提交事务,不然会引起异常。这是由于fragment的状态会丢失。若是要在可能丢失状态的状况下提交事务,请使用commitAllowingStateLoss()。

与activity通信

  尽管fragment的实现是独立于activity的,能够被用于多个activity,可是每一个activity所包含的是同一个fragment的不一样的实例。

  Fragment能够调用getActivity()方法很容易的获得它所在的activity的对象,而后就能够查找activity中的控件们(findViewById())。例如:

ViewlistView =getActivity().findViewById(R.id.list);

  一样的,activity也能够经过FragmentManager的方法查找它所包含的frament们。例如:

ExampleFragment fragment=(ExampleFragment)getFragmentManager().findFragmentById(R.id.example_fragment

activity响应fragment的事件

  有时,你可能须要fragment与activity共享事件。一个好办法是在fragment中定义一个回调接口,而后在activity中实现之。

  例如,仍是那个新闻程序的例子,它有一个activity,activity中含有两个fragment。fragmentA显示新闻标题,fragmentB显示标题对应的内容。fragmentA必须在用户选择了某个标题时告诉activity,而后activity再告诉fragmentB,fragmentB就显示出对应的内容(为何这么麻烦?直接fragmentA告诉fragmentB不就好了?也能够啊,可是你的fragment就减小了可重用的能力。如今我只需把个人事件告诉宿主,由宿主决定如何处置,这样是否是重用性更好呢?)。以下例,OnArticleSelectedListener接口在fragmentA中定义:

public static class FragmentA extends ListFragment{
   ...
   //Container Activity must implement this interface
   public interface OnArticleSelectedListener{
       public void onArticleSelected(Uri articleUri);
   }
   ...
}

而后activity实现接口OnArticleSelectedListener,在方法onArticleSelected()中通知fragmentB。当fragment添加到activity中时,会调用fragment的方法onAttach(),这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,检查方法就是对传入的activity的实例进行类型转换,以下所示:

public static class FragmentA extends ListFragment{
   OnArticleSelectedListener mListener;
   ...
   @Override
   public void onAttach(Activity activity){
       super.onAttach(activity);
       try{
           mListener =(OnArticleSelectedListener)activity;
       }catch(ClassCastException e){
           throw new ClassCastException(activity.toString()+"must implement OnArticleSelectedListener");
       }
   }
   ...
}

若是activity没有实现那个接口,fragment抛出ClassCastException异常。若是成功了,mListener成员变量保存OnArticleSelectedListener的实例。因而fragmentA就能够调用mListener的方法来与activity共享事件。例如,若是fragmentA是一个ListFragment,每次选中列表的一项时,就会调用fragmentA的onListItemClick()方法,在这个方法中调用onArticleSelected()来与activity共享事件,以下:

public static class FragmentA extends ListFragment{
   OnArticleSelectedListener mListener;
   ...
   @Override
   public void onListItemClick(ListView l,View v,int position,long id){
       //Append the clicked item's row ID with the content provider Uri
       Uri noteUri =ContentUris.withAppendedId(ArticleColumns.CONTENT_URI,id);
       //Send the event and Uri to the host activity
       mListener.onArticleSelected(noteUri);
   }
   ...
}

onListItemClick()传入的参数id是列表的被选中的行ID,另外一个fragment用这个ID来从程序的ContentProvider中取得标题的内容。

处理fragement的生命周期

把条目添加到动做栏

你的fragment们能够向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动做栏(界面中顶部的那个区域)添加条目,这都需经过实现方法onCreateOptionManu()来完成。

你从fragment添加到菜单的任何条目,都会出如今现有菜单项以后。Fragment以后能够经过方法onOptionsItemSelected()来响应本身的菜单项被选择的事件。

你也能够在fragemnt中注册一个view来提供快捷菜单(上下文菜单)。当用户要打开快捷菜单时,fragment的onCreateContextMenu()方法会被调用。当用户选择其中一项时,fragemnt的onContextItemSelected()方法会被调用。

注:尽管你的fragment能够分别收到它所添加的菜单项的选中事件,可是activity才是第一个接收这些事件的家伙,只有当activity对某个事件置之不理时,fragment才能接收到这个事件,对于菜单和快捷菜单都是这样。

 

处理fragement的生命周期

 

管理fragment的生命周期有些像管理activity的生命周期。Fragment能够生存在三种状态:

Resumed:

Fragment在一个运行中的activity中而且可见。

Paused:

另外一个activity处于最顶层,可是fragment所在的activity并无被彻底覆盖(顶层的activity是半透明的或不占据整个屏幕)。

Stoped:

Fragment不可见。多是它所在的activity处于stoped状态或是fragment被删除并添加到后退栈中了。此状态的fragment仍然存在于内存中。

一样相似于activity,你能够把fragment的状态保存在一个Bundle中,在activity被recreated时就需用到这个东西。你能够在onSaveInstanceState()方法中保存状态并在onCreate()或onCreateView()或onActivityCreated()中恢复,关于更多的保存状态的信息,请参考Activitys章节。

Fragment与Activity的生命周期中最大的不一样就是存储到后退栈中的过程。Activity是在中止时自动被系统压入中止栈,而且这个栈是被系统管理的;而fragment是被压入activity所管理的一个后退栈,而且只有你在删除fragment后并明确调用addToBackStack()方法时才被压入。

然而,管理fragment的生命周期与管理activity的生命周期极其类似。你所须要去思考的是activity的生命周期如何影响fragment的生命周期。

 

协调与 activity生命周期的关系

 

Activity直接影响它所包含的fragment的生命周期,因此对activity的某个生命周期方法的调用也会产生对fragment相同方法的调用。例如:当activity的onPause()方法被调用时,它所包含的全部的fragment们的onPause()方法都会被调用。

Fragment比activity还要多出几个生命周期回调方法,这些额外的方法是为了与activity的交互而设立,以下:

onAttach()

当fragment被加入到activity时调用(在这个方法中能够得到所在的activity)。

onCreateView()

当activity要获得fragment的layout时,调用此方法,fragment在其中建立本身的layout(界面)。

onActivityCreated()

当activity的onCreated()方法返回后调用此方法。

onDestroyView()

当fragment的layout被销毁时被调用。

onDetach()

当fragment被从activity中删掉时被调用。

一旦activity进入resumed状态(也就是running状态),你就能够自由地添加和删除fragment了。所以,只有当activity在resumed状态时,fragment的生命周期才能独立的运转,其它时候是依赖于activity的生命周期变化的。

android Fragments详解七:fragement示例

下例中实验了上面所讲的全部内容。此例有一个activity,其含有两个fragment。一个显示莎士比亚剧的播放曲目,另外一个显示选中曲目的摘要。此例还演示了如何跟据屏幕大小配置fragment。

主activity建立layout。

 

[java]  view plain copy
  1. @Override  
  2. protectedvoid onCreate(Bundle savedInstanceState) {  
  3.    super.onCreate(savedInstanceState);  
  4.   
  5.    setContentView(R.layout.fragment_layout);  
  6. }  


主activity的layoutxml文档

 

 

[java]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="horizontal"  
  3.     android:layout_width="match_parent" android:layout_height="match_parent">  
  4.   
  5.     <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"  
  6.             android:id="@+id/titles" android:layout_weight="1"  
  7.             android:layout_width="0px" android:layout_height="match_parent" />  
  8.   
  9.     <FrameLayout android:id="@+id/details" android:layout_weight="1"  
  10.             android:layout_width="0px" android:layout_height="match_parent"  
  11.             android:background="?android:attr/detailsElementBackground" />  
  12.   
  13. </LinearLayout>  


系统在activity加载此layout时初始化TitlesFragment(用于显示标题列表),TitlesFragment的右边是一个FrameLayout,用于存放显示摘要的fragment,可是如今它仍是空的,fragment只有当用户选择了一项标题后,摘要fragment才会被放到FrameLayout中。

 

然而,并非全部的屏幕都有足够的宽度来容纳标题列表和摘要。因此,上述layout只用于横屏,现把它存放于ret/layout-land/fragment_layout.xml。

以外,当用于竖屏时,系统使用下面的layout,它存放于ret/layout/fragment_layout.xml:

 

[java]  view plain copy
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent" android:layout_height="match_parent">  
  3.     <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"  
  4.             android:id="@+id/titles"  
  5.             android:layout_width="match_parent" android:layout_height="match_parent" />  
  6. </FrameLayout>  

 

这个layout只包含TitlesFragment。这表示当使用竖屏时,只显示标题列表。当用户选中一项时,程序会启动一个新的activity去显示摘要,而不是加载第二个fragment。

下一步,你会看到Fragment类的实现。第一个是TitlesFragment,它从ListFragment派生,大部分列表的功能由ListFragment提供。

当用户选择一个Title时,代码须要作出两种行为,一种是在同一个activity中显示建立并显示摘要fragment,另外一种是启动一个新的activity。

 

[java]  view plain copy
  1. public static class TitlesFragment extends ListFragment {  
  2.     boolean mDualPane;  
  3.     int mCurCheckPosition = 0;  
  4.   
  5.     @Override  
  6.     public void onActivityCreated(Bundle savedInstanceState) {  
  7.         super.onActivityCreated(savedInstanceState);  
  8.   
  9.         // Populate list with our static array of titles.  
  10.         setListAdapter(new ArrayAdapter<String>(getActivity(),  
  11.                 android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));  
  12.   
  13.         // Check to see if we have a frame in which to embed the details  
  14.         // fragment directly in the containing UI.  
  15.         View detailsFrame = getActivity().findViewById(R.id.details);  
  16.         mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;  
  17.   
  18.         if (savedInstanceState != null) {  
  19.             // Restore last state for checked position.  
  20.             mCurCheckPosition = savedInstanceState.getInt("curChoice"0);  
  21.         }  
  22.   
  23.         if (mDualPane) {  
  24.             // In dual-pane mode, the list view highlights the selected item.  
  25.             getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);  
  26.             // Make sure our UI is in the correct state.  
  27.             showDetails(mCurCheckPosition);  
  28.         }  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onSaveInstanceState(Bundle outState) {  
  33.         super.onSaveInstanceState(outState);  
  34.         outState.putInt("curChoice", mCurCheckPosition);  
  35.     }  
  36.   
  37.     @Override  
  38.     public void onListItemClick(ListView l, View v, int position, long id) {  
  39.         showDetails(position);  
  40.     }  
  41.   
  42.     /** 
  43.      * Helper function to show the details of a selected item, either by 
  44.      * displaying a fragment in-place in the current UI, or starting a 
  45.      * whole new activity in which it is displayed. 
  46.      */  
  47.     void showDetails(int index) {  
  48.         mCurCheckPosition = index;  
  49.   
  50.         if (mDualPane) {  
  51.             // We can display everything in-place with fragments, so update  
  52.             // the list to highlight the selected item and show the data.  
  53.             getListView().setItemChecked(index, true);  
  54.   
  55.             // Check what fragment is currently shown, replace if needed.  
  56.             DetailsFragment details = (DetailsFragment)  
  57.                     getFragmentManager().findFragmentById(R.id.details);  
  58.             if (details == null || details.getShownIndex() != index) {  
  59.                 // Make new fragment to show this selection.  
  60.                 details = DetailsFragment.newInstance(index);  
  61.   
  62.                 // Execute a transaction, replacing any existing fragment  
  63.                 // with this one inside the frame.  
  64.                 FragmentTransaction ft = getFragmentManager().beginTransaction();  
  65.                 ft.replace(R.id.details, details);  
  66.                 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);  
  67.                 ft.commit();  
  68.             }  
  69.   
  70.         } else {  
  71.             // Otherwise we need to launch a new activity to display  
  72.             // the dialog fragment with selected text.  
  73.             Intent intent = new Intent();  
  74.             intent.setClass(getActivity(), DetailsActivity.class);  
  75.             intent.putExtra("index", index);  
  76.             startActivity(intent);  
  77.         }  
  78.     }  


第二个fragment,DetailsFragment显示被选择的Title的摘要:

 

 

[java]  view plain copy
  1. public static class DetailsFragment extends Fragment {  
  2.     /** 
  3.      * Create a new instance of DetailsFragment, initialized to 
  4.      * show the text at 'index'. 
  5.      */  
  6.     public static DetailsFragment newInstance(int index) {  
  7.         DetailsFragment f = new DetailsFragment();  
  8.   
  9.         // Supply index input as an argument.  
  10.         Bundle args = new Bundle();  
  11.         args.putInt("index", index);  
  12.         f.setArguments(args);  
  13.   
  14.         return f;  
  15.     }  
  16.   
  17.     public int getShownIndex() {  
  18.         return getArguments().getInt("index"0);  
  19.     }  
  20.   
  21.     @Override  
  22.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  23.             Bundle savedInstanceState) {  
  24.         if (container == null) {  
  25.             // We have different layouts, and in one of them this  
  26.             // fragment's containing frame doesn't exist.  The fragment  
  27.             // may still be created from its saved state, but there is  
  28.             // no reason to try to create its view hierarchy because it  
  29.             // won't be displayed.  Note this is not needed -- we could  
  30.             // just run the code below, where we would create and return  
  31.             // the view hierarchy; it would just never be used.  
  32.             return null;  
  33.         }  
  34.   
  35.         ScrollView scroller = new ScrollView(getActivity());  
  36.         TextView text = new TextView(getActivity());  
  37.         int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
  38.                 4, getActivity().getResources().getDisplayMetrics());  
  39.         text.setPadding(padding, padding, padding, padding);  
  40.         scroller.addView(text);  
  41.         text.setText(Shakespeare.DIALOGUE[getShownIndex()]);  
  42.         return scroller;  
  43.     }  
  44. }  


若是当前的layout没有R.id.detailsView(它被用于DetailsFragment的容器),那么程序就启动DetailsActivity来显示摘要。

 

下面是DetailsActivity,它只是简单地嵌入DetailsFragment来显示摘要。

 

[java]  view plain copy
  1. public static class DetailsActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.   
  7.         if (getResources().getConfiguration().orientation  
  8.                 == Configuration.ORIENTATION_LANDSCAPE) {  
  9.             // If the screen is now in landscape mode, we can show the  
  10.             // dialog in-line with the list so we don't need this activity.  
  11.             finish();  
  12.             return;  
  13.         }  
  14.   
  15.         if (savedInstanceState == null) {  
  16.             // During initial setup, plug in the details fragment.  
  17.             DetailsFragment details = new DetailsFragment();  
  18.             details.setArguments(getIntent().getExtras());  
  19.             getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();  
  20.         }  
  21.     }  
  22. }  


注意这个activity在检测到是竖屏时会结束本身,因而主activity会接管它并显示出TitlesFragment和DetailsFragment。这能够在用户在竖屏时显示在TitleFragment,但用户旋转了屏幕,使显示变成了横屏。

Android Tabhost with FragmentActivity

此文解决我这两天的问题,故转载:原文Android Tabhost with FragmentActivity

 

2012-05-07 更新)接續Android TabHost中切換Activity記錄了使用ActivityGroup達到在TabHost中切換Activity的方法,也在Android Screen Orientation Event螢幕方向處理+Acitivity Liftcycle記錄了當螢幕方向改變時的處理,這篇小蛙繼續記錄用FragmentActivity取代ActivityGroup,透過FragmentActivity內建的BackStack來管理倒退歷程。

MainTabActivity.java : 主要的Tabhost Activity。

 

复制代码
public class MainTabActivity extends Activity { private TabHost mHost; // 在Activity中使用Tabhost必須要有LocalActivityManager物件來設定  LocalActivityManager lam; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // main layout採用預設的Tabhost mHost = (TabHost) findViewById(android.R.id.tabhost); lam = new LocalActivityManager(MainTabActivity.this, false); lam.dispatchCreate(savedInstanceState); mHost.setup(lam); mHost.addTab(mHost.newTabSpec("Tab1").setIndicator("Tab1").setContent(new Intent(MainTabActivity.this, FragmentActivity1.class))); mHost.addTab(mHost.newTabSpec("Tab2").setIndicator("Tab2").setContent(new Intent(MainTabActivity.this, FragmentActivity2.class))); } @Override protected void onPause() { // 漏掉這行必定出錯  lam.dispatchPause(isFinishing()); super.onPause(); } @Override protected void onResume() { // 漏掉這行必定出錯  lam.dispatchResume(); super.onResume(); } }
复制代码

FragmentActivity1.java : 第一個Tab中用來管理Fragment的FragmentActivity。(2012-05-07更新)經由Jay留言後,小蛙詳細測了一下,發現FragmentActivity主畫面中的Button是沒辦法消失的(因為FragmentActivity的目的關係),所以改为這樣,讓FragmentActivity純粹當成容器,主要的內容還是以Fragment為主。(這個方法不是惟一,可是一個可行的方法,應該也有更好的作法!)

复制代码
public class FragmentActivity1 extends FragmentActivity { public static FragmentManager fm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_activity_1); fm = getSupportFragmentManager(); // 只當容器,主要內容已Fragment呈現 initFragment(new Fragment1()); } // 切換Fragment public static void changeFragment(Fragment f){ changeFragment(f, false); } // 初始化Fragment(FragmentActivity中呼叫) public static void initFragment(Fragment f){ changeFragment(f, true); } private static void changeFragment(Fragment f, boolean init){ FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.simple_fragment, f); if(!init) ft.addToBackStack(null); ft.commit(); } }
复制代码

Fragment1.java : 真正使用到的Fragment。

复制代码
public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_1, container, false); Button tv = (Button)v.findViewById(R.id.button2); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 直接呼叫FragmentActivity1的靜態方法來作切換 FragmentActivity1.changeFragment(new Fragment2()); } }); return v; } }
复制代码

fragment_acitivity_1.xml : FragmentActivity layout。(2012-05-07修改) FragmentActivity只用來當容器,而不真正呈現內容(僅把Fragment內容載入)。

复制代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/simple_fragment"> </LinearLayout>
复制代码

fragment1.xml:Fragment layout,FragmentActivity載入的真正內容。

复制代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
复制代码

最後別忘了在AndroidManifest.xml中加入android:configChanges="orientation"設定。這樣就成功的使用在Activity中使用Tabhost並且透過FragmentActivity來管理Fragment囉!

相关文章
相关标签/搜索