简体   繁体   English

Android-数据绑定-带有表达式的字符串属性

[英]Android - Data Binding - String Attribute with Expression

I have been struggling with an almost unsolvable issue with Android lately. 最近,我一直在努力解决Android上几乎无法解决的问题。 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: 这是attrs.xml条目:

<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". 属性binaryText显示为“购买”。 However, the Attributes binaryValue and binaryDirection are NEVER even called! 但是,甚至从不调用属性binaryValue和binaryDirection!

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. 问题在于某些未知原因未调用某些测井点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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