博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Proxying to the New APIs 代理新的API
阅读量:4047 次
发布时间:2019-05-24

本文共 2664 字,大约阅读时间需要 8 分钟。

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 any exceptions.  http://blog.csdn.net/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 contained object.

Begin implementing CompatTabHoneycomb using the new 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 actual, obtained from its contained .

Implement TabHelperHoneycomb, proxying method calls to the 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.}
你可能感兴趣的文章
欢迎使用CSDN-markdown编辑器
查看>>
Android计算器实现源码分析
查看>>
Android系统构架
查看>>
Android 跨应用程序访问窗口知识点总结
查看>>
各种排序算法的分析及java实现
查看>>
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux irqdebug
查看>>