Skip to content

Instantly share code, notes, and snippets.

@stevdza-san
Created September 8, 2022 12:08
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 stevdza-san/06c361c7703d40c9163c25cec828aa28 to your computer and use it in GitHub Desktop.
Save stevdza-san/06c361c7703d40c9163c25cec828aa28 to your computer and use it in GitHub Desktop.
isScrolled extension property for the LazyListState
import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ContentAlpha
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun HomeScreen() {
val lazyListState = rememberLazyListState()
Scaffold(
content = {
Box(modifier = Modifier.fillMaxSize()) {
List(lazyListState = lazyListState)
Text(
modifier = Modifier.align(Alignment.Center),
text = if (lazyListState.isScrolled) "Scrolling..." else "Idle",
color = if (lazyListState.isScrolled) MaterialTheme.colors.primary
else MaterialTheme.colors.onBackground,
style = TextStyle(fontSize = MaterialTheme.typography.h5.fontSize)
)
}
}
)
}
@Composable
fun List(lazyListState: LazyListState) {
val items = remember {
List(size = 25) { it }
}
LazyColumn(
contentPadding = PaddingValues(12.dp),
state = lazyListState,
verticalArrangement = Arrangement.spacedBy(24.dp)
) {
items(
items = items,
key = { it }
) {
ItemHolder()
}
}
}
@Composable
fun ItemHolder() {
Spacer(
modifier = Modifier
.fillMaxWidth()
.height(24.dp)
.clip(RoundedCornerShape(20.dp))
.background(Color.LightGray.copy(alpha = ContentAlpha.disabled))
)
}
val LazyListState.isScrolled: Boolean
get() = firstVisibleItemIndex > 0 || firstVisibleItemScrollOffset > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment