-
-
Save shkcodes/0fbdc63171bf5bd3de193ee8b3bf21d7 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
val listState = rememberLazyListState() | |
val currentlyPlayingItem = determineCurrentlyPlayingItem(listState, state.items) | |
LazyColumn(state = listState) { | |
... | |
} | |
... | |
private fun determineCurrentlyPlayingItem(listState: LazyListState, items: TweetItems): TweetItem? { | |
val layoutInfo = listState.layoutInfo | |
val visibleTweets = layoutInfo.visibleItemsInfo.map { items[it.index] } | |
val tweetsWithVideo = visibleTweets.filter { it.hasAnimatedMedia } | |
return if (tweetsWithVideo.size == 1) { | |
tweetsWithVideo.first() | |
} else { | |
val midPoint = (layoutInfo.viewportStartOffset + layoutInfo.viewportEndOffset) / 2 | |
val itemsFromCenter = | |
layoutInfo.visibleItemsInfo.sortedBy { abs((it.offset + it.size / 2) - midPoint) } | |
itemsFromCenter.map { items[it.index] }.firstOrNull { it.hasAnimatedMedia } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment