Android数据绑定找不到属性的setter

时间:2018-03-15 14:22:23

标签: android data-binding kotlin android-databinding

我试图在Kotlin中使用android数据绑定库,并希望为RecyclerView提供自定义属性。为此,我创建了一个包含以下代码的文件:

@BindingAdapter("config")
fun configRecyclerView(recyclerView: RecyclerView, config: RecyclerViewConfig) {
    config.apply(recyclerView)
}

RecyclerViewConfig的位置:

class RecyclerViewConfig private constructor(
    val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>,
    val layoutManager: RecyclerView.LayoutManager,
    val isNestedScrollingEnabled: Boolean,
    val hasFixedSize: Boolean
) {

    fun apply(recyclerView: RecyclerView) {
        recyclerView.layoutManager = layoutManager
        recyclerView.isNestedScrollingEnabled = isNestedScrollingEnabled
        recyclerView.setHasFixedSize(hasFixedSize)
        recyclerView.adapter = adapter
    }

    class Builder {

        private lateinit var adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>
        private lateinit var layoutManager: RecyclerView.LayoutManager
        private var isNestedScrollingEnabled = true
        private var hasFixedSize = true

        fun adapter(adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>): Builder {
            this.adapter = adapter
            return this
        }

        fun layoutManager(layoutManager: RecyclerView.LayoutManager): Builder {
            this.layoutManager = layoutManager
            return this
        }

        fun nestedScrollingEnabled(isNestedScrollingEnabled: Boolean): Builder {
            this.isNestedScrollingEnabled = isNestedScrollingEnabled
            return this
        }

        fun hasFixedSize(hasFixedSize: Boolean): Builder {
            this.hasFixedSize = hasFixedSize
            return this
        }

        fun build() = RecyclerViewConfig(adapter, layoutManager, isNestedScrollingEnabled, hasFixedSize)
    }

}

此外,我自己的ViewModel内有RecyclerViewConfig

class AdvertPageViewModel(val context: Context) : RecyclerViewModel {

    private val adapter: RealtyAdvertAdapter
    private val listener: OnPageContentsClickListener
    private val config: RecyclerViewConfig

    init {
        listener = createListener()
        config = createConfig()
        adapter = RealtyAdvertAdapter(createAdvert().toPageItems(), listener)
    }

    override fun getConfig() = config

    private fun createConfig() =
            RecyclerViewConfig.Builder()
                    .adapter(adapter)
                    .layoutManager(LinearLayoutManager(context))
                    .build()


    private fun createListener() = object: OnPageContentsClickListener {

        override fun onCallButtonClick(phone: String) {
            Log.i(TAG, "onCallButtonClick")
        }

        override fun onWriteButtonClick(phone: String) {
            Log.i(TAG, "onWriteButtonClick")
        }

    }
}

我这样创建:

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    viewModel = AdvertPageViewModel(context)
    if (view != null) {
        val binding = FragmentAdvertPageBinding.bind(view)
        binding.viewModel = viewModel
    }
}

Fragment的布局:

<?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="viewModel"
      type="ru.am.realty.ui.fragments.advertpage.AdvertPageViewModel"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:config="@{viewModel.getConfig()}"/>

    </LinearLayout>
</layout>

问题在于,当我尝试构建应用程序时,发生了错误:

Error:(20, 27) Cannot find the setter for attribute 'app:config' with 
parameter type ru.am.realty.ui.base.viewmodel.RecyclerViewConfig on 
android.support.v7.widget.RecyclerView. 

所以,我一直在寻找任何解决方案,但没有成功。我不知道,我的代码有什么问题

更新:那里的gradle文件

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'

    // Rx
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'

    //di
    implementation 'com.google.dagger:dagger:2.15'
    kapt 'com.google.dagger:dagger-compiler:2.15'

    kapt 'com.android.databinding:compiler:3.0.1'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

0 个答案:

没有答案