免费高清特黄a大片,九一h片在线免费看,a免费国产一级特黄aa大,国产精品国产主播在线观看,成人精品一区久久久久,一级特黄aa大片,俄罗斯无遮挡一级毛片

分享

Fragment對比Activity -Android碎片介紹

 jemeen 2012-03-01

  Fragment是Android honeycomb 3.0新增的概念,F(xiàn)ragment名為碎片不過卻和Activity十分相似,下面Android123介紹下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,如圖

fragment android

  我們可以看到兩個Activity通過兩個Fragment合并到一個Activity的布局方式,對于平板等大屏幕設(shè)備來說有著不錯的展示面板。不過因為Fragment和Activity的生命周期都比較復(fù)雜,我們分別對比下:

  fragment生命周期

   創(chuàng)建一個fragment你必須創(chuàng)建一個Fragment的子類或存在的子類,比如類似下面的代碼

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

Fragment類的一些代碼看起來有些像Activity為了讓大家了解清楚,Android開發(fā)網(wǎng)給大家整理下Fragment的生命周期如上圖所示,部分類似Activity的,我們詳細解釋

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

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./apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.android123.cwj.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="com.android123.cwj.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

  當然有關(guān)Android Fragment的內(nèi)容由于篇幅的原因,Android123將在明天繼續(xù)幫大家分析下,最后提醒大家Fragment存在于Activity的ViewGroup中,按照繼承關(guān)系大家就可以了解他的結(jié)構(gòu),由于Android 3.0的代碼還沒有開源,所以測試只能從Android honeycomb版本的模擬器上進行了。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多