Skip to content Skip to sidebar Skip to footer

Two Pieces Of Fragments Are Displayed On Orientation Change To Landscape With ViewPager2

I am using ViewPager2 with WebView inside, on portrait mode it showing me single page, but when I switch to landscape mode it showing half of the current page and half of the previ

Solution 1:

It seems to be a bug in ViewPager2. https://issuetracker.google.com/issues/175796502?pli=1

I used the following workaround to fix the issue temporarily. The problem occures when I have a previous page, so what I did was switching to previous page and switch back to the current page without the smooth scrolling, I hope they will fix the bug as soon as possible.

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        val c = currentPageIndex
        binding.viewPager.setCurrentItem(if (currentPageIndex-1 > 0) currentPageIndex-1 else 0, false)
        binding.viewPager.setCurrentItem(c, false)
    }
}

Post a Comment for "Two Pieces Of Fragments Are Displayed On Orientation Change To Landscape With ViewPager2"