【问题标题】:How can I launch recomposition when a specified Flow changed in Jetpack Compose?当 Jetpack Compose 中指定的 Flow 发生变化时,如何启动重组?
【发布时间】:2022-11-26 12:16:21
【问题描述】:

我知道 Column(){...} 将在更改 b1 或 b2 时重新组合。

如果我希望Column(){...}只在b2改变时重新组合,而b1改变时Column(){...}不重新组合,我该怎么办?

@Composable
fun ScreenDetail(
    mViewMode: SoundViewModel
) {
      val b1=mViewMode.a1.collectAsState(initial = 0)
      val b2=mViewMode.a2.collectAsState(initial = 0)
      
      Column() {
          Text(" ${b1.value}   ${b2.value}")

          Text(Calendar.getInstance().time.toSeconds())
      }
}

fun Date.toSeconds():String{
    return SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US).format(this)
}

class SoundViewModel(): ViewModel() {
    var i = 0
    val a1: Flow<Int> = flow {
        while (true) {
            emit(i++)
            delay(1000)
        }
    }

    val a2: Flow<Int> = flow {
        while (true) {
            emit(i)
            delay(2000)
        }
    }
}

【问题讨论】:

    标签: kotlin android-jetpack-compose compose-recomposition


    【解决方案1】:

    你需要创造范围如果你需要有范围的重组。我所说的范围是指创建一个不像 Column、Row 或 Box 那样内联的 Composable。你可以在这个link查看答案和文章。

    Compose 重组最接近的作用域/函数状态被阅读。如果你读

       Text(" ${b1.value}   ${b2.value}")
    

    当任何这些状态发生变化时,您的专栏将被重新组合。但是如上所述,即使您阅读了其中的任何内容,它们也应该有所更改,因为 Column 不会创建范围

    @Composable
    fun ScreenDetail2(
        mViewMode: SoundViewModel
    ) {
        val b1=mViewMode.a1.collectAsState(initial = 0)
        val b2=mViewMode.a2.collectAsState(initial = 0)
    
        Column(modifier= Modifier.background(getRandomColor()).fillMaxWidth()) {
            Text("${b1.value}")
    
        }
    
        Column(modifier= Modifier.background(getRandomColor()).fillMaxWidth()) {
           Text("${b2.value}")
       }
    }
    

    但是如果你创建一个函数,比如

    @Composable
    private fun MyColumn(counter:Int){
        Column(modifier= Modifier.background(getRandomColor()).fillMaxWidth()) {
            Text("$counter")
        }
    }
    

    您将拥有您阅读的每个值的范围

    @Composable
    fun ScreenDetail3(
        mViewMode: SoundViewModel
    ) {
        val b1=mViewMode.a1.collectAsState(initial = 0)
        val b2=mViewMode.a2.collectAsState(initial = 0)
    
        MyColumn(b1.value)
        MyColumn(b2.value)
    }
    

    正如您在 gif 中看到的那样,当 b1 或 b2 更改时,ScreenDetail2 会重组每个列,但 ScreenDetail3 只会重组各自的功能范围。我将 b1 的延迟时间更改为 300,将 b2 的延迟时间更改为 2000,以便在视觉上易于观察重组。

    上面的两列来自ScreenDetail2,下面的两列来自ScreenDetail3

    重组可以在很多方面成为观察者,我加 2

    类 Ref(变量值:Int)

    // Note the inline function below which ensures that this function is essentially
    // copied at the call site to ensure that its logging only recompositions from the
    // original call site.
    @Composable
    inline fun LogCompositions(msg: String) {
        val ref = remember { Ref(0) }
        SideEffect { ref.value++ }
        println("$msg, recomposition: ${ref.value}")
    }
    

    或改变颜色

    fun getRandomColor() =  Color(
        red = Random.nextInt(256),
        green = Random.nextInt(256),
        blue = Random.nextInt(256),
        alpha = 255
    )
    

    【讨论】:

    • 谢谢!当我使用 ScreenDetail3 时,我总是能得到相同的结果吗?我认为 Android Studio 优化了重组来影响这个结果。
    • 还有更多,我在官方文档中找不到You need to create scopes if you need to have scoped recompositions. 。
    • 当您创建范围时,您肯定会得到相同的结果。我建议你查看这个article,以及我发布的答案,以及this one。而且,是的,官方文件中没有提到它。您可以使用 AS dolphin 布局进行观察者重组,我正在添加我的答案两个函数以在视觉上或在日志中观察。
    • 谢谢!我根据您对问题stackoverflow.com/questions/72696157/… 的想法更新了我的代码 我仍然对 Jetpack Compose 何时启动重组以及什么是重组感到困惑?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多