با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
گاهی اوقات چیدمان شما نیاز به نماهای پیچیده ای دارد که به ندرت از آنها استفاده می شود. خواه جزئیات مورد، نشانگرهای پیشرفت، یا لغو پیامها باشند، میتوانید با بارگیری نماها فقط در صورت نیاز، مصرف حافظه را کاهش دهید و سرعت رندر را افزایش دهید.
با تعریف ViewStub برای نماهای پیچیده و به ندرت استفاده میشود، زمانی که نماهای پیچیدهای دارید که برنامه شما در آینده به آنها نیاز دارد، بارگیری منابع را به تعویق بیندازید.
ViewStub را تعریف کنید
ViewStub یک نمای سبک وزن و بدون بعد است که چیزی را ترسیم نمی کند یا در طرح بندی شرکت نمی کند. به این ترتیب، به منابع کمی برای افزایش و رها کردن سلسله مراتب دیدگاه نیاز دارد. هر ViewStub دارای ویژگی android:layout برای تعیین طرح بندی برای باد کردن است.
فرض کنید یک طرح بندی دارید که می خواهید بعداً در سفر کاربر برنامه خود بارگیری کنید:
می توانید با استفاده از ViewStub زیر بارگذاری را به تعویق بیندازید. برای نشان دادن یا بارگیری هر چیزی، باید کاری کنید که طرح بندی ارجاع شده را نشان دهد:
قطعه کد در بخش قبل چیزی شبیه شکل 1 را ایجاد می کند:
شکل 1. وضعیت اولیه صفحه: ViewStub در حال پنهان کردن طرح سنگین است.
وقتی میخواهید طرحبندی مشخصشده توسط ViewStub را بارگیری کنید، آن را با فراخوانی setVisibility(View.VISIBLE) روی قابل مشاهده تنظیم کنید یا inflate() را فراخوانی کنید.
قطعه کد زیر یک بار به تعویق افتاده را شبیه سازی می کند. صفحه طبق معمول در Activity و onCreate() بارگیری می شود، سپس طرح heavy_layout_we_want_to_postpone را نشان می دهد:
کاتلین
overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_old_xml)Handler(Looper.getMainLooper()).postDelayed({findViewById<View>(R.id.stub_import).visibility=View.VISIBLE// Or val importPanel: View = findViewById<ViewStub>(R.id.stub_import).inflate()},2000)}
جاوا
@OverridevoidonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState);setContentView(R.layout.activity_old_xml);Handler(Looper.getMainLooper()).postDelayed({findViewById<View>(R.id.stub_import).visibility=View.VISIBLE// Or val importPanel: View = findViewById<ViewStub>(R.id.stub_import).inflate()},2000);}
شکل 2. چیدمان سنگین قابل مشاهده است.
وقتی عنصر ViewStub قابل مشاهده یا باد شد، دیگر بخشی از سلسله مراتب view نیست. با طرح باد شده جایگزین میشود و شناسه نمای ریشه آن طرحبندی با ویژگی android:inflatedIdViewStub مشخص میشود. شناسه android:id مشخص شده برای ViewStub فقط تا زمانی معتبر است که طرح ViewStub قابل مشاهده یا باد شده باشد.
برای اطلاعات بیشتر در مورد این موضوع، به پست وبلاگ بهینه سازی با خرد مراجعه کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و 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,["# Load views on demand\n\nSometimes your layout requires complex views that are rarely used. Whether\nthey are item details, progress indicators, or undo messages, you can reduce\nmemory usage and speed up rendering by loading the views only when they're\nneeded.\n\nYou can defer loading resources when you have complex views that your app\nneeds in the future by defining a\n[ViewStub](/reference/android/view/ViewStub) for\ncomplex and rarely used views.\n\nDefine a ViewStub\n-----------------\n\n`ViewStub` is a lightweight view with no dimension that doesn't\ndraw anything or participate in the layout. As such, it requires few resources\nto inflate and leave in a view hierarchy. Each `ViewStub` includes\nthe `android:layout` attribute to specify the layout to inflate.\n\nSuppose you have a layout you want to load later in the user journey of your\napp: \n\n```transact-sql\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cFrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cImageView\n android:src=\"@drawable/logo\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"/\u003e\n\u003c/FrameLayout\u003e\n```\n\nYou can postpone loading using the following `ViewStub`. To make\nit show or load anything, you must make it show the referred layout: \n\n```xml\n\u003cFrameLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\nandroid:id=\"@+id/root\"\nandroid:layout_width=\"match_parent\"\nandroid:layout_height=\"match_parent\"\u003e\n\n\u003cViewStub\n android:id=\"@+id/stub_import\"\n android:inflatedId=\"@+id/panel_import\"\n android:layout=\"@layout/heavy_layout_we_want_to_postpone\"\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:layout_gravity=\"bottom\" /\u003e\n\u003c/FrameLayout\u003e\n```\n\nLoad the ViewStub layout\n------------------------\n\nThe code snippets in the previous section produce something like figure\n1:\n**Figure 1.** Initial state of the screen: the `ViewStub` is hiding the heavy layout.\n\nWhen you want to load the layout specified by the `ViewStub`,\neither set it to visible by calling\n[setVisibility(View.VISIBLE)](/reference/android/view/View#setVisibility(int))\nor call\n[inflate()](/reference/android/view/ViewStub#inflate()).\n\nThe following code snippet simulates a postponed load. The screen loads as\nusual in the `Activity` and `onCreate()`, then it shows\nthe `heavy_layout_we_want_to_postpone` layout: \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_old_xml)\n\n Handler(Looper.getMainLooper())\n .postDelayed({\n findViewById\u003cView\u003e(R.id.stub_import).visibility = View.VISIBLE\n \n // Or val importPanel: View = findViewById\u003cViewStub\u003e(R.id.stub_import).inflate()\n }, 2000)\n}\n```\n\n### Java\n\n```java\n@Override\nvoid onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_old_xml);\n\n Handler(Looper.getMainLooper())\n .postDelayed({\n findViewById\u003cView\u003e(R.id.stub_import).visibility = View.VISIBLE\n \n // Or val importPanel: View = findViewById\u003cViewStub\u003e(R.id.stub_import).inflate()\n }, 2000);\n}\n```\n**Figure 2.** The heavy layout is visible. **Note:** The `inflate()` method returns the inflated `View` after it's complete, so you don't need to call [findViewById()](/reference/android/app/Activity#findViewById(int)) if you need to interact with the layout.\n\nOnce visible or inflated, the `ViewStub` element is no longer part\nof the view hierarchy. It is replaced by the inflated layout, and the ID for the\nroot view of that layout is specified by the `android:inflatedId`\nattribute of the `ViewStub`. The ID `android:id` specified\nfor the `ViewStub` is valid only until the `ViewStub`\nlayout is visible or inflated.\n| **Note:** A drawback of `ViewStub` is that it doesn't support the `\u003cmerge\u003e` tag in the layouts to be inflated.\n\nFor more information about this topic, see the blog post\n[Optimize\nwith stubs](http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-with.html)."]]