Proxying to the New APIs 代理新的API

本文详细介绍了如何在较旧的设备上使用新API实现Tab功能,通过代理类CompatTabHoneycomb和TabHelperHoneycomb来引用单个Tab,并利用Action Bar的API进行属性设置。

Implement Tabs Using New APIs

The concrete classes for CompatTab and TabHelper that use newer APIs are aproxy implementation.

You can directly use newer APIs in these concrete classes—and not crash on earlier devices—because of lazy class loading. Classes are loaded and initialized on first access—instantiating the class or accessing one of its static fields or methods for the first time. Thus, as long as you don't instantiate the Honeycomb-specific implementations on pre-Honeycomb devices, the Dalvik VM won't throw anyVerifyError exceptions. http://blog.youkuaiyun.com/sergeycao

A good naming convention for this implementation is to append the API level or platform version code name corresponding to the APIs required by the concrete classes. For example, the native tab implementation can be provided byCompatTabHoneycomb and TabHelperHoneycomb classes, since they rely on APIs available in Android 3.0 (API level 11) or later.


Figure 1. Class diagram for the Honeycomb implementation of tabs.

Implement CompatTabHoneycomb

CompatTabHoneycomb is the implementation of the CompatTab abstract class thatTabHelperHoneycomb uses to reference individual tabs. CompatTabHoneycomb simply proxies all method calls to its containedActionBar.Tab object.

Begin implementing CompatTabHoneycomb using the new ActionBar.Tab APIs:

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.)
}

Implement TabHelperHoneycomb

TabHelperHoneycomb is the implementation of the TabHelper abstract class that proxies method calls to an actualActionBar, obtained from its contained Activity.

Implement TabHelperHoneycomb, proxying method calls to the ActionBar API:

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

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

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

    // The other important method, newTab() is part of
    // the base implementation.
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值