如何在jetpack compose中使用静态值创建可扩展的列表视图

拉曼吉特

我用静态值创建了 Kotlin 代码:

我想知道如何使用 jetpack compose 创建相同的内容?我不知道

代码:

       class TestApp : AppCompatActivity() {
    
        var listAdapter: ExpandableListAdapter? = null
        var expListView: ExpandableListView? = null
        var listDataHeader: MutableList<String>? = null
        var listDataChild: HashMap<String, List<String>>? = null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            expListView = findViewById<View>(R.id.lvExp) as ExpandableListView
            prepareListData()
            listAdapter = ExpandableListAdapter(this, listDataHeader, listDataChild)
            expListView!!.setAdapter(listAdapter)
    
        }
        private fun prepareListData() {
        listDataHeader = ArrayList()
        listDataChild = HashMap()
        listDataHeader?.add(getString(R.string.q_1))
        val on_0: MutableList<String> = ArrayList()
        on_0.add(getString(R.string.faq_d_0))
        listDataChild!![listDataHeader!![0]] = on_0
}
    }
菲利普·杜霍夫

您可以使用LazyColumnplus 可变状态列表来存储折叠状态:

@Composable
fun CollapsableLazyColumn(
    sections: List<CollapsableSection>,
    modifier: Modifier = Modifier
) {
    val collapsedState = remember(sections) { sections.map { true }.toMutableStateList() }
    LazyColumn(modifier) {
        sections.forEachIndexed { i, dataItem ->
            val collapsed = collapsedState[i]
            item(key = "header_$i") {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .clickable {
                            collapsedState[i] = !collapsed
                        }
                ) {
                    Icon(
                        Icons.Default.run {
                            if (collapsed)
                                KeyboardArrowDown
                            else
                                KeyboardArrowUp
                        },
                        contentDescription = "",
                        tint = Color.LightGray,
                    )
                    Text(
                        dataItem.title,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier
                            .padding(vertical = 10.dp)
                            .weight(1f)
                    )
                }
                Divider()
            }
            if (!collapsed) {
                items(dataItem.rows) { row ->
                    Row {
                        Spacer(modifier = Modifier.size(MaterialIconDimension.dp))
                        Text(
                            row,
                            modifier = Modifier
                                .padding(vertical = 10.dp)
                        )
                    }
                    Divider()
                }
            }
        }
    }
}

data class CollapsableSection(val title: String, val rows: List<String>)

const val MaterialIconDimension = 24f

用法:

CollapsableLazyColumn(
    sections = listOf(
        CollapsableSection(
            title = "Fruits A",
            rows = listOf("Apple", "Apricots", "Avocado")
        ),
        CollapsableSection(
            title = "Fruits B",
            rows = listOf("Banana", "Blackberries", "Blueberries")
        ),
        CollapsableSection(
            title = "Fruits C",
            rows = listOf("Cherimoya", "Cantaloupe", "Cherries", "Clementine")
        ),
    ),
)

结果:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Jetpack Compose中使用CameraView?

如何在 Jetpack compose 中使用 Viewmodel

如何在 Jetpack Compose 中使用 SharedFlow

如何在Compose Jetpack中创建回收者视图?

如何在jetpack中使用LayoutAlign撰写

如何在 Jetpack Compose 中使用寻呼机布局?

如何在jetpack compose中使用Android PdfViewer

如何在 Jetpack Compose 的 MutableState 中使用条件

如何在 jetpack compose 中使用复选框控件?

如何在Jetpack Compose中获取onTouchEvent?

如何在Jetpack Compose中处理导航?

如何在jetpack Compose中将按钮居中?

如何在jetpack compose上回收webview?

如何在 Jetpack Compose 中使底片覆盖整个屏幕

如何在 Jetpack Compose 中创建表格?

如何在 Jetpack Compose 中返回值

如何在 Jetpack Compose Navigation 中正确使用 Viewmodel

如何在抽屉式布局中使用普通Listview实现可扩展列表视图?

如何在可扩展列表视图中使用外部字体?(Android Studio)

如何在jQuery Mobile中的可单击列表项中使用按钮创建列表视图

如何在 Jetpack Compose 中使用 Coil 顯示自定義可組合佔位符?

如何在 Jetpack Compose 的 TextField 中正确设置高度?

如何在 Jetpack Compose 中获取此 UI?

如何在 Jetpack Compose Desktop 中存储数据?

如何在 Jetpack Compose 中显示横幅广告?

如何在 Jetpack Compose 中围绕画布移动矩形?

如何在 Jetpack Compose 中覆盖系统 onbackpress()

如何在Jetpack Compose中实现此按钮动画?

如何在Jetpack Compose中将TextUnit转换为Dp?