編輯:關於 Android 程式設計
Android 3.0開始引入了一個新概念——Fragment。它有自己的版面配置檔,可以作為元件排布,也能相互組合去實現不同的版面顯示。使用Fragment可以重複利用程式碼,並滿足不同裝置尺寸的需求。Fragment不能單獨存在,只能存在於Activity中,而一個Activity可以擁有多個Fragment。很重要的一點是,Fragment可以和Activity中的其他元件一起使用,無需重寫所有Activity的介面。所以使用Fragment就能這樣完成「主介面—詳細介面」的App需求。
在手機上是這樣顯示的:

而在平板上是這樣的:

在一個小螢幕的裝置上,一個Activity通常占據整個螢幕,同時顯示各種UI視圖元件,Activity實際上就是視圖的容器。而當一個Activity被顯示在大螢幕裝置(例如平板電腦)上時,總會顯得有些不適應:螢幕太大,Activity中的所有UI元件要充滿整個螢幕,這樣一來視圖的層次結構就很複雜了。一個更好的辦法,是使用一種「輕量級」的Activity,每個「輕量級」Activity包含自己的視圖、互不干擾,在執行期間根據螢幕的方向和尺寸,一個Activity可以包含一個或多個「輕量級」Activity。在Android 3.0以上版本,這種「輕量級」Activity叫做Fragment。
怎麼建立一個Fragment
現在我們了解了Fragment的生命週期,接著就需要知道怎麼建立一個Fragment並綁定到Activity中。第一件要做的事,就是繼承android.app.Fragment來寫一個Fragment。假設我們的Fragment叫做Fragment1,建立與定義如下:
public class Fragment1 extends Fragment { ... } 就像我們上面說的,Fragment只能存在於Activity中,所以我們必須在某處定義它,有兩種方式:
- 直接在xml版面配置檔中定義;
- 在xml版面配置檔中定義一個佔位符,然後動態地在Activity中操作Fragment;
我們定義Fragment的方式會影響它的生命週期,因為在第一種情況下onInflate方法會被呼叫,而第二種情況下它的生命週期是從onAttach方法開始的。
如果我們在XML檔中定義Fragment,需要:
<fragment android:id="@+id/f1" class="com.survivingwithandroid.fragment.Fragment1" android:layout_width="match_parent" android:layout_height="20dp"/>
然而如果我們在XML中用佔位符,就需要再做一些工作。
版面配置框架和Fragment
如果我們在XML版面配置檔中定義Fragment,就不能自由、動態修改Fragment了。還有別的方法可以讓我們更靈活地操作:使用時需要在XML檔中定義:
<FrameLayout android:id="@+id/fl1" android:layout_width="match_parent" android:layout_height="200dp"/>
在Activity裡面還需要做一點工作,因為我們必須手動初始化Fragment,然後把它「插入」到FrameLayout中。
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Fragment2 f2 = new Fragment2(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.fl1, f2); ft.commit(); } 例子
可以把Fragment想象成Activity的另外一種形式。你建立fragment去包含UI元件,就像建立Activity那樣,但是Fragment總是被嵌在Activity中。
下面透過一個例子來看一下流程:
1.建立一個名為Fragments的工程。
2.在res/layout文件夾下,新建一個叫fragment1.xml的檔。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00FF00" android:orientation="vertical" > <TextView android:id="@+id/lblFragment1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #1" android:textColor="#000000" android:textSize="25sp" /> </LinearLayout>
3.在res/layout文件夾下,新建一個叫fragment2.xml的檔。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFE00" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #2" android:textColor="#000000" android:textSize="25sp" /> <Button android:id="@+id/btnGetText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Get text in Fragment #1" android:textColor="#000000" /> </LinearLayout>
4.main.xml中的程式碼。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="net.learn2develop.Fragments.Fragment1" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="net.learn2develop.Fragments.Fragment2" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
5.新建兩個類:Fragment1.java和Fragment2.java。
6.Fragment1.java中的程式碼。
package net.learn2develop.Fragments; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.d("Fragment 1", "onCreateView"); // ---Inflate the layout for this fragment--- return inflater.inflate(R.layout.fragment1, container, false); } } 7.Fragment2.java中的程式碼。
package net.learn2develop.Fragments; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // ---Inflate the layout for this fragment--- return inflater.inflate(R.layout.fragment2, container, false); } }
ActionBar、Tabs、Fragment、ViewPager 實現標籤頁切換並快取頁面
今天來談談如何用 ActionBar、Tabs、Fragment 與 ViewPager 實現標籤頁切換並快取頁面。關於它們的介紹網上不勝枚舉,這裡只講實作中容易踩到的幾
自定義控制元件系列:Touch 事件的傳遞、分發與攔截
想寫好自訂 View,Touch 事件機制是繞不過去的一關。它牽涉到三個方法在父子層級之間的流轉,以及 boolean 回傳值如何改變整條鏈的走向。與其背結論,不如自己打
安卓手機怎麼改 HOSTS?解決 Google 服務無法登入
不少安卓使用者都碰過 Google 帳號登不入、Play 商店打不開的情況。電腦端我們會改 hosts 繞過解析問題,手機上其實也能做,只是需要先取得 root 權限。
RecyclerView 進階:Item 拖拽排序與滑動刪除的完整封裝
效果先看 需求拆解與技術選型 要實現兩個交互: 拖拽排序——長按 RecyclerView 的 Item,或者觸摸 Item 上的某個指定按鈕,進入拖拽狀態,放手後完