欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android應用開發(fā)中使用Fragment的入門學習教程

 更新時間:2016年02月25日 10:32:32   作者:xyz_lmn  
這篇文章主要介紹了Android應用開發(fā)中Fragment的入門學習教程,可以把Fragment看作為Activity基礎之上的模塊,需要的朋友可以參考下

  Fragment是Android honeycomb 3.0開始新增的概念,F(xiàn)ragment名為碎片不過卻和Activity十分相似,下面介紹下Android Fragment的作用和用法。Fragment用來描述一些行為或一部分用戶界面在一個Activity中,你可以合并多個fragment在一個單獨的activity中建立多個UI面板,同時重用fragment在多個activity中.你可以認為fragment作為一個activity中的一節(jié)模塊 ,fragment有自己的生命周期,接收自己的輸入事件,你可以添加或移除從運行中的activity.

       一個fragment必須總是嵌入在一個activity中,同時fragment的生命周期受activity而影響,舉個例子吧,當activity 暫停,那么所有在這個activity的fragments將被destroy釋放。然而當一個activity在運行比如resume時,你可以單獨的操控每個fragment,比如添加或刪除。

       Fragment作為Android 3.0的新特性,有些功能還是比較強大的,比如 合并兩個Activity,如圖

2016225102811270.gif (555×174)

 如上圖所示,用Activity A 表示文章標題列表,ActivityB表示文章具體內(nèi)容。我們可以看到兩個Activity通過兩個Fragment合并到一個Activity的布局方式,對于平板等大屏幕設備來說有著不錯的展示面板。不過因為Fragment和Activity的生命周期都比較復雜,下圖表示的fragments的生命周期:

2016225102831717.gif (319×768)

Activity、Fragment分別對比下:

2016225102909568.gif (403×691)

兩個的生命周期很類似,也息息相關。
 
 
創(chuàng)建一個fragment你必須創(chuàng)建一個Fragment的子類或存在的子類,比如類似下面的代碼

public static class AndroidFragment extends Fragment {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.android_fragment, container, false);
 }
}

 

onCreate()
當fragment創(chuàng)建時被調(diào)用,你應該初始化一些實用的組件,比如在fragment暫停或停止時需要恢復的

onCreateView()
當系統(tǒng)調(diào)用fragment在首次繪制用戶界面時,如果畫一個UI在你的fragment你必須返回一個View當然了你可以返回null代表這個fragment沒有UI.

那么如何添加一個Fragment到Activity中呢? 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.android.cwj.ArticleListFragment"
   android:id="@+id/list"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent" />
 <fragment android:name="com.android.cwj.ArticleReaderFragment"
   android:id="@+id/viewer"
   android:layout_weight="2"
   android:layout_width="0dp"
   android:layout_height="match_parent" />
</LinearLayout>

 通常地 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類.

當系統(tǒng)創(chuàng)建這個activity layout時, 它實例化每一個在layout中指定的fragment,并調(diào)用每一個上的onCreateView()方法,來獲取每一個fragment的layout. 系統(tǒng)將從fragment返回的 View 直接插入到<fragment>元素所在的地方.

注意: 每一個fragment都需要一個唯一的標識, 如果activity重啟,系統(tǒng)可以用來恢復fragment(并且你也可以用來捕獲fragment來處理事務,例如移除它.)

有3種方法來為一個fragment提供一個標識:

  • 為 android:id 屬性提供一個唯一ID.
  • 為 android:tag 屬性提供一個唯一字符串.
  • 如果以上2個你都沒有提供, 系統(tǒng)使用容器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()的第一個參數(shù)是fragment要放入的ViewGroup, 由resource ID指定, 第二個參數(shù)是需要添加的fragment.一旦用FragmentTransaction做了改變,為了使改變生效,必須調(diào)用commit().

相關文章

  • Android原生繪圖工具Paint詳細

    Android原生繪圖工具Paint詳細

    這篇文章要給大家分享的是Android原生繪圖工具Paint,android中提供了類似的工具Canvas和Paint,分別對應畫布和畫筆,本文就來介紹Androi中的Paint,感興趣的小伙伴一起來學習下面文章內(nèi)容
    2021-09-09
  • Android編程獲取地理位置的經(jīng)度和緯度實例

    Android編程獲取地理位置的經(jīng)度和緯度實例

    這篇文章主要介紹了Android編程獲取地理位置的經(jīng)度和緯度實現(xiàn)方法,結合實例形式詳細分析了Android操作系統(tǒng)服務調(diào)用GPS實現(xiàn)定位的相關技巧,需要的朋友可以參考下
    2016-01-01
  • android自動生成dimens適配文件的圖文教程詳解(無需Java工具類)

    android自動生成dimens適配文件的圖文教程詳解(無需Java工具類)

    這篇文章主要介紹了android自動生成dimens適配文件,無需Java工具類,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Android Toast的用法總結(五種用法)

    Android Toast的用法總結(五種用法)

    本篇文章主要介紹了Android Toast的用法總結(五種用法),android toast幾種使用方法 toast經(jīng)常會用到,今天做個總結,特別是自定義toast的布局,值得一看。
    2017-01-01
  • Android貝塞爾曲線實現(xiàn)直播點贊效果

    Android貝塞爾曲線實現(xiàn)直播點贊效果

    這篇文章主要為大家詳細介紹了Android貝塞爾曲線實現(xiàn)直播點贊效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Android ListView 實例講解清晰易懂

    Android ListView 實例講解清晰易懂

    這篇文章主要通過實例介紹了Android ListView,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • 仿Iphone中搜索結果的布局詳解

    仿Iphone中搜索結果的布局詳解

    本篇文章是對仿Iphone中搜索結果的布局進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Android實現(xiàn)斷點下載的方法

    Android實現(xiàn)斷點下載的方法

    這篇文章主要為大家詳細介紹了Android實現(xiàn)斷點下載的方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • android webView截圖的4種方法

    android webView截圖的4種方法

    這篇文章主要為大家詳細介紹了android webView截圖的4種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android編程使用sax解析xml數(shù)據(jù)的方法詳解

    Android編程使用sax解析xml數(shù)據(jù)的方法詳解

    這篇文章主要介紹了Android編程使用sax解析xml數(shù)據(jù)的方法,結合實例形式詳細分析了Android使用sax解析xml數(shù)據(jù)的操作步驟及界面布局、單元測試等相關操作技巧,需要的朋友可以參考下
    2017-07-07

最新評論