viewpager2中的Fragmentstateadapter未在导航体系结构组件

时间:2019-11-18 11:41:52

标签: android xml android-spinner android-chips

我使用导航体系结构组件-一个活动多片段样式。在ProfileEditHolderFragment中,我想使用最新的ViewPager2和FragmentStateAdapter以可交换的方式显示Fragment列表。我像波纹管一样实现了它。

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支架中没有显示片段。

1 个答案:

答案 0 :(得分:0)

尝试使用childFragmentManager代替活动