Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pager Indicators] Fix: #768 #783

Merged
merged 5 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fun Modifier.pagerTabIndicatorOffset(
val targetIndicatorOffset: Dp
val indicatorWidth: Dp

val currentTab = tabPositions[pagerState.currentPage]
val currentTab = tabPositions[minOf(tabPositions.lastIndex, pagerState.currentPage)]
val targetPage = pagerState.targetPage
val targetTab = tabPositions.getOrNull(targetPage)

Expand Down
4 changes: 4 additions & 0 deletions pager/src/main/java/com/google/accompanist/pager/Pager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ internal fun Pager(
(flingBehavior as? SnappingFlingBehavior)?.animationTarget
}

LaunchedEffect(count) {
state.currentPage = minOf(count-1, state.currentPage)
ch4rl3x marked this conversation as resolved.
Show resolved Hide resolved
}

// Once a fling (scroll) has finished, notify the state
LaunchedEffect(state) {
// When a 'scroll' has finished, notify the state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class PagerState(
@get:IntRange(from = 0)
var currentPage: Int
get() = _currentPage
private set(value) {
internal set(value) {
if (value != _currentPage) {
_currentPage = value
if (DebugLog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
Expand All @@ -34,8 +36,11 @@ import androidx.compose.material.TabRowDefaults
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -62,6 +67,9 @@ class HorizontalPagerTabsSample : ComponentActivity() {
}
}

val largeList = listOf("Home", "Shows", "Movies", "Books", "Really long movies", "Short audiobooks")
val smallList = listOf("Home", "Shows", "Movies")

@OptIn(ExperimentalPagerApi::class)
@Composable
private fun Sample() {
Expand All @@ -74,8 +82,8 @@ private fun Sample() {
},
modifier = Modifier.fillMaxSize()
) {
val pages = remember {
listOf("Home", "Shows", "Movies", "Books", "Really long movies", "Short audiobooks")
var pages by remember {
ch4rl3x marked this conversation as resolved.
Show resolved Hide resolved
mutableStateOf(largeList)
}

Column(Modifier.fillMaxSize()) {
Expand All @@ -84,6 +92,19 @@ private fun Sample() {
// Remember a PagerState
val pagerState = rememberPagerState()

Button(
modifier = Modifier.padding(bottom = 10.dp),
onClick = {
if(pages == largeList) {
pages = smallList
} else {
pages = largeList
}
}
) {
Text("Change data count")
}

ScrollableTabRow(
// Our selected tab is our current page
selectedTabIndex = pagerState.currentPage,
Expand Down