kotlin databindingadapter在XML中不起作用

时间:2019-08-02 07:35:01

标签: kotlin android-databinding

我想将imageview与数据绑定适配器结合起来。我在Google搜寻有关我问题的一周。请帮助我。.问题是databindingadapter在我的XML中不起作用。

为简单起见,其他视图也很容易识别,例如textView If。如果我在ImageView中输入变量名称,则颜色应该会改变,但不会改变

我更改了我的绑定适配器名称,包括app:和不包括app: 但它没有用。 我将对象更改为同伴或移动了其他类文件。但这没用。

DataClass

class Comment {

var commentId = ""
var postId = ""
var writerId = ""
var message = ""
var writeTime: Any = Any()
var bgUri = ""

object imageBindingAdapter {

    val comment = Comment()
    @JvmStatic
    @BindingAdapter("image")
    fun bindImage(imageView: ImageView, bgUri: String) {

        comment.bgUri = bgUri

        Picasso.get()
            .load(Uri.parse(comment.bgUri))
            .fit()
            .centerCrop()
            .into(imageView)

      }

    }
}

XML

<layout>

<data>

    <variable name="itemComment" type="com.sns.data.Comment"/>

</data>

<androidx.cardview.widget.CardView 
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:layout_width="wrap_content"
                                   android:layout_height="wrap_content"
                                   android:clickable="true"
                                   android:foreground="? 
android:attr/selectableItemBackground"

app:cardBackgroundColor="@android:color/white"
                                   app:cardCornerRadius="4dp"
                                   app:cardUseCompatPadding="true">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="120dp"
            android:layout_height="120dp">

        <ImageView
                android:id="@+id/img_background"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="centerCrop"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                image="@{itemComment.bgUri}"/>

        <TextView
                android:id="@+id/tv_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:background="#aa000000"
                android:gravity="center"
                android:maxLines="3"
                android:textColor="@android:color/white"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:text="@{itemComment.message}"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

0 个答案:

没有答案