A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
You should design each fragment as a modular and reusable activity component.
public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); }}
For example, here's the layout file for an activity with two fragments:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout>
programmatically add the fragment to an existing ViewGroup.
At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to place the fragment.
To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction. You can get an instance of FragmentTransaction from your Activity like this:
FragmentManager fragmentManager = getFragmentManager()FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:
ExampleFragment fragment = new ExampleFragment();fragmentTransaction.add(R.id.fragment_container, fragment);fragmentTransaction.commit();The first argument passed to add() is the ViewGroup in which the fragment should be placed, specified by resource ID, and the second parameter is the fragment to add.
Once you've made your changes with FragmentTransaction, you must call commit() for the changes to take effect.
Communicating with the Activity:
Although a Fragment is implemented as an object that's independent from an Activity and can be used inside multiple activities, a given instance of a fragment is directly tied to the activity that contains it.
Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout:
View listView = getActivity().findViewById(R.id.list);Likewise, your activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag(). For example:
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
分享到:
相关推荐
### Android Fragments:掌握核心概念与实践技巧 #### 引言 随着移动应用开发成为热点话题,越来越多的开发者加入到这一领域。对于Android开发者而言,理解并熟练掌握**Fragments**这一关键概念至关重要。...
[Packt Publishing] 动态 UI 开发教程 Android Fragments 实现 英文版 [Packt Publishing] Creating Dynamic UI with Android Fragments E Book ☆ 图书概要:☆ Leverage the power of Android fragments to ...
Android Fragments 是Android开发中的一个重要概念,自Android 3.0(API级别11)引入,主要用于解决不同屏幕尺寸设备上的UI适配问题。Fragments 提供了一种灵活的方式来构建动态和可重构的用户界面,特别是在平板...
在Android应用开发中,Fragments是重要的组件,用于构建可重用、动态的用户界面,尤其是在平板电脑和大屏幕设备上。本示例"Android Fragments 使用的一些建议demo"提供了一些最佳实践,帮助开发者更高效地使用...
《使用Android Fragments创建动态UI》第二版是一本专注于Android平台的开发书籍,详细介绍了如何利用Android的Fragment组件构建出适应不同设备特性的动态用户界面。书籍内容不仅涵盖了Fragment的基本概念和使用方法...
基于Android框架的动态UI构建(Creating Dynamic UI with Android Fragments, 2nd Edition)-2016英文原版,0积分——该书是2016年最新的第2版,全书154页。
This book looks at the impact fragments have on Android UI design and their role in both simplifying many common UI challenges and in providing best practices for incorporating rich UI behaviors....
Trying to meet these demands using Android's traditional activity-centric UI design model is difficult. As developers, we need more control than that afforded by activities. We need a new approach: ...
Trying to meet these demands using Android's traditional activity-centric UI design model is dif cult at best. As developers, we need more control than that afforded by activities. We need a new ...
在Android开发中,Fragment是一种可以被嵌入到Activity中的模块化组件,它有自己的生命周期,并且可以接收自己的输入事件。在运行时,开发者可以添加或移除Fragment,就像使用子Activity一样,可以在不同的Activity...
一个实用程序库,用于使用Android Fragments促进React Native开发。 入门 $ npm install react-native-android-fragment --save 或者 $ yarn add react-native-android-fragment 大多是自动安装 $ react-native ...
"Fragments_Details:详细信息Android Fragments的东西"这个主题深入探讨了Android Fragments的相关知识,结合Slidenerd的教程,我们将对这个核心概念进行详尽的解析。 1. **Fragment基础** - Fragment是Android ...
Android Fragments and Activities的实例保护程序。 您再也不必编写一百万个公共静态最终字符串了! 背包是一个注释处理器,通过简单注释通常可以在onSaveInstanceState()期间保存到bundle中并在onCreate()中...
钛碎片模块 此模块为 Appcelerator Titanium 应用程序添加了 Android Fragments 支持 贡献者 Nimrod Kobelkowsky (@nborracha) 合法的
36. An Introduction to Android Fragments 37. Using Fragmentsin Android Studio-An Example 39. An Android letpack viewModel Tutorial 40. An Android Jetpack LiveData Tutorial 41. An Overview of Android ...
《Android Fragments 深入理解与应用》 在Android开发中,Fragments是一个至关重要的组件,自Android 3.0引入以来,它为开发者提供了更灵活的UI设计能力,以适应不同尺寸屏幕的需求。Fragments的设计初衷是解决在...