Skip to content Skip to sidebar Skip to footer

Why Is This Custom View Attribute Ignored?

I am trying to define an attribute for any view using the Data Binding Library, as explained in this Android Developers post. To do so, the post says one first needs a layout with

Solution 1:

I found the solution to my problem, I was not creating the Binding correctly:

Following the first steps of the Guide, I used DataBindingUtil.setContentView, but for ListView items you need to use ItemBinding.inflate in the Adapter's ViewHolder:

@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ViewDataBindingbinding= DataBindingUtil.inflate(
            LayoutInflater.from(parent.getContext()),
                    R.layout.item, parent, false);
    returnnewViewHolder(binding.getRoot());
}

Solution 2:

From what I can tell in that first link, the data tag is present. It was probably omitted in the G+ post because its boilerplate. In fact, in the docs it says

Data-binding layout files are slightly different and start with a root tag of layout followed by a data element and a view root element.

Anyways, I think you might be missing some required sugar in the layout file. Can you try:

app:attribute='@{"name"}`

Maybe its required for the binding to occur. I mean right now I am aiming blind until I actually test this. But from that post I see app:imageUrl='@{"http://example.com/image.jpg"}'.

Solution 3:

It should be @BindingAdapter("bind:attribute") instead of @BindingAdapter("app:attribute")

and try with this, it might work.

app:attribute="@{`name`}"

Post a Comment for "Why Is This Custom View Attribute Ignored?"