【问题标题】:How to make an endless list in Jetpack Compose如何在 Jetpack Compose 中制作无穷无尽的列表
【发布时间】:2020-06-30 14:15:43
【问题描述】:

当制作一个 xml 无限列表时需要创建RecyclerView 并添加RecyclerViewOnScrollListener。如何在 Jetpack Compose 中进行操作?

【问题讨论】:

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


    【解决方案1】:

    您可以为此使用androidx.ui.foundation.AdapterList。 它只会组合和布置当前可见的项目。

    @Composable
    fun CustomerListView(list: List<Customer>) {
    
        AdapterList(data = list) { customer->
    
           Text("name:${customer.name}") 
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以像 here 解释的那样使用 LazyColumnFor:

      @Composable
      fun LazyColumnForDemo() {
          LazyColumnFor(items = listOf(
                  "A", "B", "C", "D"
          ) + ((0..100).map { it.toString() }),
                  modifier = Modifier,
                  itemContent = { item ->
                      Log.d("COMPOSE", "This get rendered $item")
                      when (item) {
                          "A" -> {
                              Text(text = item, style = TextStyle(fontSize = 80.sp))
                          }
                          "B" -> {
                              Button(onClick = {}) {
                                  Text(text = item, style = TextStyle(fontSize = 80.sp))
                              }
                          }
                          "C" -> {
                              //Do Nothing
                          }
                          "D" -> {
                              Text(text = item)
                          }
                          else -> {
                              Text(text = item, style = TextStyle(fontSize = 80.sp))
                          }
                      }
                  })
      }
      

      谷歌在这里解释:https://youtu.be/SMOhl9RK0BA23:18

      【讨论】:

        猜你喜欢
        • 2014-10-09
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        • 2022-08-17
        • 1970-01-01
        相关资源
        最近更新 更多