Skip to content

Commit

Permalink
[Fix] Set offscreen page limit to 1 for ViewPager2 in image viewer.
Browse files Browse the repository at this point in the history
Because it was 1 for the old ViewPager and provided better user
experience.

Fixes: #1033
  • Loading branch information
zhanghai committed Sep 23, 2023
1 parent f7d5056 commit c31e095
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,22 @@ class ImageViewerFragment : Fragment(), ConfirmDeleteDialogFragment.Listener {
}
// This will set up window flags.
systemUiHelper.show()
adapter = ImageViewerAdapter(viewLifecycleOwner) { systemUiHelper.toggle() }
adapter.replace(paths)
binding.viewPager.adapter = adapter
// ViewPager saves its position and will restore it later.
binding.viewPager.setCurrentItem(args.position, false)
binding.viewPager.setPageTransformer(DepthPageTransformer)
binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
updateTitle()
}
})
adapter = ImageViewerAdapter(viewLifecycleOwner) { systemUiHelper.toggle() }.apply {
replace(paths)
}
binding.viewPager.apply {
// 1 is the default for the old androidx.viewpager.widget.ViewPager.
offscreenPageLimit = 1
adapter = this@ImageViewerFragment.adapter
// ViewPager saves its position and will restore it later.
setCurrentItem(args.position, false)
setPageTransformer(DepthPageTransformer)
registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
updateTitle()
}
})
}
}

override fun onViewStateRestored(savedInstanceState: Bundle?) {
Expand Down

0 comments on commit c31e095

Please sign in to comment.