Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 入門第十五篇:用 ActivityGroup 打造 Tab 分頁標籤

編輯:Android 開發實例

大量內容需要分類呈現時,Tab 分頁標籤是客戶端與瀏覽器最常見的作法。除了大家熟悉的 TabHost,其實還可以用 ImageButton + ActivityGroup 這條路:把子 Activity 的 Window 轉成 View,掛到 ActivityGroup 指定的容器裡。本文就用 LinearLayout 當容器,示範如何裝載子 Activity。

先想清楚:子 Activity 的生命週期

這個做法有個很值得注意的副作用——每次切換分頁,前一個子 Activity 會被徹底銷毀、資源完全釋放。從 subActivity1 切到 subActivity2 時,你能在 Log 裡清楚看到 subActivity1 走完 onStop 與 onDestroy,而 onCreate 與 onDestroy 成對出現。這代表切換乾淨、不留殘影,但也意味著每次切頁都要重跑一次建立流程,若子畫面初始化成本很高,效能上要自己拿捏。

主畫面配置:main.xml

上方一列放兩個 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>

子畫面配置:listview.xml

兩個分頁共用同一份配置,只靠傳入的參數決定清單樣式:

<?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>

testActivityGroup.java:分頁的调度中心

主類別繼承 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());
        }
    }
}

subActivity.java:一個會自己看參數的子頁

子 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?

ActivityGroup 的價值在於「一個 Activity 內承載多個子畫面」,適合邏輯相關、需要共用外框的分頁場景。不過它屬於較早期的寫法,新專案若只是要 Tab 效果,優先考慮 Fragment 加 ViewPager 會更好維護;理解 ActivityGroup 的運作,則有助於讀懂老專案與 LocalActivityManager 的設計脈絡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved