編輯:關於 Android 程式設計
Android 從 3.0(API Level 11)開始引入 Fragment。可以把 Fragment 想成 Activity 中的一個模塊:它有自己的版面配置、自己的生命週期,能獨立處理自己的輸入事件,並在 Activity 執行期間被動態加入或移除。
正因為它是模塊化的,Fragment 可以被設計成能在多個 Activity 之間重複使用。當同一個應用同時要跑在平板與手機上時,善用 Fragment 就能輕鬆做出彈性的版面配置,大幅改善使用者體驗。
因為 Fragment 必須嵌入 Activity 中使用,兩者的生命週期緊密相關:
但在 Activity 處於活動狀態時,可以獨立控制各 Fragment 的狀態,例如加上或移除某個 Fragment。進行這種 fragment transaction 時,可以把 Fragment 放進 Activity 的 back stack,讓使用者得以回退。
使用 Fragment 時必須繼承 Fragment 或其子類(DialogFragment、ListFragment、PreferenceFragment、WebViewFragment),因此程式碼風格與 Activity 相當類似。
Support Library 是一份提供 API 函式庫的 JAR 檔,讓舊版 Android 也能用上新版的 API,例如 android-support-v4.jar,完整路徑為:
<sdk>/extras/android/support/v4/android-support-v4.jar
它提供了 Fragment 的 API,使 Android 1.6(API Level 4)以上都能使用。為了確保不會在舊系統上誤用新 API,導入時要使用 support 版本的套件:
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager;
同時把 jar 複製到專案 libs 目錄,並在專案 Properties 中補上:右鍵點專案 → Properties → 左側選 Java Build Path → Add External JARs… → 選取 android-support-v4.jar。
另外,建立含 Fragment 的 Activity 時,若使用 Support Library,該 Activity 必須繼承 FragmentActivity 而非 Activity。
onCreate():系統建立 Fragment 時呼叫。在此初始化元件,以及那些即使被暫停或停止也仍要保留的東西。onCreateView():首次繪製 Fragment 的 UI 時呼叫,必須回傳一個 View;若 Fragment 不提供 UI,也可回傳 null。特別注意:若繼承自 ListFragment,此方法預設會回傳一個 ListView,就不必自己實作。onPause():使用者離開 Fragment 時第一個被呼叫的方法。該提交的變更要在此提交,因為使用者很可能不會再回來。要提供 UI,就必須實作 onCreateView()。假設版面写在 example_fragment.xml:
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 把本 Fragment 的版面攤開
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
其中 container 代表這個 Fragment 在 Activity 中的父容器;savedInstanceState 提供上一個實例的資料。
false,因為系統已經會把版面插入父容器;設為 true 會多產生一層多餘的 ViewGroup。Fragment 加入 Activity 後,會處於對應的 ViewGroup 中。加入方式有兩種:在 Activity 版面中用 <fragment> 標籤宣告,或在程式碼中把它加入指定的 ViewGroup。(Fragment 也可以是完全不可見的,這部分此處先略過。)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
android:name 要填你自己建立的 Fragment 完整類別名。系統在攤開這個 Activity 版面時,會逐一實例化每個 Fragment、呼叫它們的 onCreateView() 取得版面,再把回傳值插入 <fragment> 標籤所在的位置。
為 Fragment 提供識別的三種方式:
android:id 屬性 —— 唯一的 idandroid:tag 屬性 —— 唯一字串Activity 處於 Running 狀態時,可以動態把 Fragment 加進版面,只需指定它的父 ViewGroup:
FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();
註:若 import 的是 android.support.v4.app.FragmentManager,則要改用 getSupportFragmentManager()。add() 的第一個參數是父容器 id,最後務必呼叫 commit() 讓變更生效。
這個練習自訂一個 Fragment 類別並寫好版面(兩個 TextView),然後分別用上述兩種方式加入同一個 Activity。
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExampleFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("ExampleFragment--onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("ExampleFragment--onCreateView");
return inflater.inflate(R.layout.example_fragment_layout, container, false);
}
@Override
public void onPause() {
super.onPause();
System.out.println("ExampleFragment--onPause");
}
@Override
public void onResume() {
super.onResume();
System.out.println("ExampleFragment--onResume");
}
@Override
public void onStop() {
super.onStop();
System.out.println("ExampleFragment--onStop");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num2" />
</LinearLayout>
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class LearnFragment extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learn_fragment);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.linear, fragment);
fragmentTransaction.commit();
}
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1" />
<!-- 方式一:版面宣告 -->
<fragment
android:name="com.example.learningfragment.ExampleFragment"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2" />
<!-- 方式二:程式碼加入的目標容器 -->
<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3" />
</LinearLayout>
</LinearLayout>
可以看到:用方式二加入 Fragment 時,指定了父容器(一個 LinearLayout)的 id,而該容器中已有一個 Button 3,所以 Fragment 被加在按鈕之後。
Android中的網絡編程:從 HttpURLConnection 到異步請求與多線程下載
網絡編程是 Android 開發中的基礎能力。無論是加載圖片、登錄驗證、提交表單,還是下載文件,都需要理解請求方式、數據編碼、線程模型與異常處理。 一、訪問網絡資源 An
Android Socket 與多線程聊天功能:客戶端實作
使用 Socket 與多線程可以建立簡單的聊天客戶端。手機端透過 Socket 連線到伺服器,再分別使用輸入與輸出線程處理訊息接收與傳送,从而实现基本的即時通訊。基本流程
Android 自定義 ViewGroup(一)——帶箭頭的圓角矩形菜單
這次要做一个帶箭頭的圓角矩形菜單,大概是這個樣子: 要求:頂部的箭頭要對準菜單錨點,菜單項按壓時反色,背景色與按壓色可配置。 最偷工減料的做法是請 UX 切一張三角形圖
Android 必備知識:Fragment 使用、參數傳遞與宿主通訊
Fragment 是 Android 中用來表示 Activity 中一部分介面的元件。它可以被多個 Activity 重用,也可以在同一個 Activity 中組合多個