当前位置: 首页 > news >正文

kotlin,数字滚动选择

用国内的通义灵码和codegeex都没有弄出来,最后只得用墙外的chatgpt才弄出一个满意的。kotlin真的有点难,好在有AI,让学习没这难了。

package com.example.mynumset

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehavior
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mynumset.ui.theme.MyNumSetTheme
import kotlinx.coroutines.launch
import androidx.compose.ui.graphics.Color

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyNumSetTheme {
                NumberPickerDemo()
            }
        }
    }
}

@Composable
fun NumberPicker(
    modifier: Modifier = Modifier,
    initialValue: Int = 0,
    range: IntRange = 0..100,
    onValueChange: (Int) -> Unit
) {
    val itemHeight = 50.dp
    val listState = rememberLazyListState(initialFirstVisibleItemIndex = initialValue)
    val coroutineScope = rememberCoroutineScope()
    val itemHeightPx = with(LocalDensity.current) { itemHeight.toPx() }

    // 实时计算当前选中项索引
    val selectedIndex by remember {
        derivedStateOf {
            val offset = listState.firstVisibleItemScrollOffset
            val index = listState.firstVisibleItemIndex
            if (offset > itemHeightPx / 2) index + 1 else index
        }
    }

    // 当滚动停止时,吸附到中间项并回调
    LaunchedEffect(listState.isScrollInProgress) {
        if (!listState.isScrollInProgress) {
            coroutineScope.launch {
                listState.animateScrollToItem(selectedIndex)
            }
            onValueChange(range.first + selectedIndex)
        }
    }

    LazyColumn(
        state = listState,
        modifier = modifier
            .height(itemHeight * 3)
            .width(100.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        contentPadding = PaddingValues(vertical = itemHeight),
        flingBehavior = rememberSnapFlingBehavior(lazyListState = listState)
    ) {
        items(range.count()) { index ->
            val value = range.first + index
            val isSelected = selectedIndex == index

            Text(
                text = value.toString(),
                fontSize = if (isSelected) 32.sp else 20.sp,
                fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,
                color = if (isSelected) Color.Black else Color.Gray,
                modifier = Modifier
                    .height(itemHeight)
                    .fillMaxWidth(),
                textAlign = TextAlign.Center
            )
        }
    }
}

@Composable
fun NumberPickerDemo() {
    var selectedNumber by remember { mutableStateOf(50) }

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        NumberPicker(
            initialValue = selectedNumber,
            onValueChange = { selectedNumber = it }
        )

        Spacer(modifier = Modifier.height(16.dp))

        Text("当前选择: $selectedNumber", fontSize = 24.sp)
    }
}

相关文章:

  • <工具 Claude Desktop>配置 Whois MCP 用于 whois 查询
  • Matlab:三维绘图
  • 【PostgreSQL内核学习:深入理解 PostgreSQL 中的 tuplesort_performsort 函数】
  • 微软2025年AI技术深度解析:从多模态大模型到企业级代理服务
  • Go语言的可选链
  • c++中的auto关键字
  • 项目中集成ECharts图表(通过定时任务SpringTask统计每天的订单金额)
  • 下一代AI App架构:前端生成,后端消失
  • 15.QT窗口:主窗口、浮动窗口、对话框
  • 云原生 | 配置TektonArgo cd完成简单的CI/CD
  • 【Gorm】模型定义
  • http协议版本的区别 -- 2和3
  • 20250406学会提问
  • 《C++探幽:STL(String类的使用)》
  • 【安全】Java幂等性校验解决重复点击(6种实现方式)
  • JavaScript | 流程控制-分支与循环
  • TypeScript 类型系统详解
  • 第2.7节 iOS生成报告时排除指定的类
  • JavaScript零基础入门速通(完整)
  • Java学习——day22(Java反射基础入门)
  • 做网站与平台的区别/seo常见优化技术
  • wordpress充值卡/开封网站优化公司
  • win10 中国建设银行网站/做销售怎么和客户聊天
  • 直播网站怎么做/河南百度关键词优化排名软件
  • 创新的成都 网站建设/设计网站接单
  • 网站开发设计步骤/目前最新的营销方式有哪些