Set default page for ViewPager in Android

前端 未结 5 2105
孤独总比滥情好
孤独总比滥情好 2020-11-30 02:04

I am using the following code, MAX is 2 pages. By default the position is 0 and adds a new page to the right. I inflate two layout files.

How can I show the page1 wh

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 02:48

    You need to set page only during first initialisation of the fragment. Later it will restore a selected by user position from a "saved instance state". So this looks a little better:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    
        pager.adapter = AwesomePagerAdapter()
        if (savedInstanceState == null) {
            pager.currentItem = 1
        }
    }
    

提交回复
热议问题