Android Data Binder bug: androidbinder

Source: Internet
Author: User

Android Data Binder bug: androidbinder

Following the official tutorial to learn how to use data binding, the function is indeed very powerful. This is a huge step for Android to become MVVM and a small step for Native development to move closer to the Web.

One of the binding methods is to directly use resource data, for example:

android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"

Official Tutorial:

The layout file of the full version is as follows:

<layout xmlns:android="http://schemas.android.com/apk/res/android">    <data class="ResourceBinding">        <variable name="large" type="boolean" />    </data>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView            android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"            android:background="@android:color/black"            android:textColor="@android:color/white"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/hello_world" />    </LinearLayout></layout>

largePaddingAndsmallPaddingAll are defined indimens.xmlFile.

<dimen name="largePadding">20dp</dimen><dimen name="smallPadding">5dp</dimen>

Bind the large variable in the Java code and assign the valueture.

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);   ResourceBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_resource);    binding.setLarge(true);} 

Theoretically, this should be fine, but an error occurs during the Run project. The error message is as follows:

cannot find the setter for attribute 'android:padding' on android.widget.TextView with parameter type float.

It seems like DataBinder@dimen/largePaddingResolvedfloatType, you can try type conversion:

android:padding="@{large? (int)@dimen/largePadding : (int)@dimen/smallPadding}"

After compilation is passed, the running result is correct. It should be a bug in DataBinder. The original resource binding has such advanced usage, and type conversion can be performed directly-(int)@dimen/largePadding.

It feels amazing. You must read the source code to understand how DataBinder works.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.