I have an Activity with a ViewPager containing Fragments that are summoned with custom buttons. Currently, my custom adapter's getItem method is being called, but the pager is going blank rather than going to the new fragment

Here is my adapter :

class ScreenSlidePagerAdapter(fragmentManager: FragmentManager) : FragmentStatePagerAdapter(fragmentManager) {


    private val NUM_FRAGMENTS = 2



    override fun getCount(): Int = NUM_FRAGMENTS

    override fun getItem(position: Int): Fragment {

        var fragment: Fragment = WelcomeFragment.newInstance()


        when (position) {
            0 -> fragment = WelcomeFragment.newInstance()
            1 -> fragment = LanguageSelectFragment.newInstance()


        }

        println(position)



        return fragment
    }
}

Here is the method that gets called on button click. The views are given a tag corresponding to their Fragment's position in the adapter, and this is retrieved on click to pull up the right fragment.

private fun switchScreens(view: View) {


        val fragment = mPagerAdapter.getItem(view.getTag().toString().toInt())
        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.main_view_pager, fragment)
        fragmentTransaction.commit()
    }

And my fragment:

class LanguageSelectFragment : Fragment() {


    private var listener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_language_select, container, false)
    }


    fun onButtonPressed(uri: Uri) {
        listener?.onFragmentInteraction(uri)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnFragmentInteractionListener) {
            listener = context
        } else {

        }
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }


    interface OnFragmentInteractionListener {

        fun onFragmentInteraction(uri: Uri)
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            LanguageSelectFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}
Answer

Do you have a ViewPager container for your framgents ?

  • If yes, that's not the way to use the pagerAdapter. You should see this link. It's in Java but you can easily adapt it to kotlin.
  • If no, I'm not sure what you are trying to do but I think the ViewPager is what you need.

If you just want to switch fragments on a click, just use a FrameLayout to display the fragment you want and switch between them with transactions. I'm not familiar with kotlin but something like this should work.

private fun switchScreens(i: Integer) {

    val fragment;

    when (i) {
        0 -> fragment = WelcomeFragment.newInstance()
        1 -> fragment = LanguageSelectFragment.newInstance()
    }

    val fragmentManager = supportFragmentManager
    val fragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.main_view_pager, fragment)
    fragmentTransaction.commit()
}

If you want to slide to one fragment to another, you can use a ViewPager (see the link above).

Best

  • 1
Reply Report
      • 2
    • I want a slide animation directly from one fragment to another via button click, but I do not want the user to be able to slide it without the button. Edit: And FrameLayout worked for that. Thank you.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions