1

我使用导航架构组件 - 一个活动多片段样式。在 ProfileEditHolderFragment 中,我想使用最新的 ViewPager2 和 FragmentStateAdapter 以可交换的方式显示片段列表。我像下面这样实现它。

ProfileEditHolderFragment.kt

class ProfileEditHolderFragment : BaseFragment() {

    override fun getLayoutResId() = R.layout.fragment_profile_edit_holder

    override fun initWidget() {
        profile_state_progress_bar.setStateDescriptionData(arrayOf("Tuition", "Education", "Personal", "Credential", "Quiz"))

        val listOfFragment = listOf<Fragment>(TuitionInfoFragment(), EditEducationFragment())
        profile_edit_view_pager.adapter = activity?.let {
            TutorProfileEditAdapter(it, listOfFragment)
        }
    }

}

fragment_profile_edit_holder.xml

<androidx.core.widget.NestedScrollView
    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=".ui.fragment.tutorPanel.profileEdit.ProfileEditHolderFragment">

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

        <another view>

        <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/profile_edit_view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="@dimen/_20sdp"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/profile_state_progress_bar"
                app:layout_constraintBottom_toBottomOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

TutorProfileEditAdapter.kt

class TutorProfileEditAdapter(
    activity: FragmentActivity,
    private val fragmentList: List<Fragment>
): FragmentStateAdapter(activity) {

    override fun getItemCount() = fragmentList.size

    override fun createFragment(position: Int) = fragmentList[position]

}

但问题是 ViewPager2 持有者中没有显示任何片段。

4

1 回答 1

1

尝试使用 childFragmentManager 而不是活动

于 2019-11-18T13:26:34.513 回答