Last active
May 13, 2021 09:06
-
-
Save rahulsainani/a359335dca1455b131e55d9403f82ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun FeatureList( | |
list: List<Feature>, | |
modifier: Modifier, | |
) { | |
var itemsListState by remember { mutableStateOf(list) } | |
val lazyListState = rememberLazyListState() | |
LazyRow( | |
state = lazyListState, | |
modifier = modifier, | |
) { | |
items(itemsListState) { | |
FeatureTile(feature = it) | |
Spacer(modifier = Modifier.width(Dimens.grid_1)) | |
if (it == itemsListState.last()) { | |
val currentList = itemsListState | |
val secondPart = currentList.subList(0, lazyListState.firstVisibleItemIndex) | |
val firstPart = currentList.subList(lazyListState.firstVisibleItemIndex, currentList.size) | |
rememberCoroutineScope().launch { | |
lazyListState.scrollToItem(0, maxOf(0, lazyListState.firstVisibleItemScrollOffset - SCROLL_DX_INT)) | |
} | |
itemsListState = firstPart + secondPart | |
} | |
} | |
} | |
LaunchedEffect(Unit) { | |
autoScroll(lazyListState) | |
} | |
} | |
private tailrec suspend fun autoScroll(lazyListState: LazyListState) { | |
lazyListState.scroll(MutatePriority.PreventUserInput) { | |
scrollBy(SCROLL_DX) | |
} | |
delay(DELAY_BETWEEN_SCROLL_MS) | |
autoScroll(lazyListState) | |
} | |
private const val DELAY_BETWEEN_SCROLL_MS = 8L | |
private const val SCROLL_DX = 1f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment