編輯:Android 開發實例
大量內容需要分類呈現時,Tab 分頁標籤是客戶端與瀏覽器最常見的作法。除了大家熟悉的 TabHost,其實還可以用 ImageButton + ActivityGroup 這條路:把子 Activity 的 Window 轉成 View,掛到 ActivityGroup 指定的容器裡。本文就用 LinearLayout 當容器,示範如何裝載子 Activity。
這個做法有個很值得注意的副作用——每次切換分頁,前一個子 Activity 會被徹底銷毀、資源完全釋放。從 subActivity1 切到 subActivity2 時,你能在 Log 裡清楚看到 subActivity1 走完 onStop 與 onDestroy,而 onCreate 與 onDestroy 成對出現。這代表切換乾淨、不留殘影,但也意味著每次切頁都要重跑一次建立流程,若子畫面初始化成本很高,效能上要自己拿捏。
上方一列放兩個 ImageButton 當分頁切換鈕,下方留一個空的 LinearLayout 作為子 Activity 的落腳處:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ibtnTab1"
android:background="@drawable/png1298"></ImageButton>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ibtnTab2"
android:background="@drawable/png1292"></ImageButton>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</LinearLayout>
兩個分頁共用同一份配置,只靠傳入的參數決定清單樣式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/MyListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
主類別繼承 ActivityGroup。點擊分頁鈕時先清空容器,再用 Intent 帶上 this 專屬的參數(清單文字、標籤名稱、選擇模式),交給 getLocalActivityManager().startActivity() 啟動子 Activity,最後把它的 DecorView 加進容器。
package com.testActivityGroup;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
public class testActivityGroup extends ActivityGroup {
LinearLayout container; // 裝載 sub Activity 的容器
ImageButton ibtnTab1, ibtnTab2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
container = (LinearLayout) findViewById(R.id.LinearLayout02);
ibtnTab1 = (ImageButton) this.findViewById(R.id.ibtnTab1);
ibtnTab1.setOnClickListener(new ClickEvent());
ibtnTab2 = (ImageButton) this.findViewById(R.id.ibtnTab2);
ibtnTab2.setOnClickListener(new ClickEvent());
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
container.removeAllViews();
Intent intent = new Intent(testActivityGroup.this, subActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String[] str = new String[12];
if (v == ibtnTab1) {
for (int i = 0; i < str.length; i++)
str[i] = "單選" + String.valueOf(i);
intent.putExtra("Name", "subActivity1");
intent.putExtra("Strings", str);
intent.putExtra("ChoiceMode", ListView.CHOICE_MODE_SINGLE); // 單選樣式
} else if (v == ibtnTab2) {
for (int i = 0; i < str.length; i++)
str[i] = "復選" + String.valueOf(i);
intent.putExtra("Name", "subActivity2");
intent.putExtra("Strings", str);
intent.putExtra("ChoiceMode", ListView.CHOICE_MODE_MULTIPLE); // 多選樣式
}
Window subActivity = getLocalActivityManager().startActivity("subActivity", intent);
container.addView(subActivity.getDecorView());
}
}
}
子 Activity 從 Intent 取出標籤名稱、字串陣列與選擇模式,再依模式挑選對應的系統清單樣式(單選、多選或一般項目)。每個生命週期方法都寫了 Log,方便直接觀察切頁時的資源釋放順序。
package com.testActivityGroup;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class subActivity extends Activity {
String name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// 讀取傳入的參數
name = this.getIntent().getStringExtra("Name");
String[] str = this.getIntent().getStringArrayExtra("Strings");
int choiceMode = this.getIntent().getIntExtra("ChoiceMode", ListView.CHOICE_MODE_NONE);
ListView listView = (ListView) findViewById(R.id.MyListView);
// 依選擇模式決定項目樣式
int itemID = android.R.layout.simple_list_item_1;
if (choiceMode == ListView.CHOICE_MODE_MULTIPLE)
itemID = android.R.layout.simple_list_item_multiple_choice;
else if (choiceMode == ListView.CHOICE_MODE_SINGLE)
itemID = android.R.layout.simple_list_item_single_choice;
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, itemID, str);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(choiceMode);
Log.e(name, "onCreate"); // onCreate 與 onDestroy 成對
}
@Override public void onDestroy() { super.onDestroy(); Log.e(name, "onDestroy"); }
@Override public void onStart() { super.onStart(); Log.e(name, "onStart"); }
@Override public void onStop() { super.onStop(); Log.e(name, "onStop"); }
@Override public void onRestart() { super.onRestart(); Log.e(name, "onRestart"); }
@Override public void onResume() { super.onResume(); Log.e(name, "onResume"); }
@Override public void onPause() { super.onPause(); Log.e(name, "onPause"); }
}
ActivityGroup 的價值在於「一個 Activity 內承載多個子畫面」,適合邏輯相關、需要共用外框的分頁場景。不過它屬於較早期的寫法,新專案若只是要 Tab 效果,優先考慮 Fragment 加 ViewPager 會更好維護;理解 ActivityGroup 的運作,則有助於讀懂老專案與 LocalActivityManager 的設計脈絡。
Android SQLite 快速入門
SQLite 是 Android 常用的輕量級嵌入式資料庫,以單一檔案保存資料,資源占用低,適合本地結構化資料儲存。基本概念SQLite 以檔案為資料庫,一個檔案可包含多
Android Service 全面整理:分類、生命週期與使用方式
Service 是 Android 四大元件之一,但也最常被誤解成「執行緒」。這篇把它的分類、與 Thread 的差異、生命週期、啟動與綁定方式,以及 manifest
Android 進度條教學:用 Spinner 顯示不確定載入狀態
App 在等待網路回應或讀取大量資料時,如果畫面完全靜止,使用者很可能以為程式當掉了。一個持續旋轉的載入指示器,就能有效安撫這種焦慮。 Android 用 Progre
Android Toast 提示訊息使用教學
Toast 是 Android 中最常見的輕量提示方式。它會短暫顯示訊息,然後自動消失,不需要使用者點擊確認,也不會中斷目前 Activity 的操作。Toast 的使用