简体   繁体   中英

Does Android data-binding support raw resources?

I am currently trying to pass the resource id of a raw resource to a special View using the new Android data-binding mechanism. When I try something like this

...
    app:bufferedSvg="@{ViewModel.headerCollapsed ? @raw/header_expand : @raw/header_collapse}"
...

I get an error <expr> expected, got '@' . The raw resources exist and cannot be turned into another kind of resource as they contain SVG data.

Is this a bug of Android data binding or is this intended behaviour?

At least I found a workaround meanwhile. Import the R class:

<data>
    <import type="com.yourdomain.R"/>
    <variable name="ViewModel" type="com.yourdomain.ViewModel"/>
</data>

...

<com.yourdomain.yourview

    ...

    app:bufferedSvg="@{ViewModel.yourchoice ? R.raw.raw_resource1 : R.raw.raw_resource2}"

    ...

/>

Hope this helps someone who runs into the same problem. Would still like to know whether the @-notation for raw resources is planned to be supported or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM