پروکسی به API های جدید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این درس به شما نشان میدهد که چگونه کلاسهای انتزاعی CompatTab
و TabHelper
را زیر کلاسبندی کنید و از APIهای جدید استفاده کنید. برنامه شما میتواند از این پیادهسازی در دستگاههایی که نسخه پلتفرمی را پشتیبانی میکنند استفاده کند.
تب ها را با استفاده از API های جدید پیاده سازی کنید
کلاس های مشخص برای CompatTab
و TabHelper
که از API های جدیدتر استفاده می کنند، یک پیاده سازی پروکسی هستند. از آنجایی که کلاس های انتزاعی تعریف شده در درس قبلی منعکس کننده API های جدید (ساختار کلاس، امضای روش و غیره) هستند، کلاس های ملموسی که از این API های جدیدتر استفاده می کنند، به سادگی فراخوانی های متد پراکسی و نتایج آن ها را انجام می دهند.
شما میتوانید مستقیماً از APIهای جدیدتر در این کلاسهای بتن استفاده کنید - و در دستگاههای قبلی خراب نشوند - به دلیل بارگذاری کلاس تنبل. کلاس ها در اولین دسترسی بارگیری و مقداردهی اولیه می شوند - نمونه سازی کلاس یا دسترسی به یکی از فیلدها یا متدهای ثابت آن برای اولین بار. بنابراین، تا زمانی که پیادهسازیهای خاص Honeycomb را در دستگاههای قبل از Honeycomb نمونهسازی نکنید، Dalvik VM هیچ گونه استثنای VerifyError
ایجاد نخواهد کرد.
یک قرارداد نامگذاری خوب برای این پیادهسازی، اضافه کردن سطح API یا نام کد نسخه پلتفرم مربوط به APIهای مورد نیاز کلاسهای بتن است. برای مثال، پیادهسازی برگه بومی را میتوان توسط کلاسهای CompatTabHoneycomb
و TabHelperHoneycomb
ارائه کرد، زیرا آنها به APIهای موجود در Android نسخه 3.0 (سطح API 11) یا جدیدتر متکی هستند.

شکل 1. نمودار کلاس برای اجرای زبانه های لانه زنبوری.
CompatTabHoneycomb را پیاده سازی کنید
CompatTabHoneycomb
پیاده سازی کلاس انتزاعی CompatTab
است که TabHelperHoneycomb
از آن برای ارجاع به تب های جداگانه استفاده می کند. CompatTabHoneycomb
به سادگی تمام فراخوانی های متد را به شی ActionBar.Tab
موجود در خود پراکسی می کند.
پیاده سازی CompatTabHoneycomb
را با استفاده از APIهای جدید ActionBar.Tab
آغاز کنید:
کاتلین
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.)
}
جاوا
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
پیاده سازی کلاس انتزاعی TabHelper
است که متد پراکسی را به یک ActionBar
واقعی فراخوانی می کند که از Activity
موجود در آن به دست می آید.
پیاده سازی TabHelperHoneycomb
، فراخوانی های متد پراکسی به ActionBar
API:
کاتلین
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)
}
}
جاوا
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.
}
شما هم باید مطالعه کنید
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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)"]]