สร้างส่วนย่อย

ส่วนย่อยแสดงส่วนที่เป็นโมดูลของผู้ใช้ ภายในกิจกรรมได้ แฟรกเมนต์มีวงจรของตัวเอง และจะได้รับ ของเหตุการณ์อินพุตเอง และคุณจะเพิ่มหรือนำส่วนย่อยออก กิจกรรมกำลังทำงาน

เอกสารนี้จะอธิบายวิธีสร้างส่วนย่อยและรวมไว้ในกิจกรรม

ตั้งค่าสภาพแวดล้อมของคุณ

ส่วนย่อยต้องการการอ้างอิง ไลบรารี Fragment ของ AndroidX สิ่งที่คุณต้องทำ เพิ่มที่เก็บของ Google Maven ลงในไฟล์ settings.gradle ของโปรเจ็กต์เพื่อรวมทรัพยากร Dependency นี้

ดึงดูด

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        ...
    }
}

Kotlin

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        ...
    }
}

หากต้องการรวมไลบรารี AndroidX Fragment ไว้ในโปรเจ็กต์ของคุณ ให้เพิ่มรายการต่อไปนี้ ทรัพยากร Dependency ในไฟล์ build.gradle ของแอป

ดึงดูด

dependencies {
    def fragment_version = "1.8.2"

    // Java language implementation
    implementation "androidx.fragment:fragment:$fragment_version"
    // Kotlin
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
}

Kotlin

dependencies {
    val fragment_version = "1.8.2"

    // Java language implementation
    implementation("androidx.fragment:fragment:$fragment_version")
    // Kotlin
    implementation("androidx.fragment:fragment-ktx:$fragment_version")
}

สร้างคลาสส่วนย่อย

หากต้องการสร้างส่วนย่อย ให้ขยาย AndroidX Fragment ของชั้นเรียน และลบล้าง ในการแทรกตรรกะแอปของคุณ ซึ่งคล้ายกับวิธีที่คุณจะสร้าง ชั้นเรียน Activity เพื่อสร้าง แฟรกเมนต์ที่กำหนดเลย์เอาต์ของตัวเอง ระบุทรัพยากรเลย์เอาต์ของส่วนย่อย ลงในตัวสร้างฐานดังที่ปรากฏในตัวอย่างต่อไปนี้:

Kotlin

class ExampleFragment : Fragment(R.layout.example_fragment)

Java

class ExampleFragment extends Fragment {
    public ExampleFragment() {
        super(R.layout.example_fragment);
    }
}

นอกจากนี้ ไลบรารี Fragment ยังมีคลาสฐาน Fragment เฉพาะทางอื่นๆ อีกด้วย ดังนี้

DialogFragment
แสดงกล่องโต้ตอบแบบลอย การใช้ชั้นเรียนนี้สร้างการโต้ตอบ นอกเหนือจากการใช้เมธอดตัวช่วยกล่องโต้ตอบใน Activity เป็นส่วนย่อย จัดการการสร้างและการล้าง Dialog โดยอัตโนมัติ ดูการแสดงกล่องโต้ตอบที่มี DialogFragment เพื่อดูรายละเอียดเพิ่มเติม
PreferenceFragmentCompat
แสดงลำดับชั้นของ Preference ออบเจ็กต์ในฐานะ รายการ คุณสามารถใช้ PreferenceFragmentCompat เพื่อ สร้างหน้าจอการตั้งค่าสำหรับแอป

เพิ่มส่วนย่อยลงในกิจกรรม

โดยทั่วไปแล้ว ส่วนย่อยของคุณจะต้องฝังอยู่ใน AndroidX FragmentActivity ถึง ร่วมให้ส่วนหนึ่งของ UI ในเลย์เอาต์ของกิจกรรมนั้น FragmentActivity เป็นคลาสพื้นฐานสำหรับ AppCompatActivity, ดังนั้นถ้าคุณจัดคลาสย่อย AppCompatActivity ไว้แล้วเพื่อระบุแบบย้อนหลัง ความเข้ากันได้ในแอปของคุณ คุณจึงไม่ต้องเปลี่ยนกิจกรรม คลาสพื้นฐาน

คุณสามารถเพิ่มส่วนย่อยในลำดับชั้นการแสดงผลของกิจกรรมได้โดยทำดังนี้ กำหนดส่วนย่อยในไฟล์เลย์เอาต์ของกิจกรรมหรือโดยการกำหนด คอนเทนเนอร์ Fragment ในไฟล์เลย์เอาต์ของกิจกรรมแล้ว การเพิ่มส่วนย่อยจากภายในกิจกรรมของคุณแบบเป็นโปรแกรม ไม่ว่าจะเป็นกรณีใดก็ตาม คุณต้อง เพื่อเพิ่ม FragmentContainerView ซึ่งกำหนดตำแหน่งที่ควรวางส่วนย่อยภายใน ลำดับชั้นการดูของกิจกรรม ขอแนะนำให้ใช้ FragmentContainerView เป็นคอนเทนเนอร์สำหรับส่วนย่อย เช่น FragmentContainerView มีการแก้ไขเฉพาะสำหรับส่วนย่อยที่ ดูกลุ่มต่างๆ เช่น FrameLayout ไม่ระบุ

เพิ่มส่วนย่อยผ่าน XML

หากต้องการเพิ่มส่วนย่อยใน XML ของเลย์เอาต์กิจกรรมอย่างชัดเจน ให้ใช้ องค์ประกอบ FragmentContainerView

ต่อไปนี้เป็นตัวอย่างการจัดวางกิจกรรมที่มี FragmentContainerView:

<!-- res/layout/example_activity.xml -->
<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.ExampleFragment" />

แอตทริบิวต์ android:name ระบุชื่อคลาสของ Fragment เพื่อ สร้างอินสแตนซ์ เมื่อเลย์เอาต์ของกิจกรรมพองตัว ส่วนย่อยที่ระบุ มีการสร้างอินสแตนซ์ onInflate() ถูกเรียกในแฟรกเมนต์ที่สร้างขึ้นใหม่ และ FragmentTransaction สร้างขึ้นเพื่อเพิ่มส่วนย่อยไปยัง FragmentManager

เพิ่มส่วนย่อยแบบเป็นโปรแกรม

การเพิ่มส่วนย่อยแบบเป็นโปรแกรมลงในเลย์เอาต์ของกิจกรรมนั้น การจัดวาง ควรรวม FragmentContainerView ไว้ทำหน้าที่เป็นคอนเทนเนอร์ Fragment ดังที่ปรากฏในตัวอย่างต่อไปนี้

<!-- res/layout/example_activity.xml -->
<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

ซึ่งแตกต่างจากแนวทาง XML ตรงที่ไม่ได้ใช้แอตทริบิวต์ android:name ใน FragmentContainerViewที่นี่ เพื่อให้ไม่มีส่วนย่อยที่เจาะจงโดยอัตโนมัติ สร้างขึ้นมาแล้ว แต่ FragmentTransaction ใช้เพื่อสร้างอินสแตนซ์และเพิ่มลงในเลย์เอาต์ของกิจกรรม

ขณะที่กิจกรรมดำเนินอยู่ คุณสามารถทำธุรกรรมแบบ Fragment เช่น การเพิ่ม การนำออก หรือการแทนที่ส่วนย่อย ใน FragmentActivity คุณสามารถทำสิ่งต่อไปนี้ รับอินสแตนซ์ของ FragmentManager ซึ่ง เพื่อสร้าง FragmentTransaction ได้ จากนั้น คุณสามารถสร้างอินสแตนซ์ ภายในเมธอด onCreate() ของกิจกรรมโดยใช้ FragmentTransaction.add(), การส่งผ่านในรหัส ViewGroup ของคอนเทนเนอร์ในเลย์เอาต์และส่วนย่อย ที่คุณต้องการเพิ่ม แล้วทำธุรกรรม ดังที่แสดงใน ตัวอย่างต่อไปนี้

Kotlin

class ExampleActivity : AppCompatActivity(R.layout.example_activity) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState == null) {
            supportFragmentManager.commit {
                setReorderingAllowed(true)
                add<ExampleFragment>(R.id.fragment_container_view)
            }
        }
    }
}

Java

public class ExampleActivity extends AppCompatActivity {
    public ExampleActivity() {
        super(R.layout.example_activity);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.fragment_container_view, ExampleFragment.class, null)
                .commit();
        }
    }
}

ในตัวอย่างก่อนหน้านี้ โปรดทราบว่าระบบจะสร้างธุรกรรมส่วนย่อยเท่านั้น เมื่อsavedInstanceStateคือnull ทั้งนี้เพื่อให้มั่นใจว่าส่วนย่อย จะเพิ่มเพียงครั้งเดียวเมื่อสร้างกิจกรรมเป็นครั้งแรก เมื่อ การเปลี่ยนแปลงการกำหนดค่าเกิดขึ้น และกิจกรรมจะสร้างขึ้นใหม่ savedInstanceState ไม่ใช่ null แล้ว และส่วนย่อยไม่จำเป็นต้องใช้ เพิ่มเป็นครั้งที่ 2 เนื่องจากได้รับการกู้คืนส่วนย่อยโดยอัตโนมัติ จาก savedInstanceState

หากส่วนย่อยของคุณต้องใช้ข้อมูลเบื้องต้นบางอย่าง คุณสามารถส่งอาร์กิวเมนต์ไปที่ส่วนย่อยได้โดยการใส่ Bundle ในการเรียกไปยัง FragmentTransaction.add() ตามที่แสดงด้านล่าง

Kotlin

class ExampleActivity : AppCompatActivity(R.layout.example_activity) {
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState == null) {
            val bundle = bundleOf("some_int" to 0)
            supportFragmentManager.commit {
                setReorderingAllowed(true)
                add<ExampleFragment>(R.id.fragment_container_view, args = bundle)
            }
        }
    }
}

Java

public class ExampleActivity extends AppCompatActivity {
    public ExampleActivity() {
        super(R.layout.example_activity);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            Bundle bundle = new Bundle();
            bundle.putInt("some_int", 0);

            getSupportFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.fragment_container_view, ExampleFragment.class, bundle)
                .commit();
        }
    }
}

คุณจะดึงอาร์กิวเมนต์ Bundle จากภายในส่วนย่อยได้โดย การโทร requireArguments(), และสามารถใช้เมธอด Getter Bundle ที่เหมาะสมเพื่อเรียกข้อมูล อาร์กิวเมนต์แต่ละรายการ

Kotlin

class ExampleFragment : Fragment(R.layout.example_fragment) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val someInt = requireArguments().getInt("some_int")
        ...
    }
}

Java

class ExampleFragment extends Fragment {
    public ExampleFragment() {
        super(R.layout.example_fragment);
    }

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        int someInt = requireArguments().getInt("some_int");
        ...
    }
}

ดูเพิ่มเติม

ธุรกรรมใน Fragment และ FragmentManager ได้รับการอธิบายอย่างละเอียดมากขึ้น ในคู่มือตัวจัดการส่วนย่อย