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

Android 8/9中RecyclerView ViewHolder内CalendarView设Gone仍占空间问题

问题描述
  • 我的RecyclerView包含2个及以上ViewHolder:
    1. 第一个ViewHolder仅为顶部标题横幅;
    2. 第二个ViewHolder为预订组件,包含3个下拉/详情菜单(内含CalendarView)。

问题:CalendarView高度较大,当它部分移出屏幕时,设置其为GONE后仍会占用布局空间,且该问题仅出现在**Android 8 & 9**系统中。

初始状态截图:
初始状态截图

收起CalendarView后问题截图:
收起后仍占空间截图

相关代码

ViewHolder项的ClickListener:

val clickListener = View.OnClickListener {
    // irrelevant code //
    for (type in AllOptionsEnum.values()) {
            // Expand Selected and Collapse Others
        val detailView = this.root.findViewById<ViewGroup>(type.detailViewGroupId)
        val labelView = this.root.findViewById<TextView>(type.labelViewId)
        val checkBox = this.root.findViewById<CheckBox>(type.checkBoxId)

        if (detailView.visibility == View.VISIBLE) {
            // hide detailed view
            detailView.setVisible(show = false)
            checkBox.isChecked = false
        } else {
            // show
            detailView.setVisible(show = (type.labelViewGroupId == it.id))
            checkBox.isChecked = show = (type.labelViewGroupId == it.id)
            
        }
            
    // irrelevant code //
    }
}

编辑1:扩展函数

fun View.setVisible(show: Boolean = true, invisible: Boolean = false) {
    if (show) this.visibility = View.VISIBLE
    else this.visibility = if (invisible) View.INVISIBLE else View.GONE
}

编辑2:

我尝试过另一种方案:完全移除RecyclerView,改用NestedScrollView重构整个页面,但问题依然存在。

编辑3:

我还尝试了以下代码,结果相同:

// show detailView
if(type==Enums.StayWithUsWidgetType.TYPE_CALENDAR){
    // show detailview and children
    this.calendarDetailView.children.forEach {
        it.visible()
    }
}


// Hide detailView and children 
if(type==Enums.StayWithUsWidgetType.TYPE_CALENDAR){
    // hide all
    this.calendarDetailView.children.forEach {
        it.gone()
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 21:27:48