0

在下面的代码中,我的选项卡布局显示在底部工作表中,但在视图页面片段中未显示。我使用了 childManager,但这对我也不起作用。我需要回答我的问题吗?

BottomSheetFragment

class BottomSheetFragment : BottomSheetDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?

    ): View? {

        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_bottom_sheet_dialog, container, false)

        val viewPager: ViewPager = view.findViewById(R.id.viewPager)
        val tabLayout: TabLayout = view.findViewById(R.id.tabLayout112)

        val adapter = ViewPageAdapter(childFragmentManager)
        adapter.addFragment(JobInfoFragment(), "Job Info")
        adapter.addFragment(ClientInfoFragment(), "Client Info")
        adapter.addFragment(GuarantorInfoFragment(), "Guarantor Info")
        viewPager.adapter = adapter
        tabLayout.setupWithViewPager(viewPager)


        return view
    }

}

工作详情活动

val bottomSheetFragment = BottomSheetFragment()

    slideLayout.setOnClickListener() {

        bottomSheetFragment.show(supportFragmentManager, "BottomSheetDialog")

    }

视图页面适配器

   class ViewPageAdapter(supportFragmentManager: FragmentManager) : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private val mFragmentList = ArrayList<Fragment>()
    private val mFragmentListTitle = ArrayList<String>()

    override fun getCount(): Int {
        return mFragmentList.size

    }

    override fun getItem(position: Int): Fragment {

        return mFragmentList[position]
    }

    override fun getPageTitle(position: Int): CharSequence? {

        return mFragmentListTitle[position]
    }

    fun addFragment(fragment: Fragment, title: String){

        mFragmentList.add(fragment)
        mFragmentListTitle.add(title)
    }
}

fragment_bottom_sheet_dialog

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragments.BottomSheetFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout112"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout112" />
</androidx.constraintlayout.widget.ConstraintLayout>
4

1 回答 1

0

尝试在fragment_bottom_sheet_dialog布局中使用LinearLayout作为父视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragments.BottomSheetFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout112"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
于 2021-01-07T16:57:29.320 回答