代理至新的APIs

编写: spencer198711 - 原文:http://developer.android.com/training/backward-compatible-ui/new-implementation.html

这一课展示了如何编写CompatTab和TabHelper等抽象类的子类,并且使用了较新的APIs。你的应用可以在支持这些新的APIs的平台版本的设备上使用这种实现方式。

使用较新的APIs实现Tabs

CompatTab和TabHelper抽象类的具体子类是一种代理实现,它们使用了使用较新的APIs。由于抽象类在之前的课程中定义并且是对新APIs接口(类结构、方法签名等等)的镜像,使用新APIs的具体子类只是简单的代理方法调用和方法调用的结果。

你可以在这些具体子类中直接使用较新的APIs,由于使用延迟类加载的方式,在早期版本的设备上并不会发生崩溃现象。这些类在首次次被访问(实例化类对象或者访问类的静态属性或静态方法)的时候才会去加载并初始化。因此,只要你不在Honeycomb之前的设备上实例化Honeycomb相关的实现,dalvik虚拟机都不会抛出VerifyError异常。

对于本实现,一个比较好的命名约定是把具体子类需要的API等级或者版本名字附加在APIs接口的后边。例如,本地tab实现可以由CompatTabHoneycombabHelperHoneycomb这两个类提供,名字后面附加Honeycomb是由于它们都依赖于Android 3.0(API等级11)之后版本的APIs。

backward-compatible-ui-classes-honeycomb

  • 图1. Honeycomb上tabs实现的类关系图.

实现CompatTabHoneycomb

CompatTabHoneycombCompatTab抽象类的具体实现并用来引用单独的tabs。CompatTabHoneycomb只是简单的代理ActionBar.Tab对象的方法调用。 开始使用ActionBar.Tab的APIs实现CompatTabHoneycomb:

  1. public class CompatTabHoneycomb extends CompatTab {
  2. // The native tab object that this CompatTab acts as a proxy for.
  3. ActionBar.Tab mTab;
  4. ...
  5. protected CompatTabHoneycomb(FragmentActivity activity, String tag) {
  6. ...
  7. // Proxy to new ActionBar.newTab API
  8. mTab = activity.getActionBar().newTab();
  9. }
  10. public CompatTab setText(int resId) {
  11. // Proxy to new ActionBar.Tab.setText API
  12. mTab.setText(resId);
  13. return this;
  14. }
  15. ...
  16. // Do the same for other properties (icon, callback, etc.)
  17. }

实现TabHelperHoneycomb

TabHelperHoneycombTabHelper抽象类的具体实现,TabHelperHoneycomb代理方法调用到ActionBar对象,而这个ActionBar对象是从包含他的Activity中获取的。

实现TabHelperHoneycomb,代理其方法调用到ActionBar的API:

  1. public class TabHelperHoneycomb extends TabHelper {
  2. ActionBar mActionBar;
  3. ...
  4. protected void setUp() {
  5. if (mActionBar == null) {
  6. mActionBar = mActivity.getActionBar();
  7. mActionBar.setNavigationMode(
  8. ActionBar.NAVIGATION_MODE_TABS);
  9. }
  10. }
  11. public void addTab(CompatTab tab) {
  12. ...
  13. // Tab is a CompatTabHoneycomb instance, so its
  14. // native tab object is an ActionBar.Tab.
  15. mActionBar.addTab((ActionBar.Tab) tab.getTab());
  16. }
  17. // The other important method, newTab() is part of
  18. // the base implementation.
  19. }