Abstracting the New APIs 抽象出新的API
来源:互联网 发布:cisco查看mac地址表 编辑:程序博客网 时间:2024/05/20 14:26
Suppose you want to use action bar tabs as the primary form of top-level navigation in your application. Unfortunately, theActionBar
APIs are only available in Android 3.0 or later (API level 11+). Thus, if you want to distribute your application to devices running earlier versions of the platform, you need to provide an implementation that supports the newer API while providing a fallback mechanism that uses older APIs.
In this class, you build a tabbed user interface (UI) component that uses abstract classes with version-specific implementations to provide backward-compatibility. This lesson describes how to create an abstraction layer for the new tab APIs as the first step toward building the tab component. http://blog.csdn.net/sergeycao
Prepare for Abstraction
Abstraction in the Java programming language involves the creation of one or more interfaces or abstract classes to hide implementation details. In the case of newer Android APIs, you can use abstraction to build version-aware components that use the current APIs on newer devices, and fallback to older, more compatible APIs on older devices.
When using this approach, you first determine what newer classes you want to be able to use in a backward compatible way, then create abstract classes, based on the public interfaces of the newer classes. In defining the abstraction interfaces, you should mirror the newer API as much as possible. This maximizes forward-compatibility and makes it easier to drop the abstraction layer in the future when it is no longer necessary.
After creating abstract classes for these new APIs, any number of implementations can be created and chosen at runtime. For the purposes of backward-compatibility, these implementations can vary by required API level. Thus, one implementation may use recently released APIs, while others can use older APIs.
Create an Abstract Tab Interface
In order to create a backward-compatible version of tabs, you should first determine which features and specific APIs your application requires. In the case of top-level section tabs, suppose you have the following functional requirements:
- Tab indicators should show text and an icon.
- Tabs can be associated with a fragment instance.
- The activity should be able to listen for tab changes.
Preparing these requirements in advance allows you to control the scope of your abstraction layer. This means that you can spend less time creating multiple implementations of your abstraction layer and begin using your new backward-compatible implementation sooner.
The key APIs for tabs are in ActionBar
and ActionBar.Tab
. These are the APIs to abstract in order to make your tabs version-aware. The requirements for this example project call for compatibility back to Eclair (API level 5) while taking advantage of the new tab features in Honeycomb (API Level 11). A diagram of the class structure to support these two implementations and their abstract base classes (or interfaces) is shown below.
Abstract ActionBar.Tab
Get started on building your tab abstraction layer by creating an abstract class representing a tab, that mirrors theActionBar.Tab
interface:
public abstract class CompatTab { ... public abstract CompatTab setText(int resId); public abstract CompatTab setIcon(int resId); public abstract CompatTab setTabListener( CompatTabListener callback); public abstract CompatTab setFragment(Fragment fragment); public abstract CharSequence getText(); public abstract Drawable getIcon(); public abstract CompatTabListener getCallback(); public abstract Fragment getFragment(); ...}
You can use an abstract class instead of an interface here to simplify the implementation of common features such as association of tab objects with activities (not shown in the code snippet).
Abstract ActionBar Tab Methods
Next, define an abstract class that allows you to create and add tabs to an activity, likeActionBar.newTab()
and ActionBar.addTab()
:
public abstract class TabHelper { ... public CompatTab newTab(String tag) { // This method is implemented in a later lesson. } public abstract void addTab(CompatTab tab); ...}
In the next lessons, you create implementations for TabHelper
andCompatTab
that work across both older and newer platform versions.
- Abstracting the New APIs 抽象出新的API
- Proxying to the New APIs 代理新的API
- Dynamics CRM 2015 Update 1 系列(3): API的那些事 - Old APIs VS New APIs
- 用较早版本的APIs实现抽象类
- Intro to the three Android Audio APIs 介绍 Android的三种音频API。
- Provable APIs(可查的API)
- [WUA APIs]Using the Windows Update Agent API
- Creating an Implementation with Older APIs 用较早版本的APIs实现抽象类
- iOS 8 by Tutorials: Learning the New iOS 8 APIs with Swift (包括源码)下载
- C++抽象编程 Programming Abstracting in C++ 全书笔记(未完结)
- 使用SL4J桥接遗留的API(Bridging legacy APIs)
- 【APIs — A Strategy Guide】第一章 API的机遇
- Using the Runtime APIs
- Property Animations: The New Animation API
- Using the Java APIs for Bluetooth Wireless Technology, Part 1 - API Overview
- the SDK platform-tools version(xxx) is too old to check APIs cimpiled with API xxx.
- The SDK platform-tools version(24) is too old to check APIs compiled with API 25
- The SDK platform-tools version 23 is too old to check APIs compiled with API 24... ...
- 怎样在filter-mapping中配制多个url-pattern?
- Creating Multiple APKs with 2+ Dimensions 创建两种以上屏幕尺寸多apk支持
- ExtJS中get、getDom、getCmp、getBody、getDoc使用 javascript
- Android开发之Services详解
- BitBlaze(五) - 应用及相关工作
- Abstracting the New APIs 抽象出新的API
- 《北极熊为什么是白色的——人人都需要知道的进化论》读书笔记
- file外部表--有更新版本!
- Proxying to the New APIs 代理新的API
- 设置C#编译环境
- 书后思考:输出三个数中的中间数。从小到大输出三个数
- java实现zip压缩文件/文件夹
- Creating an Implementation with Older APIs 用较早版本的APIs实现抽象类
- 不能为虚拟电脑 xxx 打开一个新任务. Failed to load VMMR0.r0 (VERR_SUPLIB_OWNER_NOT_ROOT). 返回 代码:NS_ERROR_FAIL