在 Android 控件演示 - Fragment (一), 演示了适用静态的方式将 Fragment 放置到 Activity 中。本文将简单演示如何动态的切换 Fragment。
继续使用 Android 控件演示 - Fragment (一) 中的示例,在 MainActivity 中添加一个按钮,当按钮按下时,将 fragment1 中的 Fragment 由 TodoFragment 切换新加的一个 ProductFragment。
添加按钮
添加后界面描述文件 activity_main.xml 为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<fragment android:id="@+id/fragment1" android:name="cn.com.hohistar.tutorial.fragmenttutorial.TodoFragment" android:layout_width="361dp" android:layout_height="176dp" android:layout_marginStart="27dp" android:layout_marginLeft="27dp" android:layout_marginTop="73dp" android:layout_marginEnd="27dp" android:layout_marginRight="27dp" android:layout_weight="1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/btnChange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:text="Change Fragment" app:layout_constraintEnd_toEndOf="@+id/fragment1" app:layout_constraintStart_toStartOf="@+id/fragment1" app:layout_constraintTop_toBottomOf="@+id/fragment1" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
新建 Fragment
选择新建一个 Fragment, 并命名为: ProductFragment。然后在 ProductFragment 中加入一个简单的文本显示即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ProductFragment">
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginStart="160dp" android:layout_marginLeft="160dp" android:layout_marginTop="50dp" android:layout_marginEnd="160dp" android:layout_marginRight="160dp" android:text="Product Detail" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
|
ProductFragment 对应的java代码不做任何改动,保持不变。
实现 Fragment 动态切换
然后在 MainActivity.java 中加入按钮事件处理代码:
1 2 3 4 5 6 7 8 9
| @OnClick(R.id.btnChange) void onChangeFragment() {
FragmentManager fm = getSupportFragmentManager();
Fragment productFragment = new ProductFragment(); fm.beginTransaction() .add(R.id.fragment1, productFragment) .commit(); }
|
以上代码中,使用了 FragmentManager,FragmentManager 是 Android 中用来管理 Fragment 的一个类,可以通过 Activity 中的 getSupportFragmentManager() 方法获得FragmentManager,FragmentManager 中包含了 add(), remove(), replace() 等方法对 Fragment 进行管理。其典型的调用格式为:
1 2 3 4 5 6
| beginTransaction() 操作1 操作2 ... 操作n commit()
|
该完以后运行程序,就可以看到通过按钮实现了 Fragment 的切换。