Same fragment creating on sliding in ViewPager2

132
May 16, 2021, at 03:10 AM

I have 2 fragments that I want to show in my viewpager2 but only one shows in the view pager in both the tabs even though I am passing two fragments in my adapter although titles that I set are visible correctly.

Currently I am using viewpager2 with collapsing toolbar

ParentFragment

class MessagesFragment : Fragment() {
    private lateinit var binding: FragmentMessagesBinding
    private lateinit var adapter:ViewPager2Adapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View {
//        context?.theme?.applyStyle(R.style.ViewPagerTheme, true);
        binding = FragmentMessagesBinding.inflate(inflater, container, false)
        val fm: FragmentManager? = activity?.supportFragmentManager
        adapter = fm?.let { ViewPager2Adapter(it, lifecycle) }!!
        binding.pager.adapter = adapter
        binding.tabLayout.addTab(binding.tabLayout.newTab().setText("First"));
        binding.tabLayout.addTab(binding.tabLayout.newTab().setText("Second"));
        binding.pager.setPageTransformer(ZoomOutPageTransformer())
        binding.tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                binding.pager.currentItem = tab.position
            }
            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })
        binding.pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                binding.tabLayout.selectTab(binding.tabLayout.getTabAt(position))
            }
        })
        
        return binding.root
    }

}

ViewPagerAdapter

class ViewPager2Adapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
    FragmentStateAdapter(fragmentManager, lifecycle) {
    override fun createFragment(position: Int): Fragment {
        when (position) {
            1 -> { return ViewpagerFragment() }
            2 -> {return ViewPagerFragment2() }
        }
        return ViewpagerFragment()
    }
    override fun getItemCount(): Int {
        return 2
    }
}
Answer 1

You need to change the when cases from 1, 2 to 0, 1, as positions are 0-based

override fun createFragment(position: Int): Fragment {
    when (position) {
        0 -> { return ViewpagerFragment() }
        1 -> {return ViewPagerFragment2() }
    }
    return ViewpagerFragment()
}
Rent Charter Buses Company
READ ALSO
When to use text/plain instead of text/html?

When to use text/plain instead of text/html?

When is it recommended to use text/plain content type over text/html

45
Getting wrong timezone/date from PHP

Getting wrong timezone/date from PHP

When I echo the date_default_timezone_get(); & phpinfo(); I get Europe/Berlin when I should be getting America/New_York

106
Trying to record webcam video without opening and displaying window python

Trying to record webcam video without opening and displaying window python

Hello I followed a tutorial from https://wwwcodingforentrepreneurs

100
Cannot return results from stored procedure using Python cursor

Cannot return results from stored procedure using Python cursor

For some odd reason I can't get results from a callproc call in a Python test appThe stored procedure in MqSQL 5

154