'OffscreenPageLimit behaviour for ViewPager from Accompanist

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source