简体   繁体   中英

Android - Data Binding - String Attribute with Expression

I have been struggling with an almost unsolvable issue with Android lately. I'm trying to build a custom control that would take one of its value dynamically from a function. But with no avail. Here is the attrs.xml entry:

<declare-styleable name="BinaryOptionButtonAttrs">
<attr name="binaryText" format="string"/>
<attr name="binaryValue" format="string"/>
<attr name="binaryDirection" format="enum">
    <enum name="up" value="0x00"/>
    <enum name="down" value="0x01"/>
</attr></declare-styleable>

Here is the custom control view code:

public class BinaryOptionButton extends LinearLayout {
private Context mContext;
private String mText;
private String mValue;
private BINARY_OPTION mDirection;

public BinaryOptionButton(Context context) {
    super(context);
}

public BinaryOptionButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    initAttrArray(attrs);
    initBinaryOptionButton();
}

public String getBinaryText() {
    return mText;
}

public void setBinaryText(String mText) {
    this.mText = mText;    }

public String getBinaryValue() {
    return mValue;
}

public void setBinaryValue(String mValue) {
    this.mValue = mValue;        Log.d(this.getClass().getPackage().getName(),
            "BinaryOptionButton.mValue=" + mValue);
}

public BINARY_OPTION getBinaryDirection() {
    return mDirection;
}

public void setBinaryDirection(BINARY_OPTION mDirection) {
    this.mDirection = mDirection;
}

private void initBinaryOptionButton() {

}

private void initAttrArray(AttributeSet attrs) {
    TypedArray attrsArray   = mContext.obtainStyledAttributes(attrs,
            R.styleable.BinaryOptionButtonAttrs, 0, 0);

    mText = attrsArray.getString(
            R.styleable.BinaryOptionButtonAttrs_binaryText
    );

    mValue = attrsArray.getString(
            R.styleable.BinaryOptionButtonAttrs_binaryValue
    );

    mDirection = BINARY_OPTION.parseInt(attrsArray.getInt(
            R.styleable.BinaryOptionButtonAttrs_binaryDirection,
            BINARY_OPTION.UP.value()
    ));
    Log.d(this.getClass().getPackage().getName(),
            "mText=" + mText);

    Log.d(this.getClass().getPackage().getName(),
            "mValue=" + mValue);

    Log.d(this.getClass().getPackage().getName(),
            "mDirection=" + mDirection);

    attrsArray.recycle();
} }

Here is a sample usage of the Custom View:

<layout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
    <import type="Utils"/>
    <variable
        name="HANDLER"
        type="ViewListener"/>
    <variable name="dataCount" type="int"/>
    <variable name="dataVector" type="java.util.List"/>
</data><RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent">
<com.package.BinaryOptionButton
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="100dp"
bind:binaryText='@{"BUY"}'
bind:binaryValue="@{Integer.toString(dataCount)}"
bind:binaryDirection="@{Utils.binary(dataVector)}"    
android:onClick='@{() -> HANDLER.onAction("data")}'/></RelativeLayout</layout>

The Attribute binaryText shows "BUY". However, the Attributes binaryValue and binaryDirection are NEVER even called!

What I'm doing wrong here?

Thanks

The Code ABove is Correct. The Attributes were called correctly. The problem was some of the Logging points weren't called for an unknown reason.

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