[Compose]Example2-LazyColumn

๐Ÿ‘‰๐Ÿป ์•„๋ž˜๋Š” ๋ ˆ์ด์ง€์ปฌ๋Ÿผ์•ˆ์— ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํ† ์ŠคํŠธ ๋ฉ”์„ธ์ง€๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.
Below is the code that executes a toast message when a button is clicked in the lazy column.

๐Ÿ‘‰๐Ÿป ๋ ˆ์ด์ง€์ปฌ๋Ÿผ์€ xml๋ฐฉ์‹์—์„œ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ ๋น„์Šทํ•œ ์—ญํ• ์ž…๋‹ˆ๋‹ค.
LazyColumn plays a similar role to RecyclerView in XML style method.

๐Ÿ‘‰๐Ÿป ํ† ์ŠคํŠธ๋ฉ”์„ธ์ง€๋Š” xml๋ฐฉ์‹๊ณผ compose๋ฐฉ์‹์€ ์‚ฌ์šฉ๋ฐฉ๋ฒ•, ๋””์ž์ธ,๊ธฐ๋Šฅ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
Toast messages in XML and Compose have different usage methods, designs, and functions.

๐Ÿ‘‰๐Ÿป ์ฝœ๋ฐฑํ•จ์ˆ˜ : ํŒŒ๋ผ๋ฉ”ํ„ฐ๋กœ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
Callback function: This means using a function as a parameter.

๐Ÿ‘‰๐Ÿป ๋ ˆ์ด์ง€์ปฌ๋Ÿผ์—์„œ ๋ฒ„ํŠผ์„ ์ •๋ ฌํ•˜๋ ค๋ฉด box์•„์ดํ…œ์œผ๋กœ ๊ฐ์‹ธ์•ผํ•ฉ๋‹ˆ๋‹ค.(์†์„ฑ์ด ์—†์Œ)
To align buttons in a lazy column, you need to wrap them in a box item (the attribute does not exist).

1.์ฝ”๋“œ/Code

package com.freelifemakers.lazycolumnexample

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.freelifemakers.lazycolumnexample.ui.theme.LazyColumnExampleTheme
import kotlinx.coroutines.launch
/*
*
* LazyColumn์—์„œ ์•„์ดํ…œ ์ „์ฒด์ •๋ ฌ,๊ฐœ๋ณ„์ •๋ ฌ
* - ์ „์ฒด์ •๋ ฌ : horizontalAlignment = Alignment.CenterHorizontally ์‚ฌ์šฉ
* - ๊ฐœ๋ณ„์ •๋ ฌ : ํ…์ŠคํŠธ๋Š” ์ž์ฒด ์†์„ฑ ์‚ฌ์šฉ,๋ฒ„ํŠผ์€ box๋กœ ๊ฐ์‹ธ๊ณ  ๊ฐ€์šด๋ฐ ์ •๋ ฌ
* ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํ† ์ŠคํŠธ๋ฉ”์„ธ์ง€ ๋„์šฐ๊ธฐ(2๊ฐ€์ง€ ๋ฐฉ์‹)
*
* */
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            LazyColumnExampleTheme {
                // ํ† ์ŠคํŠธ๋ฉ”์„ธ์ง€2: SnackBar ์ƒํƒœ์™€ Scope๋ฅผ ์ตœ์ƒ์œ„์—์„œ ์ƒ์„ฑ
                val snackbarHostState = remember { SnackbarHostState() }
                val scope = rememberCoroutineScope()

                Scaffold(modifier = Modifier.fillMaxSize(),

                    // ํ† ์ŠคํŠธ๋ฉ”์„ธ์ง€2:!!! ์ตœ์ƒ์œ„ Scaffold์— SnackBarHost๋ฅผ ์—ฐ๊ฒฐ !!!
                    snackbarHost = {
                        SnackbarHost(hostState = snackbarHostState)
                    }

                    ) { innerPadding ->


                LazyColumn(
                    modifier = Modifier
                        .background(color = Color.LightGray)
                        .fillMaxWidth()
                        .padding(innerPadding),

                    // ์•„์ดํ…œ ์ „์ฒด๋ฅผ ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•  ๊ฒฝ์šฐ ์ด ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
                    // Add this property if you want to center all items.
                    //horizontalAlignment = Alignment.CenterHorizontally

                    contentPadding = PaddingValues(16.dp), //
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    item{
                        Text(
                            text = "LazyColum Text",
                            // ์•„์ดํ…œ ์ „์ฒด ๋„ˆ๋น„๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ํ•จ / Make the item take up the entire width
                            modifier = Modifier.fillMaxWidth(),
                            // ๊ฐ€์šด๋ฐ ์ •๋ ฌ / center aligned
                            textAlign = TextAlign.Center
                        )
                    }
                    item{
                        // LazyColumn๋‚ด์—์„œ ๋ฒ„ํŠผ์„ ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๋ ค๋ฉด ๋ฐ•์Šค์•ˆ์— ๋„ฃ์–ด์„œ ์ •๋ ฌ
                        // To center the button within the LazyColumn, place it in a box and align it.
                        Box(
                            // Box๊ฐ€ ์•„์ดํ…œ ๋„ˆ๋น„ ์ „์ฒด๋ฅผ ์ฐจ์ง€ํ•˜๊ฒŒ ํ•จ.
                            // Make the box take up the entire width of the item.
                            modifier = Modifier.fillMaxWidth(),

                            // ๋ฐ•์Šค๋‚ด ์ปจํ…์ธ  ์ค‘์•™์ •๋ ฌ.
                            // Center the content within the box.
                            contentAlignment = Alignment.Center
                        ) {
                            // Snackbar XML style
                            SnackbarButton1(msg = "ํ† ์ŠคํŠธ ๋ฉ”์„ธ์ง€1/Toast Message1", btnText = "ํด๋ฆญํ•˜์„ธ์š”1/Please Click1")
                        }
                    }
                    item{
                        // Snackbar compose๋ฐฉ์‹ / Snackbar compose style
                        // SnackbarButton2๋ฅผ ํ˜ธ์ถœ์‹œ SnackBar ๋กœ์ง์„ ์ฝœ๋ฐฑ์œผ๋กœ ์ „๋‹ฌ
                        // When calling SnackbarButton2, pass the SnackBar logic as a callback.
                        SnackbarButton2(
                            msg = "์Šค๋‚ต๋ฐ” ๋ฉ”์„ธ์ง€/Snackbar Message",
                            btnText = "์Šค๋‚ต๋ฐ” ๋„์šฐ๊ธฐ/Show SnackBar",
                            acLabel = "๋‹ซ๊ธฐ/Close",
                            onButtonClick = {
                                // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, MainAcitivity์˜ Scope์™€ State๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ SnackBar ์‹คํ–‰
                                scope.launch {
                                    snackbarHostState.showSnackbar(
                                        message = "์Šค๋‚ต๋ฐ” ๋ฉ”์„ธ์ง€/Snackbar Message",
                                        actionLabel = "๋‹ซ๊ธฐ/Close",
                                        duration = SnackbarDuration.Short
                                    )
                                }
                            }
                        )
                    }
                    items(100){
                        index ->
                        Text(" text : $index")
                    }
                    item{
                        Text("--End--")
                    }
                }


//                    Greeting(
//                        name = "Android",
//                        modifier = Modifier.padding(innerPadding)
//                    )


                }
            }
        }
    }
}

@Composable
fun SnackbarButton1(modifier: Modifier = Modifier,msg:String,btnText:String){

    // ์ปจํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ
    val context = LocalContext.current
    //Text("ButtonEvent")

    // Button ์ปดํฌ์ €๋ธ” ์ถ”๊ฐ€
    Button(onClick ={
        println("$msg")
        Toast.makeText(context,"$msg",Toast.LENGTH_LONG).show()
    },
        modifier = modifier

    ) {
        // ๋ฒ„ํŠผ ๋‚ด๋ถ€์— ํ‘œ์‹œํ•  ๋‚ด์šฉ ์ •์˜
        Text("$btnText")
    }
}

@Composable
fun SnackbarButton2(
    modifier: Modifier = Modifier,
    msg: String,
    btnText: String,
    acLabel: String,
    onButtonClick: () -> Unit // ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ถ”๊ฐ€ / Add Callback function to handle click events
) {
    
    Button(
        onClick = onButtonClick, // ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์‹คํ–‰ / Run callback function
        modifier = Modifier.fillMaxWidth()
    ) {
        Text(btnText)
    }
}
//@Composable
//fun Greeting(name: String, modifier: Modifier = Modifier) {
//    Text(
//        text = "Hello $name!",
//        modifier = modifier
//    )
//}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    LazyColumnExampleTheme {
        //Greeting("Android")
    }
}

2.์Šคํฌ๋ฆฐ์ƒท/Screen Shot

Leave a Reply

Your email address will not be published. Required fields are marked *