Android 8/9中RecyclerView ViewHolder内CalendarView设Gone仍占空间问题
问题描述
- 我的RecyclerView包含2个及以上ViewHolder:
- 第一个ViewHolder仅为顶部标题横幅;
- 第二个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
相关产品推荐
相关产品推荐

