Last active
February 7, 2025 17:56
-
-
Save slaviboy/50e8d852f3e46543aad061c4141af87a to your computer and use it in GitHub Desktop.
Infinite Circular List using Jetpack Compose using Kotlin (Scroll Wheel in Jetpack Compose)
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
package com.slaviboy.test | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.ExperimentalFoundationApi | |
import androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehavior | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.foundation.lazy.rememberLazyListState | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.layout.onGloballyPositioned | |
import androidx.compose.ui.layout.positionInParent | |
import androidx.compose.ui.platform.LocalDensity | |
import androidx.compose.ui.text.TextStyle | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.unit.sp | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
var day by remember { | |
mutableStateOf(1) | |
} | |
var month by remember { | |
mutableStateOf(1) | |
} | |
var year by remember { | |
mutableStateOf(2023) | |
} | |
var lastDayInMonth by remember { | |
mutableStateOf(30) | |
} | |
fun adjustDay() { | |
val newLastDayInMonth = lastDayInMonth(month, year) | |
if (lastDayInMonth != newLastDayInMonth) { | |
lastDayInMonth = newLastDayInMonth | |
if (day > newLastDayInMonth) { | |
day = lastDayInMonth | |
} | |
} | |
} | |
Row( | |
modifier = Modifier | |
.fillMaxSize(), | |
horizontalArrangement = Arrangement.SpaceEvenly | |
) { | |
InfiniteCircularList( | |
width = 50.dp, | |
itemHeight = 70.dp, | |
items = (1..lastDayInMonth).toMutableList(), | |
initialItem = day, | |
textStyle = TextStyle(fontSize = 23.sp), | |
textColor = Color.LightGray, | |
selectedTextColor = Color.Black, | |
onItemSelected = { i, item -> | |
day = item | |
} | |
) | |
InfiniteCircularList( | |
width = 200.dp, | |
itemHeight = 70.dp, | |
items = listOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"), //(1..30).map { it.toString() }, | |
initialItem = "December", | |
textStyle = TextStyle(fontSize = 23.sp), | |
textColor = Color.LightGray, | |
selectedTextColor = Color.Black, | |
onItemSelected = { i, item -> | |
month = i + 1 | |
adjustDay() | |
} | |
) | |
InfiniteCircularList( | |
width = 100.dp, | |
itemHeight = 70.dp, | |
items = (1900..2023).toList(), | |
initialItem = 2023, | |
textStyle = TextStyle(fontSize = 23.sp), | |
textColor = Color.LightGray, | |
selectedTextColor = Color.Black, | |
onItemSelected = { i, item -> | |
year = item | |
adjustDay() | |
} | |
) | |
} | |
} | |
} | |
private fun lastDayInMonth(month: Int, year: Int): Int { | |
return if (month != 2) { | |
31 - (month - 1) % 7 % 2 | |
} else { | |
if (year and 3 == 0 && (year % 25 != 0 || year and 15 == 0)) { | |
29 | |
} else { | |
28 | |
} | |
} | |
} | |
} | |
@OptIn(ExperimentalFoundationApi::class) | |
@Composable | |
fun <T> InfiniteCircularList( | |
width: Dp, | |
itemHeight: Dp, | |
numberOfDisplayedItems: Int = 3, | |
items: List<T>, | |
initialItem: T, | |
itemScaleFact: Float = 1.5f, | |
textStyle: TextStyle, | |
textColor: Color, | |
selectedTextColor: Color, | |
onItemSelected: (index: Int, item: T) -> Unit = { _, _ -> } | |
) { | |
val itemHalfHeight = LocalDensity.current.run { itemHeight.toPx() / 2f } | |
val scrollState = rememberLazyListState(0) | |
var lastSelectedIndex by remember { | |
mutableStateOf(0) | |
} | |
var itemsState by remember { | |
mutableStateOf(items) | |
} | |
LaunchedEffect(items) { | |
var targetIndex = items.indexOf(initialItem) - 1 | |
targetIndex += ((Int.MAX_VALUE / 2) / items.size) * items.size | |
itemsState = items | |
lastSelectedIndex = targetIndex | |
scrollState.scrollToItem(targetIndex) | |
} | |
LazyColumn( | |
modifier = Modifier | |
.width(width) | |
.height(itemHeight * numberOfDisplayedItems), | |
state = scrollState, | |
flingBehavior = rememberSnapFlingBehavior( | |
lazyListState = scrollState | |
) | |
) { | |
items( | |
count = Int.MAX_VALUE, | |
itemContent = { i -> | |
val item = itemsState[i % itemsState.size] | |
Box( | |
modifier = Modifier | |
.height(itemHeight) | |
.fillMaxWidth() | |
.onGloballyPositioned { coordinates -> | |
val y = coordinates.positionInParent().y - itemHalfHeight | |
val parentHalfHeight = (coordinates.parentCoordinates?.size?.height ?: 0) / 2f | |
val isSelected = (y > parentHalfHeight - itemHalfHeight && y < parentHalfHeight + itemHalfHeight) | |
if (isSelected && lastSelectedIndex != i) { | |
onItemSelected(i % itemsState.size, item) | |
lastSelectedIndex = i | |
} | |
}, | |
contentAlignment = Alignment.Center | |
) { | |
Text( | |
text = item.toString(), | |
style = textStyle, | |
color = if (lastSelectedIndex == i) { | |
selectedTextColor | |
} else { | |
textColor | |
}, | |
fontSize = if (lastSelectedIndex == i) { | |
textStyle.fontSize * itemScaleFact | |
} else { | |
textStyle.fontSize | |
} | |
) | |
} | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment