Android

【Android】Fragmnet 片段 範例

【Android】Fragmnet 片段 範例

Android Fragment 是一個可以被放置在 Android Activity 中的 UI 部件。它可以被用來組成多個頁面的應用程式,並且允許在不同的螢幕尺寸或設備上重用 UI。Fragment 可以被加入或移除,並且可以在不同的 Activity 之間共用。

它可以搭配 Android 的 FragmentManager 和 FragmentTransaction 類別來管理和操作。


文章目錄

  1. Fragment 創建
  2. Fragment 參數傳遞
  3. Fragment 添加依賴
  4. Developer Documents Fragment

1.Fragment 創建

fragment_first.xml
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
FirstFragment.kt
class FirstFragment : Fragment(R.layout.fragment_first) {

}
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity(R.layout.activity_main) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) {
            supportFragmentManager.commit {
                setReorderingAllowed(true)
                add<FirstFragment>(R.id.fragment_container_view)
            }
        }
    }

}

2.Fragment 參數傳遞

MainActivity.kt
class MainActivity : AppCompatActivity(R.layout.activity_main) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) {
            val bundle = bundleOf("number" to 5)
            supportFragmentManager.commit {
                setReorderingAllowed(true)
                add<FirstFragment>(R.id.fragment_container_view, args = bundle)
            }
        }
    }

}
FirstFragment.kt
class FirstFragment : Fragment(R.layout.fragment_first) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val number = requireArguments().getInt("number")
        Log.e("FirstFragment", number.toString())
    }

}

3.Fragment 添加依賴

PersonData.kt
data class PersonData(val name: String, val age: Int)
FirstFragment.kt
class FirstFragment(private val personData: PersonData) : Fragment(R.layout.fragment_first) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        Log.e("FirstFragment", personData.toString())
    }

}
FirstFragmentFactory.kt
class FirstFragmentFactory(private val personData: PersonData) : FragmentFactory() {

    override fun instantiate(classLoader: ClassLoader, className: String): Fragment =
        when (loadFragmentClass(classLoader, className)) {
            FirstFragment::class.java -> FirstFragment(personData)
            else -> super.instantiate(classLoader, className)
        }

}
MainActivity.kt
class MainActivity : AppCompatActivity(R.layout.activity_main) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (savedInstanceState == null) {
            supportFragmentManager.fragmentFactory = FirstFragmentFactory(PersonData("wade", 26))

            supportFragmentManager.commit {
                add<FirstFragment>(R.id.fragment_container_view)
                setReorderingAllowed(true)
            }
        }
    }

}

4.Developer Documents Fragment

Open in Documents Fragment

發表迴響