数据绑定:无法将列表的大小从xml传递到适配器

时间:2017-12-01 15:04:42

标签: android android-databinding

在我的app / build.gradle中:

dataBinding {
        enabled = true
}

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

带数据绑定的xml布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="item"
            type="com.myproject.android.customer.api.model.Item" />
        </data>
        <android.support.constraint.ConstraintLayout
            android:id="@+id/bottomContainer"
            android:layout_width="0dp"
            android:layout_height="wrap_content">        
            <TextView
                android:id="@+id/subTitleTextView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"
                android:textColor="#a3a3a3"
                android:textSize="9sp"
                app:subtitle="@{item.offers}" />
        </android.support.constraint.ConstraintLayout>
    </android.support.constraint.ConstraintLayout>
</layout>

适配器的代码:

   @BindingAdapter("subtitle")
    public static void setSubtitle(TextView view, List<Offer> offers) {
        int offersCount = offers.size();
        String subtitle = ((offersCount == 1) ? String.format(view.getContext().getString(R.string.offer), offersCount)
                : String.format(view.getContext().getString(R.string.offers), offersCount));
        view.setText(subtitle);
}

行。它的工作正常。

但我想只将列表的大小传递给方法setSubtitle()。我不需要传递整个列表。

所以我的改变:

xml文件中的

<TextView
    android:id="@+id/subTitleTextView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:subtitle="@{item.offers.size()}" />

并在适配器中:

@BindingAdapter("subtitle")
    public static void setSubtitle(TextView view, int offersCount) {
        String subtitle = ((offersCount == 1) ? String.format(view.getContext().getString(R.string.offer), offersCount)
                : String.format(view.getContext().getString(R.string.offers), offersCount));
        view.setText(subtitle);
}

但我得到错误:

:app:kaptDebugKotlin
e: error: cannot generate view binders com.sun.tools.javac.code.Symbol$CompletionFailure: class file for io.reactivex.Flowable not found   
e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
    at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:138)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:154)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)
Caused by: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing

0 个答案:

没有答案