【问题标题】:Android dataBinding - @BindingAdapter custom app namespace being ignoredAndroid dataBinding - @BindingAdapter 自定义应用命名空间被忽略
【发布时间】:2016-05-06 02:12:48
【问题描述】:

我在 android 中创建了一个自定义 bindingAdapter,当我传入颜色时,我希望颜色发生变化,这实际上是为了让我进行测试以确保它正常工作。这是代码: 这是我的数据绑定视图模型:

public class User {
public ObservableInt visible;

public User(int visible) {
    this.visible=new ObservableInt(visible);
}


@BindingAdapter({"app:bindColor"}) //notice the bindColor custom attribute
public static void setTextColor(TextView view,String color) {

    if("green".equals(color))
    view.setTextColor(Color.parseColor("#63f421"));

}

}

现在在绑定到此模型的我的 xml 文件中,我希望传入一种颜色,以便 setTextColor 方法可以使用它:

 <?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">


    <data class="MainActivityBinder">
        <variable name="user" type="com.example.android.floatingactionbuttonbasic.User"/>
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_one"
            android:text="my first textview"/>

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_two"
            android:text="my second textview"
            android:visibility="@{user.visible}"
            app:bindColor="@{'green'}" //see im passing in the green string here
            android:textColor="@android:color/holo_green_dark"/>

    </LinearLayout>
</layout>

我在运行时收到错误:

Error:(27, 65) error: package com.example.android.floatingactionbuttonbasic.databinding does not exist
Warning:Application namespace for attribute app:bindColor will be ignored.
Error:(24, 33) Identifiers must have user defined types from the XML file. een is missing it 
Error:Execution failed for task ':Application:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.

如果我取出 bindingAdapter 的东西,它可以与其他数据绑定的东西完美配合。它只是这个自定义绑定不起作用。我的项目名为floatingactionbuttonbasic btw。

【问题讨论】:

标签: android data-binding


【解决方案1】:

我能够让它工作。问题是我如何传递字符串。它周围应该有``。

app:bindColor="@{`green`}"

你也可以这样做:

app:bindColor='@{"green"}'

但似乎不允许的是这种表示法:

app:bindColor="@{'green'}"

如果有兴趣,我写了一个blog about it 来帮助其他人。

【讨论】:

  • 请将您的答案标记为已回答,这样问题就不会再出现在未回答的标签上
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
  • 2014-07-21
  • 2010-09-10
  • 2011-12-05
  • 2015-02-18
  • 1970-01-01
相关资源
最近更新 更多