如果将List<Interface>与LazyColumn一起使用,则会重新组合所有项,而如果我使用List<Implementation>,则它会智能地跳过重新组合.

例如,如果我在可组合重组中使用示例1 MutableList,则对列表中的所有加法操作的项进行重组,但在示例2中,它会智能地跳过.WHY?

例1:

    val selectionState: MutableList<ViewItemScratch> = remember {
      mutableStateListOf()
    }

示例2:

    val selectionState: MutableList<TitleViewItemScratch> = remember {
      mutableStateListOf()
    }

接口:

interface ViewItemScratch {
  val identifier: Int
  val layoutId: Int

  @SuppressLint("NotConstructor")
  @Composable
  fun ViewItem(
    lazyItemScope: LazyItemScope? = null,
    rowScope: RowScope? = null,
    columnScope: ColumnScope? = null
  ) {
  }
}

实施类:

data class TitleViewItemScratch(
  val index: Int,
  val text: StringResolver,
  val modifier: Modifier = Modifier,
  override val identifier: Int = text.hashCode()
) : ViewItemScratch {
  override val layoutId: Int
    get() = R.id.compose_view_title_view_item

  @Composable
  override fun ViewItem(
    lazyItemScope: LazyItemScope?, rowScope: RowScope?, columnScope: ColumnScope?
  ) {
    Text(
      modifier = modifier
        .fillMaxWidth()
        .wrapContentHeight()
        .padding(horizontal = 16.dp),
      text = text.resolve(),
    )
  }
}

可组合的:

 @Composable
  private fun WithViewItemScratch() {
    val selectionState: MutableList<ViewItemScratch> = remember {
      mutableStateListOf()
    }
    val onButtonClick = remember {
      {
        selectionState.add(
          TitleViewItemScratch(
            index = selectionState.size + 1,
            text = UIString("Hello world #${selectionState.size + 1}"),
            identifier = selectionState.size + 1,
          )
        )
      }
    }
    Column {
      Button(onClick = {
        onButtonClick.invoke()
      }) {
        Text(text = "Add")
      }
      LazyColumn {
        items(
          items = selectionState,
          key = { viewItem: ViewItemScratch -> viewItem.identifier }
        ) { item ->
          item.ViewItem()
        }
      }
    }
  }

编译报道(S):

restartable skippable fun ViewItem(
  unused stable lazyItemScope: LazyItemScope? = @static null
  unused stable rowScope: RowScope? = @static null
  unused stable columnScope: ColumnScope? = @static null
  unused <this>: ViewItemScratch
)
stable class TitleViewItemScratch {
  stable val index: Int
  stable val text: StringResolver
  stable val color: CompassColor
  stable val typography: CompassTypography
  stable val modifier: Modifier
  stable val identifier: Int
  <runtime stability> = Stable
}

版面判断器重新组合:

With List of 接口: enter image description here

带实现列表/具体类:

enter image description here

推荐答案

提取Jetpack Compose编译器报告/指标显示接口的 solidity 缺失.因为接口没有逻辑,但讲述了它所带来的抽象.因此,如果界面没有通过应用@Stable@Immutable来显式保证组成的 solidity ,则仅将其视为不稳定.

默认情况下,该实现被认为是稳定的,因为它是一个数据类(带有equals()hashCode()实现),所有属性都是val(不可变).

Android相关问答推荐

Jetpack Compose Scaffold—content不在TopAppBar下面开始'

如何将结果从viewModelScope传递到活动

尽管我们不再使用GCM SDK,但应用程序已被标记为使用GCM SDK

安卓喷气背包组成倒计时动画

为什么我有多个Player实例?

如何在Jetpack Compose android中使用导航

设置文本 colored颜色 动画时如何减少重新组合?

使用lazyColumn迁移paging3的旧代码

使用 Gadle kotlin 为多模块 Android 代码库设置 jacoco

Android 构建失败:找不到 flexbox2.0.1.aar

Android Jetpack Compose全宽度抽屉优化

ArrayList 上的 Android intent.getParcelableArrayListExtra 引发 Nullpointer 异常

如何在 android compose 中将具有渐变边缘的透明圆圈绘制到阴影覆盖层中?

以下代码如何在 Android 上运行

Android 设备断开连接后发送的 BLE 蓝牙数据

未找到 com.android.tools.build:gradle:7.4.0 的匹配变体

在 Jetpack Compose 中使用 ViewModel 实现 startActivity 的最佳实践

如何在 kotlin 的 android room DB 中设置一对多关系

在使用 Retrofit 和 Room 时,我是否需要提及协程调度程序?

即使我在单选按钮上明确设置了选中状态,RecyclerView 中的单选按钮也会随机取消选中