Kotlin Android - 无法让@BindingAdapter工作

时间:2018-04-14 07:06:23

标签: android binding kotlin

尝试使用@BindingAdapter时,我一直收到绑定错误。尝试3天,关注此主题的大量在线文章,但仍然会收到以下错误。

@BindingAdapter("focusableColor")
fun setFocusableColor(v:CardView,  color:Int) {
    println("hello")
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          val binding:ActivityMainBinding  = 
              DataBindingUtil.setContentView(this,R.layout.activity_main)
          etc...
    }



In current_task_layout.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
            <variable name="task" type="com.edenhan.simplytask.Task">
            </variable>
    </data>

<android.support.v7.widget.CardView
  android:id="@+id/card_view">
  .....
  focusableColor="@{1}"/>

遇到错误:

发现数据绑定错误。

  

**** /数据绑定错误**** msg:找不到参数类型为int的属性'focusableColor'的setter   android.support.v7.widget.CardView。   文件:d:\ ...... \应用\ SRC \主\ RES \布局\ current_task_layout.xml

2 个答案:

答案 0 :(得分:0)

您是否尝试将绑定移出配套对象? 你应该将它放在一个kotlin文件中,并使其成为顶级函数。例如:

Bindings.kt

@BindingAdapter("focusableColor")
fun setFocusableColor(v:CardView,  color:Int) {..}

将绑定xml放入app namespace

另请参阅Kotlin custom attribute databinding

编辑:完整示例

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

@BindingAdapter("focusableColor")
fun setColor(card: CardView, @ColorInt color: Int) {
    // or whatever
    card.setBackgroundColor(color)
}

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"
    >
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <android.support.v7.widget.CardView
            android:id="@+id/card"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:focusableColor="@{1}"/>
    </android.support.constraint.ConstraintLayout>
</layout>

答案 1 :(得分:0)

下面使用BindingAdapter和Kotlin为加载图像定义完整的代码

ImageLoader.kt

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableField

class ImageLoader {

    val imageResource = ObservableField(R.drawable.ic_launcher_background)

    companion object {
        @JvmStatic
        @BindingAdapter("android:src")
        fun setImage(imageView: ImageView, imageRes: Int) {
            imageView.setImageResource(imageRes)
        }
    }
}

activity_home.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">
    <data>
        <variable name="imageLoader" type="com.sample.testdemo.ImageLoader"/>
    </data>
    <RelativeLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <ImageView android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:contentDescription="@string/app_name"
                   android:src="@{imageLoader.imageResource}"
                   android:layout_centerInParent="true"/>
    </RelativeLayout>
</layout>

HomeActivity.kt

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.sample.testdemo.databinding.ActivityHomeBinding

class HomeActivity : AppCompatActivity() {

    lateinit var binding: ActivityHomeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this,  R.layout.activity_home)

        binding.imageLoader = ImageLoader()
    }
}

注意:不要忘记在应用程序级别build.gradle的顶部添加以下行

apply plugin: 'kotlin-kapt'