Home > OS >  How to change page with button in viewPager?
How to change page with button in viewPager?

Time:11-07

I have a dialog fragment that contains a viewpager with four fragments. The navigation between these fragments works fine with swipe, but I want to change fragments, with a button on the fragment.

I found that I have to use setCurrentItem(int iten). But I don't know how to use in a different fragment, which contains the xml with the viewpager.

I have this:

class DialogFragmentContainer: DialogFragment() {

    lateinit var binding: DialogFragmentContainerBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DialogFragmentContainerBinding.inflate(layoutInflater)
        if(dialog != null && dialog!!.window != null){
            dialog!!.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val viewPagerAdapter = ViewPagerAdapter(this)
        val viewPager = binding.pager
        viewPager.adapter = viewPagerAdapter
    }
}

and the xml

<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:layout_height="wrap_content">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@ id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

The adapter

class ViewPagerAdapter(fragment: Fragment): FragmentStateAdapter(fragment) {
    override fun getItemCount(): Int {
        return 4
    }

    override fun createFragment(position: Int): Fragment {
        return when(position){
            0 -> Step1()
            1 -> Step2()
            2 -> Step3()
            3 -> Step4()
            else -> Step1()
        }
    }
}

And this is the fragment with the button, where I want to change to next fragment

class Step1(): Fragment() {

    lateinit var binding: Step1FragmentBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = Step1FragmentBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onStart() {
        super.onStart()

        binding.btnNext.setOnClickListener{ 
            //I want to change fragment here 
        }

    }
}

Sorry, if it's too much code, and thanks!

CodePudding user response:

The DialogFragmentContainer is the parent fragment of the ViewPager page fragments; i.e. it's the parent of Step1 through Step4 fragments.

And therefore you can access it from Step1 fragment, the fragment that you want to change the current ViewPager page using the parentFragment attribute:

binding.btnNext.setOnClickListener{ 
    //I want to change fragment here 
    (parentFragment as DialogFragmentContainer).binding.pager.currentItem = myItemPosition
}
  • Related