<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Constantinos Levi Ibrushi on Medium]]></title>
        <description><![CDATA[Stories by Constantinos Levi Ibrushi on Medium]]></description>
        <link>https://medium.com/@k.ibrushi?source=rss-82859d22b47e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*p9UBCZgQOZ3YjduAK-Ciog.jpeg</url>
            <title>Stories by Constantinos Levi Ibrushi on Medium</title>
            <link>https://medium.com/@k.ibrushi?source=rss-82859d22b47e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 11:55:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@k.ibrushi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Jetpack Compose: HorizontalPager with PagerIndicator & Infinity scroll]]></title>
            <link>https://medium.com/xmglobal/jetpack-compose-horizontalpager-with-pagerindicator-infinity-scroll-32f6e9357974?source=rss-82859d22b47e------2</link>
            <guid isPermaLink="false">https://medium.com/p/32f6e9357974</guid>
            <category><![CDATA[android-viewpager]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[viewpager]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Constantinos Levi Ibrushi]]></dc:creator>
            <pubDate>Mon, 29 May 2023 10:38:20 GMT</pubDate>
            <atom:updated>2023-06-23T15:44:40.497Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we will explore the process of creating an infinite HorizontalPager with a PagerIndicator by using Compose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*cTGSU8GkxYNIc875-Hnabw.gif" /><figcaption>Android | Kotlin | Jetpack Compose | ViewPager | PagerIndicator</figcaption></figure><p>As you may already know, Jetpack Compose does not have a built-in ViewPager component. However, we can incorporate ViewPager functionality into our project by adding the respective <a href="https://github.com/google/accompanist">accompanist</a> library dependency to our build.gradle file.</p><pre>implementation &quot;com.google.accompanist:accompanist-pager:0.28.0&quot;</pre><p>In order to incorporate indicators, we will also utilize the respective library from <a href="https://github.com/google/accompanist">accompanist</a>.</p><pre>implementation &quot;com.google.accompanist:accompanist-pager-indicators:0.28.0&quot;</pre><blockquote>Note: For this project, we are using Compose version 1.3.1 and Kotlin version 1.8.10.</blockquote><h3>Let’s begin by creating a HorizontalPager.</h3><p>Thanks to the <a href="https://github.com/google/accompanist">accompanist</a> library, creating a HorizontalPager is a simple task.</p><pre>HorizontalPager(<br>  count = pageCount,<br>  state = pagerState,<br>  modifier = modifier<br>) {<br>   // page content<br>}</pre><ul><li><strong>Count</strong>: The number of pages</li></ul><p>We set the count to a significantly large number, such as Int.MAX_VALUE, so we can achieve infinite scrolling behavior.</p><pre>// Used Int.MAX_VALUE for infinity scroll<br>val pageCount = Int.MAX_VALUE</pre><ul><li><strong>State: </strong>The state object that is used to control or observe the pager’s state.</li></ul><p>For <strong>state </strong>creation,<strong> </strong>we only need the initialPage value. In order to enable infinite scrolling in both directions, we should start from the middle of the given page count. Therefore, the initialPage can be set as in the following example:</p><pre>val middlePage = pageCount / 2<br>val pagerState = rememberPagerState(initialPage = middlePage)</pre><p>While everything may seem correct at first glance, we will soon encounter an issue concerning the initial displayed page.</p><h3>The Trick 🪄</h3><p>In order to achieve infinite behavior for the ViewPager, we implemented a solution by setting the count to a very large number. However, our actual list of items (trophies) is considerably smaller. To ensure that the ViewPager displays the correct page from our real list without creating duplicate pages, we need to handle the page number appropriately.</p><p>To address this challenge, we divide the provided page number by the size of the trophies list. The remainder of this division allows us to obtain the correct page index within our real list. By performing this calculation, we ensure that the ViewPager only displays the actual items from the list, preventing any duplications.</p><p>By utilizing this approach, we can seamlessly navigate through the trophies list within the ViewPager while maintaining its infinite behavior.</p><p>Take a look at the following example:</p><pre>val realSize = trophies.size<br><br>HorizontalPager(<br>    count = pageCount,<br>    state = pagerState,<br>    modifier = modifier<br>) { page -&gt; <br>    val realPage = page % realSize<br>    // max value is trophies.size<br>    TrophyWidget(realPage, trophy = trophies[realPage])<br>}</pre><p>Did you get it? No? 🤔 (<em>Let&#39;s do the maths then!)</em></p><figure><img alt="ViewPager position" src="https://cdn-images-1.medium.com/max/1024/1*hounONQjLBcA-0RoFalaVQ.png" /></figure><p>Looks like our math is working just fine! 🎉</p><p>If you take a closer look, it becomes apparent that the initial page is not the first on the trophies list. And actually, the initial state depended on the trophies list size. To resolve this discrepancy and ensure the correct initial state, it is necessary to calculate and pass a parameter to the ViewPager state.</p><pre>val realSize = trophies.size<br><br>val middlePage = pageCount / 2<br>// Init the PagerState with a very large number and make it always start from the first item of the real list<br>val pagerState = rememberPagerState(initialPage = middlePage - (middlePage % realSize))</pre><p>By decreasing the middlePage with the result of the mod of middlePage and trophies size we are making sure that the ViewPager is going to start from the begging of the trophies list.</p><h3>Pager Indicator</h3><p>Adding indicators is also very simple, we just need to add HorizontalPagerIndicatorand pass pagerStateas a parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*-zcdWQg-kChevEXwylPlOw.png" /><figcaption>Android | HorizontalPager | Indicator</figcaption></figure><p>⚠️ However, there is an issue here! If you try to use the pagerState without specifying the real size of the list (trophies), then the app will 🧨 . That’s because the default pageCount of HorizontalPagerIndicator is set to the value of PagerState.pageCount, which, in our case, is a very large number.</p><p>Fortunately, we can specify the pageCount by adding it as a parameter in HorizontalPagerIndicator.</p><p>Take a look at the example:</p><pre>HorizontalPagerIndicator(<br>    pagerState = pagerState,<br>    pageCount = realSize,<br>    pageIndexMapping = { page -&gt; page % realSize },<br>    activeColor = Color.White,<br>    modifier = modifier<br>        .align(Alignment.BottomCenter)<br>        .padding(bottom = 12.dp)<br>)</pre><p>Also we have to<strong> describe how to obtain the position of the active indicator</strong> by passing the page into pageIndexMapping function. This can be achieved by dividing pagerState.currentPage by the size of the trophies list.</p><p>As you can see in the example above, this can be implemented with the following code snippet:</p><pre>pageIndexMapping = { page -&gt; page % realSize }</pre><p>To get the position of the active indicator, you can use the pageIndexMapping function and perform the mod using pagerState.currentPage and the size of the trophies list.</p><h3>Auto-Scroll</h3><p>If you need also your pager to auto-scroll, you can use the following code snippet:</p><pre>// Start auto-scroll effect<br>LaunchedEffect(isDraggedState) {<br>    // convert compose state into flow<br>    snapshotFlow { isDraggedState.value }<br>        .collectLatest { isDragged -&gt;<br>            // if not isDragged start slide animation<br>            if (!isDragged) {<br>                // infinity loop<br>                while (true) {<br>                    // duration before each scroll animation<br>                    delay(5_000L)<br>                    runCatching {<br>                        pagerState.animateScrollToPage(pagerState.currentPage.inc() % pagerState.pageCount)<br>                    }<br>                }<br>            }<br>        }<br>}</pre><h3>Let’s put it all together</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51ce74af71ade522c14632b4f7c13aee/href">https://medium.com/media/51ce74af71ade522c14632b4f7c13aee/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32f6e9357974" width="1" height="1" alt=""><hr><p><a href="https://medium.com/xmglobal/jetpack-compose-horizontalpager-with-pagerindicator-infinity-scroll-32f6e9357974">Jetpack Compose: HorizontalPager with PagerIndicator &amp; Infinity scroll</a> was originally published in <a href="https://medium.com/xmglobal">XM Global</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>