使用新 API 代理執行

本課程將說明如何為 CompatTabTabHelper 抽象類別建立子類別,並使用新的 API。如果裝置搭載的平台版本支援這項實作功能,您就可以使用這項功能。

使用新 API 實作分頁

使用較新 API 的 CompatTabTabHelper 具體類別是 Proxy 實作。由於上一個課程中定義的抽象類別會反映新 API (類別結構、方法簽章等),因此採用這些新版 API 的具體類別只會透過 Proxy 方法呼叫及其結果。

由於延遲類別載入,您可以在這些具體類別中使用新版 API,而不會在舊版裝置上當機。類別會在第一次存取時載入並初始化,也就是首次存取該類別或存取其其中一個靜態欄位或方法。因此,如果您未在 Honeycomb 之前的裝置上將 Honeycomb 特定實作執行個體化,Dalvik VM 就不會擲回任何 VerifyError 例外狀況。

為這項實作作業良好的命名慣例,就是將相應的 API 級別或平台版本代碼名稱附加至具體類別所需的 API。舉例來說,CompatTabHoneycombTabHelperHoneycomb 類別可以提供原生分頁實作項目,因為這些類別仰賴 Android 3.0 (API 級別 11) 以上版本中的 API。

Honeycomb 實作分頁的類別圖表。

圖 1 Honeycomb 實作分頁的類別圖表。

導入 CompatTabHoneycomb

CompatTabHoneycombCompatTab 抽象類別的實作,TabHelperHoneycomb 會使用該類別參照個別分頁。CompatTabHoneycomb 只會透過 Proxy 對其內含的 ActionBar.Tab 物件發出所有方法呼叫。

使用新的 ActionBar.Tab API 開始實作 CompatTabHoneycomb

Kotlin

class CompatTabHoneycomb internal constructor(val activity: Activity, tag: String) :
        CompatTab(tag) {
    ...
    // The native tab object that this CompatTab acts as a proxy for.
    private var mTab: ActionBar.Tab =
            // Proxy to new ActionBar.newTab API
            activity.actionBar.newTab()

    override fun setText(@StringRes textId: Int): CompatTab {
        // Proxy to new ActionBar.Tab.setText API
        mTab.setText(textId)
        return this
    }

    ...
    // Do the same for other properties (icon, callback, etc.)
}

Java

public class CompatTabHoneycomb extends CompatTab {
    // The native tab object that this CompatTab acts as a proxy for.
    ActionBar.Tab mTab;
    ...

    protected CompatTabHoneycomb(FragmentActivity activity, String tag) {
        ...
        // Proxy to new ActionBar.newTab API
        mTab = activity.getActionBar().newTab();
    }

    public CompatTab setText(int resId) {
        // Proxy to new ActionBar.Tab.setText API
        mTab.setText(resId);
        return this;
    }

    ...
    // Do the same for other properties (icon, callback, etc.)
}

實作 TabHelperHoneycomb

TabHelperHoneycombTabHelper 抽象類別的實作,可透過 Proxy 方法呼叫實際的 ActionBar,從其內含的 Activity 取得。

實作 TabHelperHoneycomb,以 Proxy 方法呼叫 ActionBar API:

Kotlin

class TabHelperHoneycomb internal constructor(activity: FragmentActivity) : TabHelper(activity) {

    private var mActionBar: ActionBar? = null

    override fun setUp() {
        mActionBar = mActionBar ?: mActivity.actionBar.apply {
            navigationMode = ActionBar.NAVIGATION_MODE_TABS
        }
    }

    override fun addTab(tab: CompatTab) {
        // Tab is a CompatTabHoneycomb instance, so its
        // native tab object is an ActionBar.Tab.
        mActionBar?.addTab(tab.getTab() as ActionBar.Tab)
    }
}

Java

public class TabHelperHoneycomb extends TabHelper {
    ActionBar actionBar;
    ...

    protected void setUp() {
        if (actionBar == null) {
            actionBar = activity.getActionBar();
            actionBar.setNavigationMode(
                    ActionBar.NAVIGATION_MODE_TABS);
        }
    }

    public void addTab(CompatTab tab) {
        ...
        // Tab is a CompatTabHoneycomb instance, so its
        // native tab object is an ActionBar.Tab.
        actionBar.addTab((ActionBar.Tab) tab.getTab());
    }

    // The other important method, newTab() is part of
    // the base implementation.
}

另請參閱