Wednesday, September 22, 2021

jetpack compose - Row scrolling

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.horizontalScroll
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
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.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold(){
        Scaffold(
            topBar = {TopAppBar(
                title = {Text(
                    "Compose - Row Scrolling",
                    color = Color.White)},
                backgroundColor = Color(0xFF58427C)) },
            content = {MainContent()},
            backgroundColor = Color(0xFFEDEAE0)
        )
    }


    @Composable
    fun MainContent(){
        val scrollState = rememberScrollState()

        Row(
            modifier = Modifier
                .background(Color(0xFFEDC9AF))
                .fillMaxWidth()
                .height(125.dp)
                .padding(12.dp)
                .horizontalScroll(scrollState),
            horizontalArrangement = Arrangement.spacedBy(12.dp)

        ) {
            repeat(100){ counter ->
                Card(
                    backgroundColor = if(counter%2 == 0)
                        Color(0xFFDA3287) else Color(0xFFF56FA1),
                    modifier = Modifier
                        .width(125.dp)
                        .fillMaxHeight()
                        .clip(RoundedCornerShape(12.dp))
                ){
                    Box(
                        contentAlignment = Alignment.Center,
                    ){
                        Text(
                            text = "${counter+1}",
                            style = MaterialTheme.typography.h4
                        )
                    }
                }
            }
        }
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        //GetScaffold()
    }
}