2

I am using HorizontalViewPager from Accompanist to show an image in each page of the viewpager. I am loading images with Coil. Here's the code:

val pagerState = rememberPagerState()

HorizontalPager(count = photos.size, state = pagerState) {
    idx ->
    photos[idx].let { cur ->
        Image(
            painter = rememberImagePainter(url),
            contentDescription = null,
            modifier - Modifier.fillMaxSize(), 
            contentScale = ContentScale.FillHeight,
            alignment = Alignment.Center
        )
    }
}

It is working fine. But, Images are being loaded only after the page is opened. I was wondering if there is a way to preload the content of the adjacent pages of viewpager before they are opened.

ViewPager from AndroidX offers similar behavior with setOffscreenPageLimit method.

SpiralDev
  • 7,011
  • 5
  • 28
  • 42
  • Turns out old versions of the accompanist-pager had a offScreenLimit parametr which was [removed](https://github.com/google/accompanist/blob/6547266f2577d05353c86c1586c6466f00aadb6f/docs/pager.md#horizontalpager--verticalpager) in 0.19.0 version. What do I do now? – SpiralDev Mar 23 '22 at 14:53
  • Does [this](https://stackoverflow.com/a/69602438/3585796) answer your question? – Phil Dukhov Mar 24 '22 at 03:35
  • @PylypDukhov I gave it a try, but the loading time is still not that short. During loading time page is blank and it is noticable – SpiralDev Mar 24 '22 at 08:56
  • I noticed that If I slightly swipe the current page to open the edge of the next page, then the next page starts loading, I wonder if this can help me to do some hack – SpiralDev Mar 24 '22 at 08:56
  • Using my method you can start loading next page (or a couple of pages) image from the current page, is this what you're trying? – Phil Dukhov Mar 24 '22 at 08:58
  • Yes, I got your method and tried it. It is loading the images into memory (at least thats what documentation says), but the time taken to load the image from memory into Image view is still noticable somehow – SpiralDev Mar 24 '22 at 09:07
  • I was able to reproduce this, I think it is a bug and should be reported to Coil. The `offscreenLimit` was removed [during the rewrite](https://github.com/google/accompanist/pull/678) to lazy views, this solved some problems, but `offscreenLimit` became impossible to maintain. Perhaps someday it will be rewritten again, but for now you will have to live without it. – Phil Dukhov Mar 24 '22 at 09:29
  • @PylypDukhov Funny but when I tried your method inside the Pager block and loaded the next page image simultaneously with the current page image, it is working :). Thanks! – SpiralDev Mar 25 '22 at 06:07

0 Answers0