使用新 API 代理執行

本課程將說明如何將 CompatTabTabHelper 抽象類別設為子類別,並使用新版 API。您的應用程式可以在支援這些實作方式的平台版本上使用。

使用新版 API 實作分頁

使用較新 API 的 CompatTabTabHelper 具體類別是「Proxy」實作項目。由於上一堂課中定義的抽象類別會反映新的 API (類別結構、方法簽章等),因此使用這些新版 API 的具體類別會代理方法呼叫及其結果。

由於類別延遲載入,您可以在這些具體類別中直接使用較新的 API,且不會在舊版裝置上當機。類別會在首次存取時載入及初始化,也就是首次例項化類別,或存取類別的其中一個靜態欄位或方法。因此,只要您不在 Honeycomb 之前的裝置上例項化 Honeycomb 專屬的實作項目,Dalvik VM 就不會擲回任何 VerifyError 例外狀況。

這個實作項目的良好命名慣例是附加與具體類別所需 API 相對應的 API 級別或平台版本產品代號。舉例來說,由於原生分頁實作項目依賴 Android 3.0 (API 級別 11) 以上版本提供的 API,因此可由 CompatTabHoneycombTabHelperHoneycomb 類別提供。

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

實作 CompatTabHoneycomb

CompatTabHoneycombCompatTab 抽象類別的實作項目,TabHelperHoneycomb 用於參照個別分頁。CompatTabHoneycomb 會將所有方法呼叫作業代理至所含的 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.
}

另請參閱