为了充分合理地使用平板等大屏幕空间,Android 提供了Fragment
组件,它是一种可以嵌入到Activity
中使用的界面片段。如果说Activity
是剧院的大舞台,那么 Fragment
就相当于是这个大舞台的分会场,它的使用方式跟Activity
很相似。
代码演示,Fragment
的使用方法
- 创建一个名为
FragmentSample
的项目; - 依次右击项目包名,创建两个
Fragment
,分别名为FragmentA
、FragmentB
.
创建完成后的项目结构:
- 分别编辑
FragmentA
、FragmentB
对应的布局文件fragment_a.xml
、fragment_b.xml
.
fragment_a.xml
代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff1177"
tools:context=".FragmentA">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello A fragment !"
android:textColor="@color/white" />
</FrameLayout>
fragment_b.xml
代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#221177"
tools:context=".FragmentA">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello B fragment !"
android:textColor="@color/white" />
</FrameLayout>
这里设置了对应布局的背景颜色和文本,用来区分不同的Fragment
;实际的项目中可以根据具体的需求设置界面。
- 编辑项目主
Activity
的布局文件activity_main.xml
activity_main.xml
代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="Hello World!" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<FrameLayout
android:id="@+id/container_a"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/container_b"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
这里两个 <FrameLayout/>
标签用于承载Fragment
。
- 动态加载
Fragment
编辑主界面 MainActivity
,加载Fragment
。
Java 代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentA fragmentA = new FragmentA();
FragmentB fragmentB = new FragmentB();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container_a, fragmentA);
transaction.replace(R.id.container_b, fragmentB);
transaction.commit();
}
Kotlin 代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val fragmentA = FragmentA()
val fragmentB = FragmentB()
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.container_a, fragmentA)
transaction.replace(R.id.container_b, fragmentB)
transaction.commit()
}
这里重写了 Activity
的 onCreate()
函数;
首先创建了即将要加载的 Fragment
的实例,其次通过 getSupportFragmentManager()
函数获取 FragmentManager
对象,接着通过 beginTransaction()
函数开启一个事务,然后使用 add()
或 replace()
函数向容器中添加相应的Fragment
实例,最后调用 commit()
函数提交事务,操作完成。
- 运行项目
Fragment
与Activity
互相通信
在Activity
中通过Fragment
实例可以调用相应Fragment
的函数;
在Fragment
中可以调用 getActivity()
方法获取其宿主Activity
对象,从而实现在Fragment
中调用Activity
里的方法.
Java 代码:
if (null!= getActivity()) {
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.doSomething();
}
Kotlin 代码:
if (null!= activity) {
val mainActivity = activity as MainActivity
mainActivity.doSomething()
}
项目代码地址
Java 版:
https://github.com/BethelDEV/shaguaAndroid/tree/main/javaSource/FragmentSampleKotlin 版:
https://github.com/BethelDEV/shaguaAndroid/tree/main/kotlinSource/FragmentSample