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

Android DataBinding:XML设置默认可见性解决RecyclerView布局闪烁

Fixing RecyclerView Item Layout Flash with DataBinding Include

Hey there, let's tackle this layout flash issue and resolve the default parameter error you're facing!

Why the Default Parameter Error Happens

The error 'View' is incompatible with attribute android:visibility (attr) enum [gone=2, invisible=1, visible=0] pops up because DataBinding's default parameter expects a literal value that matches the attribute's type, not a reference to a class constant like View.GONE. At compile time, the default value is resolved directly, and it can't interpret View.GONE as the integer it represents under the hood.

Solution 1: Use the Integer Literal for GONE in Default

Since View.GONE maps to the integer value 2, you can pass this literal directly as the default:

<include 
    android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE, default=2}" 
    bind:viewmodel="@{viewmodel}" 
    layout="@layout/full_station_layout"/>

This tells DataBinding to use GONE as the initial visibility before the viewmodel's expandable value is bound, stopping the flash from happening.

Solution 2: Set Initial Visibility Directly in XML (More Intuitive)

An even cleaner fix is to set the include tag's initial visibility to gone directly, without relying on the default parameter. DataBinding will overwrite this value once the viewmodel is bound, but the initial state stays hidden to avoid the flash:

<include 
    android:visibility="@{viewmodel.expandable ? View.VISIBLE : View.GONE}" 
    android:visibility="gone"
    bind:viewmodel="@{viewmodel}" 
    layout="@layout/full_station_layout"/>

This works because the XML's default attribute value takes effect immediately when the layout is inflated, before DataBinding finishes its binding process.

Why the Initial Flash Occurred

The flash happens because when RecyclerView inflates the item layout, DataBinding hasn't finished binding the viewmodel.expandable value yet. By default, the include layout is visible until the binding updates it. Setting an initial hidden state (either via the default literal or direct XML attribute) ensures the layout stays hidden until the viewmodel's value is applied.

Quick note: The default parameter hasn't been removed from DataBinding—it's just strict about type matching. Using the correct literal value or the direct XML attribute will fix both the error and the flash issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:20:39