تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
تمثل علامة Fragment جزءًا قابلاً
لإعادة الاستخدام من واجهة مستخدم تطبيقك. يحدد الجزء تخطيطه الخاص ويديره،
ويكون له دورة حياة خاصة به ويمكنه التعامل مع أحداث الإدخال الخاصة به. لا يمكن أن تعيش الأجزاء
من تلقاء نفسها. ويجب أن تكون مستضافة بواسطة نشاط أو جزء آخر. يصبح العرض الهرمي لعرض الجزء جزءًا من التدرّج الهرمي لطريقة العرض للمضيف أو يتم إرفاقه به.
الوحدة النمطية
تقدم الأجزاء الوحدة النمطية وإمكانية إعادة الاستخدام في واجهة مستخدم نشاطك من خلال السماح لك بتقسيم واجهة المستخدم إلى أجزاء منفصلة. تُعد الأنشطة مكانًا مثاليًا لإضافة عناصر عالمية حول واجهة مستخدم تطبيقك، مثل
درج التنقل. وعلى العكس من ذلك، تكون الأجزاء أكثر ملاءمة لتحديد
وإدارة واجهة المستخدم لشاشة واحدة أو جزء من الشاشة.
ضع في اعتبارك تطبيقًا يستجيب لأحجام الشاشات المختلفة. على الشاشات الأكبر حجمًا، قد ترغب في
أن يعرض التطبيق درج تنقل ثابتًا وقائمة بتخطيط
الشبكة. على الشاشات الأصغر، قد ترغب في أن يعرض التطبيق
شريط تنقل سفلي وقائمة بتنسيق خطي.
إن إدارة هذه الاختلافات في
النشاط أمر ليس عملية. يمكن أن يؤدي فصل عناصر التنقل عن المحتوى إلى
تسهيل إدارة هذه العملية. يكون النشاط بعد ذلك مسؤولاً عن عرض واجهة المستخدم الصحيحة للتنقل، بينما يعرض الجزء القائمة بالتخطيط المناسب.
الشكل 1. نسختان من الشاشة نفسها على
أحجام شاشات مختلفة. على يمين الشاشة، تحتوي شاشة كبيرة على لائحة تنقّل
يمكن التحكّم فيها من خلال النشاط وقائمة على شكل شبكة يمكن التحكّم فيها من خلال
الجزء. على يسار الشاشة، تحتوي شاشة صغيرة على شريط تنقّل سفلي يتحكّم فيه النشاط وقائمة خطية يتحكّم
فيها الجزء.
يؤدي تقسيم واجهة المستخدم إلى أجزاء إلى تسهيل تعديل ظهور نشاطك في وقت التشغيل. عندما يكون نشاطك في STARTEDحالة دورة حياة أو أعلى، يمكن إضافة أجزاء أو استبدالها أو إزالتها. ويمكنك الاحتفاظ بسجل لهذه التغييرات في المكدس الخلفي الذي يديره النشاط، بحيث يمكن عكس التغييرات.
يمكنك استخدام مثيلات متعددة لفئة الجزء نفسها ضمن النشاط نفسه أو في أنشطة متعددة أو حتى كعنصر فرعي لجزء آخر. مع وضع ذلك في الاعتبار، لا تقدم سوى جزء بالمنطق اللازم لإدارة واجهة المستخدم الخاصة بها. تجنب الاعتماد على جزء
من جزء آخر أو معالجته.
الخطوات التالية
لمزيد من المستندات والموارد المرتبطة بالأجزاء، راجِع ما يلي.
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Fragments\n\nA [`Fragment`](/reference/androidx/fragment/app/Fragment) represents a\nreusable portion of your app's UI. A fragment defines and manages its own\nlayout, has its own lifecycle, and can handle its own input events. Fragments\ncan't live on their own. They must be *hosted* by an activity or another\nfragment. The fragment's view hierarchy becomes part of, or *attaches to*,\nthe host's view hierarchy.\n| **Note:** Some [Android Jetpack](/jetpack/androidx/versions) libraries, such as [Navigation](/guide/navigation), [`BottomNavigationView`](/reference/com/google/android/material/bottomnavigation/BottomNavigationView), and [`ViewPager2`](/jetpack/androidx/releases/viewpager2), are designed to work with fragments.\n\nModularity\n----------\n\nFragments introduce modularity and reusability into your activity's UI by\nletting you divide the UI into discrete chunks. Activities are an ideal\nplace to put global elements around your app's user interface, such as a\nnavigation drawer. Conversely, fragments are better suited to define and\nmanage the UI of a single screen or portion of a screen.\n\nConsider an app that responds to various screen sizes. On larger screens, you\nmight want the app to display a static navigation drawer and a list in a grid\nlayout. On smaller screens, you might want the app to display a bottom\nnavigation bar and a list in a linear layout.\n\nManaging these variations in the activity is\nunwieldy. Separating the navigation elements from the content can make this\nprocess more manageable. The activity is then responsible for displaying\nthe correct navigation UI, while the fragment displays the list with the proper\nlayout.\n**Figure 1.** Two versions of the same screen on different screen sizes. On the left, a large screen contains a navigation drawer that is controlled by the activity and a grid list that is controlled by the fragment. On the right, a small screen contains a bottom navigation bar that is controlled by the activity and a linear list that is controlled by the fragment.\n\nDividing your UI into fragments makes it easier to modify your activity's\nappearance at runtime. While your activity is in the `STARTED`\n[lifecycle state](/guide/components/activities/activity-lifecycle) or\nhigher, fragments can be added, replaced, or removed. And you can keep a record\nof these changes in a back stack that is managed by the activity, so that\nthe changes can be reversed.\n\nYou can use multiple instances of the same fragment class within the\nsame activity, in multiple activities, or even as a child of another\nfragment. With this in mind, only provide a fragment with\nthe logic necessary to manage its own UI. Avoid depending on or\nmanipulating one fragment from another.\n\nNext steps\n----------\n\nFor more documentation and resources related to fragments, see the following.\n\n### Getting Started\n\n- [Create a fragment](/guide/fragments/create)\n\n### Further topics\n\n- [Fragment manager](/guide/fragments/fragmentmanager)\n- [Fragment transactions](/guide/fragments/transactions)\n- [Navigate between fragments using animations](/guide/fragments/animate)\n- [Fragment lifecycle](/guide/fragments/lifecycle)\n- [Saving state with fragments](/guide/fragments/saving-state)\n- [Communicate with fragments](/guide/fragments/communicate)\n- [Working with the AppBar](/guide/fragments/appbar)\n- [Display dialogs with DialogFragment](/guide/fragments/dialogs)\n- [Debug your fragments](/guide/fragments/debugging)\n- [Test your fragments](/guide/fragments/test)\n\n### Samples\n\n### Videos\n\n- [Single Activity: Why, when, and how (Android Dev Summit '18)](https://www.youtube.com/watch?v=2k8x8V77CrU)\n- [Fragments: Past, present, and future (Android Dev Summit '19)](https://www.youtube.com/watch?v=RS1IACnZLy4)"]]