0

I am developing a Kotlin app and at some point I want to implement viewPager to swipe between fragments. I have one activity to navigate to the rest of the app through the navigation graph. I have not really understood how this swiping should work.

My question is, do I need to implement a new activity as well besides the pageAdapter? And how this activity is going to cooperate with the main one? My app currently has a splash screen, and after that I would like to have the swipe mode between the fragments.

halfer
  • 19,824
  • 17
  • 99
  • 186
loukous
  • 15
  • 1
  • 9

2 Answers2

3

I want to implement viewPager to swipe between fragments.

nice

I have one activity to navigate to the rest of the app through the navigation graph.

cool

do I need to implement a new activity as well besides the pageAdapter?

no

And how this activity is going to cooperate with the main one?

don't have a second activity, then it doesn't need to "cooperate"

I would like to have the swipe mode between the fragments.

https://gist.github.com/Zhuinden/c643f03a023a9cbe83fff6c75c948d3b

class MyFragmentPagerAdapter(
    private val context: Context,
    fragmentManager: FragmentManager
) : FragmentPagerAdapter(fragmentManager) {
    override fun getCount() = 2

    override fun getItem(position: Int) = when(position) {
        0 -> FirstFragment()
        1 -> SecondFragment()
        else -> throw IllegalStateException("Unexpected position $position")
    }

    override fun getPageTitle(position: Int): CharSequence = when(position) {
        0 -> context.getString(R.string.first)
        1 -> context.getString(R.string.second)
        else -> throw IllegalStateException("Unexpected position $position")
    }
}

class ParentFragment: Fragment() {
    override fun onCreateView(...) = ...

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val viewPager = view.findViewById(R.id.view_pager)
        viewPager.adapter = MyFragmentPagerAdapter(requireContext(), childFragmentManager)
        tabLayout.setupWithViewPager(viewPager)
    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • thank you for the reply, but I still don't get this. I have created the pageAdapter like you suggest. Where should the code from the parentFragment be implemented? I am new to this and I am sorry if this sounds like a silly question. – loukous Feb 29 '20 at 19:31
  • Also, should I apply the viewPager and tabLayout to all the fragments I wish to swipe? – loukous Feb 29 '20 at 19:39
  • You need the ViewPager, you might not need the TabLayout. – EpicPandaForce Feb 29 '20 at 20:25
1

Try this

in your viewPager

 class pageradapter (fm: FragmentManager) : FragmentStatePagerAdapter(fm){
override fun getItem(position: Int): Fragment {
    when(position){
        0-> return fragment1()
        1-> return fragment2() // you can add more if you have more fragments
        else-> return fragment3()

    }
}
override fun getCount(): Int {
    return 3   
 number of fragments that you have so the swiping could work
}

in your fragments 1 or 2 or 3 ...etc'

 class fragment1 : Fragment() {


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

in your Activity after the splash screen

class MainActivity : AppCompatActivity(){

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main4)

    val adapter = pageradapter(supportFragmentManager)
    val pager = findViewById<View>(R.id.pager) as ViewPager
    pager.adapter = adapter

}