Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rubywai/f28275074efa249436b78bbf2f32aef2 to your computer and use it in GitHub Desktop.

Select an option

Save rubywai/f28275074efa249436b78bbf2f32aef2 to your computer and use it in GitHub Desktop.
@11 composable list part 2 (sticky header and scroll controller)
package com.rubylearner.composeapp
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ListItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.rubylearner.composeapp.ui.theme.ComposeAppTheme
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
ComposeAppTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
TopAppBar(
title = {
Text("My Compose Lesson")
}
)
}
) { innerPadding ->
MyCompose(
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
@Composable
fun MyCompose(modifier: Modifier) {
//mutable
//recomposition
val lazyListState = rememberLazyListState()
val coroutineScope = rememberCoroutineScope()
val contactStateList = remember {
mutableStateListOf("A", "B","C","D","E")
}
Column(modifier = modifier) {
LazyRow() {
items(contactStateList.size) {
TextButton(
onClick = {
coroutineScope.launch {
lazyListState.scrollToItem(21 * it)
}
}
) {
Text(contactStateList[it])
}
}
}
ElevatedButton(
onClick = {
coroutineScope.launch {
lazyListState.animateScrollToItem(0)
}
}
) {
Text("Scroll to top")
}
LazyColumn(
state = lazyListState
) {
for(i in 0 until contactStateList.size) {
stickyHeader {
Box(
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
.background(Color.Blue)
) {
Box(
modifier = Modifier
.padding(start = 8.dp)
.align(Alignment.CenterStart)
) {
Text(contactStateList[i])
}
}
}
items(20) {
ListItem(
modifier = Modifier.animateItem(
fadeInSpec = tween(durationMillis = 1000),
fadeOutSpec = tween(durationMillis = 1000),
placementSpec = spring(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = Spring.StiffnessLow
)
),
leadingContent = {
Icon(Icons.Filled.Person, contentDescription = "Person $it")
},
headlineContent = {
Text("${contactStateList[i]} Person ${it + 1}")
},
supportingContent = {
Text("099999999")
},
trailingContent = {
IconButton(
onClick = {
contactStateList.removeAt(it)
}
) {
Icon(
Icons.Default.Delete,
contentDescription = "Delete Person $it",
)
}
}
)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment