You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中remember与mutableStateOf无法更新Text问题

Jetpack Compose状态更新但UI不刷新问题

我在开发一款基于Jetpack Compose的应用,功能是从外部API获取数据并展示图表。目前数据能正常获取,但图表不会随新数据重组。为定位问题,我编写了一个最简的Counter可组合函数进行测试:点击按钮时,count值在Logcat和Toast中能正常增长,但Text组件始终显示0。这段测试代码在其他项目中可以正常运行。

测试代码

@Composable
fun Counter() {
    val count = remember { mutableStateOf(0) }
    val context = LocalContext.current
    Column(
        Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Button(
            onClick = {
                count.value++
                Log.d("f", "Value: ${count.value}")
                Toast.makeText(context, "Clicked button ${count.value}", Toast.LENGTH_SHORT).show()
            }
        ) {
            Text("Click me")
        }
        Text(text = "Current count: ${count.value}")
    }
}

项目build.gradle配置(compose_ui_version为'1.3.1')

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.example.airmockapiapp'
compileSdk 33

defaultConfig {
    applicationId "com.example.airmockapiapp"
    minSdk 30
    targetSdk 33
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables {
        useSupportLibrary true
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}
buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion compose_ui_version
}
packagingOptions {
    resources {
        excludes += '/META-INF/{AL2.0,LGPL2.1}'
    }
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.3"
implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.3"

// Compose viewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"

// Compose navigation
implementation "androidx.navigation:navigation-compose:2.5.3"

//Jetpack Compose Charts from tehras
//implementation "com.github.tehras:charts:beta-01"

// Williamchart xml charts library
//implementation 'com.diogobernardino:williamchart:3.3.0'

// Composable-Graphs from jaikeerthick github
implementation 'com.github.jaikeerthick:Composable-Graphs:v1.1'
}

相关截图说明

  • 模拟器截图:界面上Text组件始终显示"Current count: 0",按钮点击后UI无更新
  • Logcat截图:每次点击按钮后,日志中输出的count值依次递增(如Value: 1、Value: 2等)

内容的提问来源于stack exchange,提问作者jakgrab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 08:45:32