You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin三层嵌套ExpandableListView子项重复、二级子项不显示问题咨询

问题1:父列表子视图全部相同、groupPosition始终为0修复

该问题核心是父适配器的子项计数逻辑错误:你需要为每个Leg(父列表组项)仅创建1个子项承载对应的内层ExpandableListView,而非每个Step创建1个重复的内层列表。
修改JourneyLegExpandableListAdapter的getChildrenCount方法:

override fun getChildrenCount(groupPosition: Int): Int {
    // 每个Leg仅对应1个内层ExpandableListView子项
    return 1
}

问题2:子列表子视图不显示修复

该问题由两个原因导致:布局高度配置错误、嵌套滚动组件高度未正确测量,按以下步骤修改:

  1. 修正所有嵌套层级布局的高度配置
    所有ExpandableListView及其父容器的android:layout_height属性改为wrap_content,禁止在可滚动容器内使用match_parent:
  • leg_detail_group_view根节点MaterialCardView高度改为wrap_content
  • steps_nested_list_view根LinearLayout和内部ExpandableListView高度改为wrap_content
  • steps_nested_group_view根节点MaterialCardView高度改为wrap_content
  • step_detail_view根节点MaterialCardView和内部RelativeLayout高度改为wrap_content
  1. 新增动态测量内层ExpandableListView高度的工具方法
    避免滚动冲突导致内层列表高度被压缩为0:
private fun setNestedExpandableHeight(listView: ExpandableListView) {
    val adapter = listView.adapter ?: return
    var totalHeight = 0
    val widthSpec = View.MeasureSpec.makeMeasureSpec(listView.width, View.MeasureSpec.EXACTLY)
    // 累加所有组项高度
    for (i in 0 until adapter.groupCount) {
        val groupView = adapter.getGroupView(i, listView.isGroupExpanded(i), null, listView)
        groupView.measure(widthSpec, View.MeasureSpec.UNSPECIFIED)
        totalHeight += groupView.measuredHeight
        // 累加展开状态的子项高度
        if (listView.isGroupExpanded(i)) {
            for (j in 0 until adapter.getChildrenCount(i)) {
                val childView = adapter.getChildView(i, j, j == adapter.getChildrenCount(i)-1, null, listView)
                childView.measure(widthSpec, View.MeasureSpec.UNSPECIFIED)
                totalHeight += childView.measuredHeight
            }
        }
        // 累加分割线高度
        totalHeight += listView.dividerHeight * (adapter.getChildrenCount(i) + 1)
    }
    // 重新设置列表高度
    val params = listView.layoutParams
    params.height = totalHeight + listView.paddingTop + listView.paddingBottom
    listView.layoutParams = params
    listView.requestLayout()
}
  1. 内层列表设置适配器后触发高度计算,同步监听展开/折叠状态更新高度
    修改JourneyLegExpandableListAdapter的getChildView方法:
override fun getChildView(
    groupPosition: Int,
    childPosition: Int,
    isLastChild: Boolean,
    convertView: View?,
    parent: ViewGroup?
): View {
    val v: View
    if (convertView == null) {
        v = LayoutInflater.from(con).inflate(R.layout.steps_nested_list_view, parent, false)
    } else {
        v = convertView
    }
    val childList: ExpandableListView = v.findViewById(R.id.steps_nested_expandable_list_view)
    val adapter = StepsExpandableListAdapter(con, parentList.get(groupPosition).steps)
    childList.setAdapter(adapter)
    // 新增部分:计算高度+监听状态更新
    setNestedExpandableHeight(childList)
    childList.setOnGroupExpandListener { setNestedExpandableHeight(childList) }
    childList.setOnGroupCollapseListener { setNestedExpandableHeight(childList) }
    return v
}
  1. 修复空指针隐患
    修改StepsExpandableListAdapter的getChildrenCount方法,避免subSteps为空时崩溃:
override fun getChildrenCount(groupPosition: Int): Int {
    return parentList[groupPosition].subSteps?.size ?: 0
}

内容的提问来源于stack exchange,提问作者codeblogger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 23:15:03