Untitled
unknown
plain_text
19 days ago
4.5 kB
4
Indexable
Never
package com.example.selectimage import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.BackHandler import androidx.activity.compose.LocalOnBackPressedDispatcherOwner import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.GridItemSpan import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.material3.Checkbox import androidx.compose.material3.CheckboxColors import androidx.compose.material3.CheckboxDefaults import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.example.selectimage.ui.theme.SelectImageTheme import kotlinx.coroutines.launch class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { SelectImageTheme { Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> TimeLine() } } } for (i in 0..50) listSelected.add(false) } } @Composable fun TimeLine() { LazyVerticalGrid( columns = GridCells.FixedSize(115.dp), horizontalArrangement = Arrangement.spacedBy(5.dp), verticalArrangement = Arrangement.spacedBy(5.dp) ) { items(count = 50, span = { GridItemSpan(1) }) { Item(it, onClick = { if (SelectedMode == true) { listSelected[it] = !listSelected[it] } else listSelected[it] = false }, onLongClick = { if (SelectedMode == false) { SelectedMode = true } if (listSelected[it] == false) listSelected[it] = true } ) } } val onBackPressedDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher var backPressHandled by remember { mutableStateOf(false) } val coroutineScope = rememberCoroutineScope() BackHandler(enabled = !backPressHandled) { coroutineScope.launch { if(SelectedMode) SelectedMode = false else onBackPressedDispatcher?.onBackPressed() } } } val listSelected = mutableListOf<Boolean>() var SelectedMode = false @OptIn(ExperimentalFoundationApi::class) @Composable fun Item(idx: Int, onClick: () -> Unit, onLongClick: () -> Unit) { Box(modifier = Modifier .background(color = Color.LightGray) .size(115.dp) .combinedClickable( onClick = { onClick() }, onLongClick = { onLongClick() } ), contentAlignment = Alignment.Center ) { if (SelectedMode) { Checkbox( checked = listSelected[idx], modifier = Modifier.align(Alignment.TopStart), onCheckedChange = { listSelected[idx] = it }, colors = CheckboxDefaults.colors( uncheckedColor = Color.Black, checkedColor = Color.White, checkmarkColor = Color.Blue ) ) } Text(text = idx.toString(), color = Color.Black) } } //bam nut quay lai
Leave a Comment