พร็อกซีสำหรับ API ใหม่
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บทเรียนนี้แสดงวิธีจัดระดับย่อยของคลาสนามธรรม CompatTab
และ TabHelper
และใช้ API ใหม่ แอปพลิเคชันของคุณสามารถใช้การติดตั้งนี้บนอุปกรณ์ที่ใช้เวอร์ชันแพลตฟอร์มที่รองรับการใช้งาน
ใช้งานแท็บโดยใช้ API ใหม่
คลาสคอนกรีตสำหรับ CompatTab
และ TabHelper
ที่ใช้ API รุ่นใหม่คือการนำพร็อกซีมาใช้ เนื่องจากคลาสนามธรรมที่กำหนดไว้ในบทเรียนก่อนหน้าจะมิเรอร์ API ใหม่ (โครงสร้างคลาส ลายเซ็นเมธอด ฯลฯ) คลาสที่เป็นรูปธรรมที่ใช้ API รุ่นใหม่เหล่านี้เพียงแค่เรียกใช้เมธอดพร็อกซีและผลลัพธ์
คุณสามารถใช้ API รุ่นใหม่กว่าในชั้นเรียนที่เป็นรูปธรรมเหล่านี้ได้โดยตรงและไม่เกิดข้อขัดข้องในอุปกรณ์รุ่นเก่า เนื่องจากการโหลดคลาสแบบ Lazy Loading ระบบจะโหลดและเริ่มต้นชั้นเรียนเมื่อเข้าถึงครั้งแรก ซึ่งเป็นการยืนยันคลาสหรือการเข้าถึงช่องหรือเมธอดแบบคงที่เป็นครั้งแรก ดังนั้น ตราบใดที่คุณไม่ได้สร้างการติดตั้งใช้งานเฉพาะสำหรับ Honeycomb ในอุปกรณ์ Pre-Honeycomb อุปกรณ์ Dalvik VM ก็จะไม่ส่งข้อยกเว้น VerifyError
รูปแบบการตั้งชื่อที่ดีสำหรับการติดตั้งใช้งานนี้คือการเพิ่มระดับ API หรือชื่อโค้ดเวอร์ชันของแพลตฟอร์มที่สอดคล้องกับ API ที่จำเป็นสำหรับคลาสที่เป็นรูปธรรม เช่น คลาส CompatTabHoneycomb
และ TabHelperHoneycomb
สามารถติดตั้งใช้งานแท็บเนทีฟได้ เนื่องจากคลาสเหล่านี้ต้องใช้ API ที่มีอยู่ใน Android 3.0 (API ระดับ 11) ขึ้นไป
รูปที่ 1 แผนภาพชั้นเรียนสำหรับการใช้งานแท็บ Honeycomb
ใช้ CompatTabHoneycomb
CompatTabHoneycomb
คือการใช้งานคลาสนามธรรม CompatTab
ที่ TabHelperHoneycomb
ใช้เพื่ออ้างอิงแต่ละแท็บ CompatTabHoneycomb
เพียงพร็อกซีการเรียกเมธอดทั้งหมดไปยังออบเจ็กต์ ActionBar.Tab
ที่มีอยู่
เริ่มใช้ CompatTabHoneycomb
โดยใช้ ActionBar.Tab
API ใหม่:
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
TabHelperHoneycomb
คือการใช้งานคลาส Abstract TabHelper
ที่เรียกใช้เมธอดพร็อกซีไปยัง ActionBar
จริง ซึ่งได้มาจาก Activity
ที่มีอยู่
ใช้ TabHelperHoneycomb
การเรียกเมธอดพร็อกซีไปยัง 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.
}
คุณควรอ่าน
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Proxy to the new APIs\n\nThis lesson shows you how to subclass the `CompatTab` and `TabHelper` abstract classes and use new APIs. Your application can use this implementation on devices running a platform version that supports them.\n\nImplement tabs using new APIs\n-----------------------------\n\nThe concrete classes for `CompatTab` and `TabHelper` that use newer APIs are a *proxy* implementation. Since the abstract classes defined in the previous lesson mirror the new APIs (class structure, method signatures, etc.), the concrete classes that use these newer APIs simply proxy method calls and their results.\n\nYou 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 [VerifyError](/reference/java/lang/VerifyError) exceptions.\n\nA 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 by `CompatTabHoneycomb` and `TabHelperHoneycomb` classes, since they rely on APIs available in Android 3.0 (API level 11) or later.\n\n**Figure 1.** Class diagram for the Honeycomb implementation of tabs.\n\nImplement CompatTabHoneycomb\n----------------------------\n\n`CompatTabHoneycomb` is the implementation of the `CompatTab` abstract class that `TabHelperHoneycomb` uses to reference individual tabs. `CompatTabHoneycomb` simply proxies all method calls to its contained [ActionBar.Tab](/reference/android/app/ActionBar.Tab) object.\n\nBegin implementing `CompatTabHoneycomb` using the new [ActionBar.Tab](/reference/android/app/ActionBar.Tab) APIs: \n\n### Kotlin\n\n```kotlin\nclass CompatTabHoneycomb internal constructor(val activity: Activity, tag: String) :\n CompatTab(tag) {\n ...\n // The native tab object that this CompatTab acts as a proxy for.\n private var mTab: ActionBar.Tab =\n // Proxy to new ActionBar.newTab API\n activity.actionBar.newTab()\n\n override fun setText(@StringRes textId: Int): CompatTab {\n // Proxy to new ActionBar.Tab.setText API\n mTab.setText(textId)\n return this\n }\n\n ...\n // Do the same for other properties (icon, callback, etc.)\n}\n```\n\n### Java\n\n```java\npublic class CompatTabHoneycomb extends CompatTab {\n // The native tab object that this CompatTab acts as a proxy for.\n ActionBar.Tab mTab;\n ...\n\n protected CompatTabHoneycomb(FragmentActivity activity, String tag) {\n ...\n // Proxy to new ActionBar.newTab API\n mTab = activity.getActionBar().newTab();\n }\n\n public CompatTab setText(int resId) {\n // Proxy to new ActionBar.Tab.setText API\n mTab.setText(resId);\n return this;\n }\n\n ...\n // Do the same for other properties (icon, callback, etc.)\n}\n```\n\nImplement TabHelperHoneycomb\n----------------------------\n\n`TabHelperHoneycomb` is the implementation of the `TabHelper` abstract class that proxies method calls to an actual [ActionBar](/reference/android/app/ActionBar), obtained from its contained [Activity](/reference/android/app/Activity).\n\nImplement `TabHelperHoneycomb`, proxying method calls to the [ActionBar](/reference/android/app/ActionBar) API: \n\n### Kotlin\n\n```kotlin\nclass TabHelperHoneycomb internal constructor(activity: FragmentActivity) : TabHelper(activity) {\n\n private var mActionBar: ActionBar? = null\n\n override fun setUp() {\n mActionBar = mActionBar ?: mActivity.actionBar.apply {\n navigationMode = ActionBar.NAVIGATION_MODE_TABS\n }\n }\n\n override fun addTab(tab: CompatTab) {\n // Tab is a CompatTabHoneycomb instance, so its\n // native tab object is an ActionBar.Tab.\n mActionBar?.addTab(tab.getTab() as ActionBar.Tab)\n }\n}\n```\n\n### Java\n\n```java\npublic class TabHelperHoneycomb extends TabHelper {\n ActionBar actionBar;\n ...\n\n protected void setUp() {\n if (actionBar == null) {\n actionBar = activity.getActionBar();\n actionBar.setNavigationMode(\n ActionBar.NAVIGATION_MODE_TABS);\n }\n }\n\n public void addTab(CompatTab tab) {\n ...\n // Tab is a CompatTabHoneycomb instance, so its\n // native tab object is an ActionBar.Tab.\n actionBar.addTab((ActionBar.Tab) tab.getTab());\n }\n\n // The other important method, newTab() is part of\n // the base implementation.\n}\n```\n\n### You should also read\n\n- [Action Bar](/guide/topics/ui/actionbar)\n- [Action Bar Tabs](/guide/topics/ui/actionbar#Tabs)"]]