MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDownward
import androidx.compose.material.icons.filled.ArrowUpward
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
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.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent()
}
}
@Composable
fun MainContent(){
val list = (1..100).map { it.toString() }
val listState = rememberLazyListState()
val coroutineScope = rememberCoroutineScope()
Box(
modifier = Modifier.fillMaxSize()
) {
LazyColumn(
state = listState,
modifier = Modifier
.background(Color(0xFFFFFFFF))
.wrapContentHeight()
.fillMaxWidth(),
contentPadding = PaddingValues(12.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(list){ txt ->
Card(
backgroundColor = Color(0xFF9ACD32),
modifier = Modifier
.fillMaxWidth(),
elevation = 8.dp,
shape = RoundedCornerShape(16.dp)
) {
Text(
text = txt,
fontWeight = FontWeight.Bold,
fontSize = 30.sp,
color = Color(0xFFFFFFFF),
textAlign = TextAlign.Center,
modifier = Modifier.padding(16.dp)
)
}
}
}
val isTopButtonVisible = remember {
derivedStateOf {
listState.firstVisibleItemIndex > 0
}
}
FloatingActionButton(
onClick = {
coroutineScope.launch {
if (isTopButtonVisible.value){
listState.animateScrollToItem(0)
}else{
listState.animateScrollToItem(list.size)
}
}
},
shape = RoundedCornerShape(50),
backgroundColor = Color(0xFFFF55A3),
modifier = Modifier
.align(Alignment.BottomEnd)
.padding(16.dp)
) {
if (isTopButtonVisible.value){
Icon(Icons.Filled.ArrowUpward,"")
}else{
Icon(Icons.Filled.ArrowDownward,"")
}
}
}
}
@Preview
@Composable
fun ComposablePreview(){
//MainContent()
}
}
- jetpack compose - Image from assets folder
- jetpack compose - How to use LazyColumn
- jetpack compose - Accessing string resources
- jetpack compose - String resource positional formatting
- jetpack compose - String resource plurals
- jetpack compose - Text custom font
- jetpack compose - Accessing font resource
- jetpack compose - LazyColumn content spacing padding
- jetpack compose - LazyColumn smooth scrolling
- jetpack compose - LazyColumn scroll to position
- jetpack compose - LazyColumn alternate item color
- jetpack compose - LazyColumn sticky header
- jetpack compose - ExpandIn ShrinkOut animation
- jetpack compose - SlideIn SlideOut animation
- jetpack compose - Infinite repeatable animation