代码之家  ›  专栏  ›  技术社区  ›  Nick Wills

Kotlin Android-无法使@BindingAdapter正常工作

  •  1
  • Nick Wills  · 技术社区  · 7 年前

    我在尝试使用@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}"/>
    

    遇到错误:

    发现数据绑定错误。

    ****/数据绑定错误****消息:找不到参数类型为int on的属性focusableColor的setter 安卓支持v7。小部件。CardView。 文件:D:–of thelian–of thelian。\app\src\main\res\layout\current\u task\u布局。xml

    2 回复  |  直到 7 年前
        1
  •  0
  •   pt2121    7 年前

    您是否尝试过将绑定移出同伴对象? 您应该将其放在kotlin文件中,并使其成为顶级函数。例如:

    Bindings.kt

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

    并将绑定xml放在应用程序命名空间中

    此外,请参见 Kotlin custom attribute databinding

    编辑:完整示例

    主要活动。千吨级

    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\u 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>
    
        2
  •  0
  •   Chirag Prajapati    5 年前

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

    ImageLoader。千吨级

    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\u主页。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>
    

    家庭活动。千吨级

    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()
        }
    }
    

    注意:不要忘记在应用程序级别构建的顶部添加以下行。格拉德尔

    apply plugin: 'kotlin-kapt'