簡體   English   中英

在選項卡布局中,查看分頁器不會顯示在 Kotlin 的底部工作表對話框片段中

[英]In Tab Layout, View Pager Doesn't Show In Bottom Sheet Dialog Fragment in Kotlin

在下面的代碼中,我的選項卡布局顯示在底部工作表中,但在視圖頁面片段中未顯示。 我使用了 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>

嘗試在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM