fragment的基本用法

1.什么状况下使用?android

fragment一般用来做为一个activity的用户界面的一部分ide

例如,一个新闻应用能够在屏幕左侧使用一个fragment来展现一个文章的列表,而后在屏幕右侧使用另外一个fragment来展现一篇文章 – 2个fragment并排显示在相同的一个activity中,而且每个fragment拥有它本身的一套生命周期回调方法,而且处理它们本身的用户输 入事件. 所以, 取代使用一个activity来选择一篇文章,而另外一个activity来阅读文章 的方式,用户能够在相同的activity中选择一篇文章而且阅读。http://my.oschina.net/u/573470/blog/73333(原文出处)函数

建立Fragment布局

 要建立一个fragment, 必须建立一个 Fragment 的子类 (或者继承自一个已存在的它的子类). Fragment类的代码看起来很像 Activity .它包含了和activity相似的回调方法, 例如 onCreate(), onStart(),onPause, 以及 onStop(). 事实上, 若是你准备将一个现成的Android应用转换到使用fragment,你可能只需简单的将代码从你的activity的回调函数分别移动到你的fragment的回调方法.
一般, 应当至少实现以下的生命周期方法:
this

onCreate()spa

当建立fragment时, 系统调用此方法. 
在实现代码中,应当初始化想要在fragment中保持的必要组件, 当fragment被暂停或者中止后能够恢复..net

onCreateView()code

fragment第一次绘制它的用户界面的时候, 系统会调用此方法. 为了绘制fragment的UI,此方法必须返回一个View, 这个view是你的fragment布局的根view. 若是fragment不提供UI, 能够返回null.xml

onPause()对象

用户将要离开fragment时,系统调用这个方法做为第一个指示(然而它不老是意味着fragment将被销毁.) 在当前用户会话结束以前,一般应当在这里提交任何应该持久化的变化(由于用户有可能不会返回).

除了继承基类 Fragment , 还有一些子类你可能会继承: 

   1.DialogFragment:显示一个浮动的对话框. 

  由于你能够将一个fragment对话框合并到activity管理的fragment back stack中,容许用户返回到一个以前曾被摒弃的fragment.

  2.ListFragment

  显示一个由一个adapter(例如 SimpleCursorAdapter)管理的项目的列表, 相似于ListActivity.
      它提供一些方法来管理一个list view, 例如 onListItemClick()回调来处理点击事件.

    3.PreferenceFragment

 

  显示一个 Preference对象的层次结构的列表, 相似于PreferenceActivity.
      这在为你的应用建立一个"设置"activity时有用处.

fragment一般用来做为一个activity的用户界面的一部分,并将它的layout提供给activity.为了给一个fragment提供一个layout,你必须实现 onCreateView()回调方法, 当到了fragment绘制它本身的layout的时候,Android系统调用它.你的此方法的实现代码必须返回一个你的fragment的layout的根view. 注意   : 若是你的fragment是ListFragment的子类,它的默认实现是返回从onCreateView()返回一个ListView,因此通常状况下没必要实现它.   

    public static class ExampleFragment extends Fragment {   
             @Override   
             public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
                 // Inflate the layout for this fragment   
                 return inflater.inflate(R.layout.example_fragment, container, false);   
             }   
         }  

传入 onCreateView() 的 container 参数是你的fragmentlayout将被插入的父ViewGroup(来自activity的layout).savedInstanceState 参数是一个Bundle, 若是fragment是被恢复的,它提供关于fragment的以前的实例的数据

将fragment添加到activity

一般地, fragment为宿主activity提供UI的一部分, 被做为activity的整个viewhierarchy的一部分被嵌入. 有2种方法你能够添加一个fragment到activitylayout:
在activity的layout文件中声明fragment
你能够像为View同样, 为fragment指定layout属性.
例子是一个有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.

 撰写代码将fragment添加到一个已存在的ViewGroup.   
  当activity运行的任什么时候候, 均可以将fragment添加到activitylayout.只需简单的指定一个须要放置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().  

管理Fragment

要在activity中管理fragment,须要使用FragmentManager. 经过调用activity的getFragmentManager()取得它的实例. 
能够经过FragmentManager作一些事情, 包括: 

  • 使用findFragmentById() (用于在activitylayout中提供一个UI的fragment)或findFragmentByTag()(适用于有或没有UI的fragment)获取activity中存在的fragment
  • 将fragment从后台堆栈中弹出, 使用 popBackStack() (模拟用户按下BACK 命令).
  • 使用addOnBackStackChangeListener()注册一个监听后台堆栈变化的listener.

处理Fragment事务   
  关于在activity中使用fragment的很强的一个特性是:根据用户的交互状况,对fragment进行添加,移除,替换,以及执行其余动做.提交给activity的每一套变化被称为一个事务,可使用在 FragmentTransaction 中的 API 处理.咱们也能够保存每个事务到一个activity管理的backstack,容许用户经由fragment的变化往回导航(相似于经过activity日后导航).
   从 FragmentManager 得到一个FragmentTransaction的实例 :   

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

  每个事务都是同时要执行的一套变化.能够在一个给定的事务中设置你想执行的全部变化,使用诸如 add(), remove(),和 replace().而后, 要给activity应用事务, 必须调用 commit().  

  在调用commit()以前, 你可能想调用 addToBackStack(),将事务添加到一个fragment事务的backstack. 这个back stack由activity管理, 并容许用户经过按下 BACK按键返回到前一个fragment状态.   
  举个例子, 这里是如何将一个fragment替换为另外一个, 并在后台堆栈中保留以前的状态:

 

    // Create new fragment and transaction  
    Fragment newFragment = new ExampleFragment();  
    FragmentTransaction transaction = getFragmentManager().beginTransaction();  
    // Replace whatever is in the fragment_container view with this fragment,  
    // and add the transaction to the back stack  
    transaction.replace(R.id.fragment_container, newFragment);  
    transaction.addToBackStack(null);  
    // Commit the transaction  
    transaction.commit();  

 

  在这个例子中, newFragment替换了当前layout容器中的由R.id.fragment_container标识的fragment.经过调用 addToBackStack(), replace事务被保存到back stack,所以用户能够回退事务,并经过按下BACK按键带回前一个fragment.  

若是添加多个变化到事务(例如add()或remove())并调用addToBackStack(),而后在你调用commit()以前的全部应用的变化会被做为一个单个事务添加到后台堆栈, BACK按键会将它们一块儿回退.

相关文章
相关标签/搜索