android - 尝试将数据绑定(bind)提供的上下文变量与 BaseObservable 一起使用时出错

标签 android android-databinding

根据 this commentAndroid Data Binding tutorial我应该有一个 context数据绑定(bind)时可用的变量:

A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.

我正在使用扩展 BaseObservable 的类设置我的数据,但无论我如何尝试引入 context我的变量 @Bindable方法我得到编译时错误:

java.lang.RuntimeException: failure, see logs for details. @Bindable associated with method must follow JavaBeans convention getStyledName(android.content.Context)

据我所知,我遵循 JavaBeans 约定。我已将错误追踪到 this line in BrUtil但这也没有给我太多帮助。

我试过更改我的方法和参数名称,我试过添加 <variable>对于 context和一个 <import>对于 android.content.Context , 但这些都没有任何区别。我的User.java下面有两个@Bindable方法,裸getName()工作正常,但尝试使用 context 的那个变量没有,所以我认为我的其余数据绑定(bind)设置也不是问题。

相关代码:

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable name="user" type="com.kasra.androidsandbox.User" />
    </data>

    <LinearLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        <TextView
            android:id="@+id/main_textview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@{user.getStyledName(context)}" />
    </LinearLayout>
</layout>

User.java :

public class User extends BaseObservable {
    private String name;

    public User(String name) {
        this.name = name;
    }

    @Bindable
    public String getName() {
        return this.name;
    }

    @Bindable
    public CharSequence getStyledName(Context ctx) {
        int color = ctx.getResources().getColor(R.color.branded_pink);

        Spannable textSpan = new SpannableString(name);
        textSpan.setSpan(new ForegroundColorSpan(color), 0, textSpan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return textSpan;
    }

}

MainActivity.java :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    User user = new User("Kasra");
    binding.setUser(user);

    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    setSupportActionBar(toolbar);
}

最佳答案

我不知道这是一个错误还是有意为之的行为,但我自己观察到了这种行为,所以你没有疯。

解决方法是在您的用户中创建一个未标记为 @Bindable 的方法——需要上下文的方法。此方法不受 JavaBeans 命名约定的约束。

重要:为了确保它得到适当更新,它应该将任何可能更改的@Bindable 字段作为输入。

用户.java

public class User extends BaseObservable {
    private String name;

    public User(String name) {
        this.name = name;
    }

    @Bindable
    public String getName() {
        return this.name;
    }

    public CharSequence getStyledName(Context ctx, String name) {
        // construct styled name here
    }
}

在你的布局中:

<TextView
        android:id="@+id/main_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@{user.getStyledName(context, user.name)}" />

关于android - 尝试将数据绑定(bind)提供的上下文变量与 BaseObservable 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087082/

相关文章:

android - Jetpack Compose 与一堆卡片的时序问题

javascript - 如何在电话号码输入中插入连字符?

android - 无法解析 ':app@releaseUnitTest/compileClasspath' : Could not resolve com. android.support :appcompat-v7:27. 1.1 的依赖项

android - Kotlin Observer lambda 语法?

android - Linux ptrace() 读取整个内存页

android - 从 android java.lang.NoClassDefFoundError : javax/annotation/Generated 中的命令行获取错误

android - 具有 View 模型的 DialogFragment 无法使用数据绑定(bind)

android - 通过数据绑定(bind)创建和使用 AAR

android - 如何使用 DataBindingUtil 在 Fragment 中绑定(bind) View ?

android - 在 android 8 上使用 android 数据绑定(bind)时应用程序崩溃