【问题标题】:Fragmentstateadapter in viewpager2 not showing Fragment in Navigation Architecture Componentviewpager2 中的 Fragmentstateadapter 未显示导航架构组件中的片段
【发布时间】:2019-11-18 11:41:52
【问题描述】:

我使用导航架构组件 - 一个活动多片段样式。在 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 持有者中没有显示片段。

【问题讨论】:

  • 我认为您必须添加您尝试显示的导航场景的屏幕截图。(1)在第一个xml布局中,尝试将宽度/高度从'match_parent'更改为'0dp'( 2) ...youtube.com/watch?v=KwihiADN-0k

标签: android xml android-spinner android-viewpager2 android-chips


【解决方案1】:

尝试使用 childFragmentManager 代替活动

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 2020-01-16
    • 2021-01-12
    • 2019-12-07
    • 2020-11-08
    • 2020-11-01
    • 2019-08-03
    • 1970-01-01
    相关资源
    最近更新 更多