Skip to content

Instantly share code, notes, and snippets.

@shkcodes
Created July 17, 2021 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shkcodes/0fbdc63171bf5bd3de193ee8b3bf21d7 to your computer and use it in GitHub Desktop.
Save shkcodes/0fbdc63171bf5bd3de193ee8b3bf21d7 to your computer and use it in GitHub Desktop.
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