android viewpager change adapter

后端 未结 4 2047
花落未央
花落未央 2021-02-02 14:46

I\'m developing an app with a ViewPager for lateral scrolling tables, sometimes i need to change the adapter to load a different set of tables. I\'ve tried to do the following:<

4条回答
  •  执念已碎
    2021-02-02 15:32

    I see that @RRTW way worked for some people but it did not do the trick for me (although it was close). Without commitNow() instead of regular commit() it would not work properly. I did something like that:

    private fun changePagerAdapter(newAdapter: PagerAdapter) {
    
        val transaction = supportFragmentManager.beginTransaction()
            supportFragmentManager.fragments.forEach {
            transaction.remove(it)
        }
    
        transaction.commitNow()
        view_pager.adapter = newAdapter
    }
    

提交回复
热议问题