Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...