Android 中的巢狀碎片
簡介
在構建 Android 應用程式時,我們經常會遇到需要在一個碎片中顯示另一個碎片的情況。例如,我們在應用程式中使用選項卡布局和碎片,並且希望在按鈕點選時開啟另一個碎片,則必須在當前碎片中呼叫另一個碎片。本文將介紹如何在 Android 中在一個碎片中呼叫另一個碎片。
實現
我們將建立一個簡單的應用程式,在這個應用程式中,我們將在父活動中顯示一個 TextView。在這個父活動中,我們將呼叫我們的父碎片並在其中顯示一條文字訊息。然後,我們將在父碎片中呼叫我們的子碎片,並在該碎片中也顯示一條文字訊息。
步驟 1:在 Android Studio 中建立新專案
導航到 Android Studio,如下面的螢幕所示。在下面的螢幕中,單擊“新建專案”以建立一個新的 Android Studio 專案。
單擊“新建專案”後,您將看到下面的螢幕。
在此螢幕中,我們只需選擇“空活動”並單擊“下一步”。單擊“下一步”後,您將看到下面的螢幕。
在此螢幕中,我們只需指定專案名稱。然後包名稱將自動生成。
注意 - 確保選擇 Java 作為語言。
指定完所有詳細資訊後,單擊“完成”以建立一個新的 Android Studio 專案。
專案建立完成後,我們將看到兩個開啟的檔案,即 activity_main.xml 和 MainActivity.java 檔案。
步驟 3:使用 activity_main.xml
導航到 activity_main.xml。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>res>layout>activity_main.xml 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋以便詳細瞭解。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/idCLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- creating a text view for displaying the text for parent activity-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Parent Activity"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- creating a frame layout for displaying a parent fragment -->
<FrameLayout
android:id="@+id/parentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/idTVHeading" />
</RelativeLayout>
說明 - 在上面的程式碼中,我們建立了一個 RelativeLayout 作為根佈局,並在其中建立了一個 TextView 用於顯示當前活動的標題“父活動”,然後我們建立了一個 FrameLayout,我們將在其中顯示我們的父碎片(其中包含一個 TextView)和我們的子碎片。
步驟 4:建立兩個新的碎片
現在我們將建立兩個碎片,一個是我們的父碎片,另一個是我們的子碎片。要建立一個新的碎片,請導航到 app>java>您的應用程式包名稱>右鍵單擊它>新建>碎片並選擇“空碎片”,並將其命名為 ParentFragment。類似地,建立另一個名為 ChildFragment 的碎片。這兩個碎片的 xml 和 java 檔案將被建立。
步驟 5:使用 fragment_parent.xml 檔案
導航到 fragment_one.xml。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>res>layout>fragment_parent.xml 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋以便詳細瞭解。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".ParentFragment">
<!-- creating a text view for displaying the text for parent activity-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Parent fragment"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- creating a frame layout for displaying a child fragment -->
<FrameLayout
android:id="@+id/childFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/idTVHeading">
</FrameLayout>
</RelativeLayout>
說明 - 在上面的程式碼中,我們建立了一個 RelativeLayout 作為根佈局。在此 RelativeLayout 中,我們建立了一個 TextView 用於顯示當前碎片的標題“父碎片”,然後我們為子碎片建立了一個 FrameLayout 用於顯示我們的子碎片。
步驟 6:使用 fragment_child.xml 檔案
導航到 fragment_one.xml。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>res>layout>fragment_child.xml 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋以便詳細瞭解。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".ChildFragment">
<!-- creating a text view for displaying a text-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="5dp"
android:text="Child fragment"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
說明:在上面的程式碼中,我們建立了一個 RelativeLayout 作為根佈局。在此 RelativeLayout 中,我們建立了一個 TextView 用於顯示當前碎片的標題“子碎片”。
步驟 7 - 使用 ParentFragment.java 檔案
導航到 ParentFragment.java。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>java>您的應用程式包名稱>ParentFragment.java 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋以便詳細瞭解。
package com.example.androidjavaapp;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ParentFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_parent, container, false);
// on below line creating a child fragment
Fragment childFragment = new ChildFragment();
// on below line creating a fragment transaction and initializing it.
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
// on below line replacing the fragment in child container with child fragment.
transaction.replace(R.id.childFragmentContainer, childFragment).commit();
return view;
}
}
說明 - 在上面的程式碼中,我們可以看到 onCreateView 方法,在該方法中,我們正在為 fragment_parent.xml 檔案膨脹佈局。在此 onCreateView 方法中,我們首先為子碎片建立變數,然後呼叫並初始化碎片事務。使用此碎片事務變數,我們正在容器中替換碎片。
步驟 8:使用 MainActivity.java 檔案。
導航到 MainActivity.java。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app>java>您的應用程式包名稱>MainActivity.java 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋以便詳細瞭解。
package com.example.androidjavaapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating and initializing variable for fragment transaction.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// replacing the parent container with parent fragment.
ft.replace(R.id.parentContainer, new ParentFragment());
// committing the transaction.
ft.commit();
}
}
說明 - 在上面的程式碼中,我們可以看到 onCreate 方法,在該方法中,我們正在為 activity_main.xml 膨脹佈局。在此 onCreate 方法中,我們使用 FragmentTransaction 載入 ParentFragment。然後,我們用 ParentFragment 替換父容器中的碎片。
新增上述程式碼後,我們只需單擊頂部的綠色圖示即可在移動裝置上執行我們的應用程式。
注意 - 確保您已連線到您的真實裝置或模擬器。
輸出
在上面的輸出螢幕截圖中,我們可以看到三個 TextView,第一個是我們的父活動,我們在其中載入了我們的父碎片。因此,我們可以看到父碎片的文字,然後我們可以看到另一個 TextView 作為子碎片,我們在其中載入了我們的子碎片。
結論
在本教程中,我們瞭解瞭如何在 Android 應用程式中獲取當前顯示的碎片例項。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP