简体   繁体   中英

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

In the below code my tab layout showed in the bottom sheet but in the view page fragments doesn't show. I used childManager and that also didn't work for me. I need answer for my question?

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
    }

}

JobDetalsActivity

val bottomSheetFragment = BottomSheetFragment()

    slideLayout.setOnClickListener() {

        bottomSheetFragment.show(supportFragmentManager, "BottomSheetDialog")

    }

ViewPageAdapter

   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>

Try to use a LinearLayout as parent view in your fragment_bottom_sheet_dialog layout

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM