Android之Fragment靜態實現執行個體

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   使用   ar   strong   

Fragment是作為Activity的UI的一部分,它內嵌在Activity中,多個Fragment可以把一個Activity分成多個部分,這在大螢幕手機或者平板電腦中會比較多的用到,這樣就不用使用多個Activity來切換這麼麻煩了。當然Fragment也可以不顯示,只在幕後處理一些資料,這篇文章中就暫時不談到這個。以下來看怎麼靜態地在Activity的布局檔案中添加Fragment.

  自訂的Fragment通常要繼承Fragment這個類,也有一些特殊的是繼承ListFragment,DialogFragment等。繼承Fragment類通常要實現三個方法:onCreate(), onCreateView(), onPause();

  我在Activity中定義了兩個Fragment,一個是放在左邊的LeftFragment,一個是放在右邊的RightFragment.以下是代碼:首先我們要實現自己的Fragment類

LeftFragment類:

public class LeftFragment extends Fragment{    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        System.out.println("LeftFragment onCreate");    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    {        System.out.println("LeftFragment onCreateView");        // 第一個參數是這個Fragment將要顯示的介面布局,第二個參數是這個Fragment所屬的Activity,第三個參數是決定此fragment是否附屬於Activity        return inflater.inflate(R.layout.leftfragment, container, true);    }    @Override    public void onResume()    {        super.onResume();        System.out.println("LeftFragment onResume");    }        @Override    public void onPause()    {        super.onPause();        System.out.println("LeftFragment onPause");    }            @Override    public void onStop()    {        super.onStop();        System.out.println("LeftFragment onStop");    }}

這裡實現了幾種回呼函數,主要是為了看清Activity和Fragment生命週期之間的關係.其中onCreateView()方法是將本Fragment對應的布局返回給Activity的布局,讓Activity進行載入. inflater.inflate(R.layout.leftfragment, container, true)方法中的第一個參數R.layout.leftfragment是這個Fragment對應的布局檔案ID, 第二個參數container是要插入的目標Activity, 第三個參數是決定這個Fragment是否依附於這個container.

 

LeftFragment對應的布局檔案:

 <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"     android:layout_height="match_parent"      android:background="@android:color/holo_orange_dark"      android:orientation="vertical" >        <Button  android:id="@+id/previous_button"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/previous_button" />      <Button android:id="@+id/next_button"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/next_button" />     <Button android:id="@+id/exit_button"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/exit_button" />  </LinearLayout>

RightFragment類:和LeftFragment類似

public class RightFragment extends Fragment  {      @Override      public void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          System.out.println("RightFragment onCreate");      }           @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)     {         System.out.println("RightFragment onCreateView");         return inflater.inflate(R.layout.rightfragment, container, true);    }         @Override     public void onResume()    {        super.onResume();         System.out.println("RightFragment onResume");     }          @Override     public void onPause()     {         super.onPause();         System.out.println("RightFragment onPause");    }          @Override     public void onStop()     {         super.onStop();         System.out.println("RightFragment onStop");     } }

RightFragment對應的布局檔案:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical" >        <TextView  android:id="@+id/show_message"          android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:background="@android:color/holo_blue_dark"         android:text="@string/show_message" />  </LinearLayout>

最後是Activity的布局檔案:

<?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="horizontal" > 6  7     <fragment 8 android:id="@+id/left_fragment" 9         android:name="com.sunflower.LeftFragment"10         android:layout_width="match_parent"11         android:layout_height="fill_parent"12         android:layout_weight="3" />13 14     <fragment15 android:id="@+id/right_fragment"16         android:name="com.sunflower.RightFragment"17         android:layout_width="match_parent"18         android:layout_height="fill_parent"19         android:layout_weight="1" />20 </LinearLayout>

在Activity中的布局檔案中加入Fragment標籤,其中android:name屬性對應的就是自訂Fragment類的全名,系統會根據這個調用指定的Fragment的onCreateView()方法來得到這個Fragment的布局,然後加入Activity中. onCreateView()方法中的Container參數就是這時候傳遞過去的。

 

看看顯示結果:



開啟程式時生命週期顯示:


按返回鍵時生命週期顯示:

Android之Fragment靜態實現執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.