Android Compose keeps recomposing

1.9k Views Asked by At

I am writing a LazyColumn, items are LazyRows.

But the PasserItem keeps recomposing when gently swipe, I see log keep pringing and never stop.

I come here for help.

  1. Why this happen, any state wrong?
  2. I find tow ways stop it. remove items in MemoList2, or remove AnimatedVisibility in PasserItem2. Why these ways help?
  3. Is my pattern implementing get-in animation with AnimatedVisibility proper?(I want anim run only once)
data class Passer(val username: String?, val memo: Array<String>)

MainActivity#onCreate

val live = MutableLiveData<Array<Passer>>()
live.value = Array(100) {
    Passer("$it", arrayOf("$it", "$it"))
}
setContent {
    PasserTheme {
        Home2(live)
    }
}
private const val TAG = "Home"

@Composable
fun Home2(passersLive: LiveData<Array<Passer>>) {
    val passers = passersLive.observeAsState(initial = arrayOf())
    Scaffold {
        Box(
            modifier = Modifier.fillMaxSize(),
            content = { PasserList2(passers = passers.value) }
        )
    }
}

@OptIn(ExperimentalAnimationApi::class)
@Composable
private fun PasserList2(passers: Array<Passer>) {
    Log.d(TAG, "PasserList")
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        Log.d(TAG, "LazyColumn ${passers.size}")
        items(passers.size) { index ->
            PasserItem2(passer = passers[index])
            if (index != passers.size - 1) {
                Spacer(modifier = Modifier.height(32.dp))
            }
        }
    }
}

@OptIn(ExperimentalAnimationApi::class)
@Composable
private fun PasserItem2(passer: Passer) {
    Log.d(TAG, "PasserItem ${passer.username}")
    var visibility by remember { mutableStateOf(false) }
    LaunchedEffect(key1 = visibility) {
        Log.d(TAG, "PasserItem LaunchedEffect")
        visibility = true
    }
    Log.d(TAG, "PasserItem visible $visibility")
    AnimatedVisibility(visible = visibility) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(MaterialTheme.colors.onBackground.copy(alpha = 0.1F))
                .padding(16.dp)
        ) {
            MemoList2(passer.memo)
        }
    }
}

@Composable
private fun MemoList2(memo: Array<String>) {
    Log.d(TAG, "MemoList")
    LazyRow(modifier = Modifier.fillMaxWidth()) {
        items(memo.size) { }
    }
}
buildscript {
    ext {
        compose_version = '1.2.0-alpha07'
        my_dependency = [:]
        my_dependency.gradle = "com.android.tools.build:gradle:7.0.4"
        my_dependency.kotlin_gradle = "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

        my_dependency.core_ktx = "androidx.core:core-ktx:1.7.0"
        my_dependency.compose_ui_ui = "androidx.compose.ui:ui:1.2.0-alpha07"
        my_dependency.compose_material = "androidx.compose.material:material:1.1.1"
        my_dependency.compose_ui_tooling_preview = "androidx.compose.ui:ui-tooling-preview:1.1.1"
        my_dependency.compose_activity = "androidx.activity:activity-compose:1.4.0"
        my_dependency.compose_runtime_livedata = "androidx.compose.runtime:runtime-livedata:1.1.1"
        my_dependency.compose_ui_tooling = "androidx.compose.ui:ui-tooling:1.1.1"

        versions = [:]
        versions.versionCode = 1
        versions.versionName = "1.0"
        versions.compileSdkVersion = 31
        versions.buildToolsVersion = '33.0.1'
        versions.minSdk = 30
        versions.targetSdk = 31
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "$my_dependency.gradle"
        classpath "$my_dependency.kotlin_gradle"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "ycr.pass"
        minSdk versions.minSdk
        targetSdk versions.targetSdk
        versionCode versions.versionCode
        versionName versions.versionName
        vectorDrawables {
            useSupportLibrary true
        }
    }

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

dependencies {

    implementation my_dependency.core_ktx
    implementation my_dependency.compose_ui_ui
    implementation my_dependency.compose_material
    implementation my_dependency.compose_ui_tooling_preview
    implementation my_dependency.compose_activity
    implementation my_dependency.compose_runtime_livedata
    debugImplementation my_dependency.compose_ui_tooling
}
0

There are 0 best solutions below