【问题标题】:load image by picasso in recyclerview by databinding kotlin通过数据绑定kotlin在recyclerview中由毕加索加载图像
【发布时间】:2021-06-05 16:33:59
【问题描述】:

我想使用databindingrecycler view 中通过Picasso 加载图像,但是当我不知道如何在我的recycler view data class 中使用@BindingAdapter 时。我知道它在 java 中是如何工作的,但在 kotlin 中我不知道如何使用它。这是我的适配器的数据类:

data class Users(val name: String,
             val email: String,
             val img_link: String)

这是我的适配器:

class AdapterRecPart02(private val context: Context, private val ls: List<Users>) :
RecyclerView.Adapter<AdapterRecPart02.ItemAdapter>() {


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemAdapter {

    val inflater = LayoutInflater.from(context)
    val binding: LayoutRecPart02Binding =
        DataBindingUtil.inflate(inflater, R.layout.layout_rec_part02, parent, false)
    return ItemAdapter(binding)
}

override fun onBindViewHolder(holder: ItemAdapter, position: Int) {

    holder.bind(ls[position])

}

override fun getItemCount(): Int = ls.size


inner class ItemAdapter(private val bindingAdapter: LayoutRecPart02Binding) :
    RecyclerView.ViewHolder(bindingAdapter.root) {

    fun bind(user: Users) {

        bindingAdapter.users = user
        bindingAdapter.executePendingBindings()

    }


}

}

这是我的适配器的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<data>

    <variable
        name="users"
        type="ir.xs.mvvm.model.Users" />

</data>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img_user"
        android:layout_width="120dp"
        android:layout_height="120dp" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/img_user"
        android:text="@{users.name, default = name}"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_name"
        android:layout_marginTop="16dp"
        android:layout_toRightOf="@+id/img_user"
        android:text="@{users.email, default = email}"
        android:textSize="16sp" />

</RelativeLayout>

【问题讨论】:

    标签: android android-studio kotlin mvvm data-binding


    【解决方案1】:

    这是一个简单的绑定适配器,它将使用 Picasso 加载图像:

    @BindingAdapter("urlImage")
    fun bindUrlImage(view: ImageView, imageUrl: String?) {
        if (imageUrl != null) {
            Picasso.get()
                .load(imageUrl)
                .fit()
                .centerCrop()
                .into(view)
        } else {
            view.setImageBitmap(null)
        }
    }
    

    以下是在数据绑定布局中使用此绑定适配器的方法:

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:binding="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
        
        
        <data>
    
            <variable
                name="imageUrl"
                type="String" />
    
    
        </data>
        
        
        //.......
        
        
        <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/myImageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    binding:urlImage="@{ imageUrl }" />
                    
    </layout>
    

    对于问题中的特定情况,您将如何绑定图像 url:

     <ImageView
        android:id="@+id/img_user"
        android:layout_width="120dp"
        android:layout_height="120dp"
        binding:urlImage="@{ users.img_link }" />
    

    【讨论】:

    • 我应该把它放在哪里:有趣的 bindUrlImage ?而且我也在尝试使用 mvvm 。这适合 mvvm 吗?
    • @poya4220 通常,您会将绑定适配器放在没有任何类的单独文件中,类似于仅具有扩展功能的文件。 BindingAdapters.kt 之类的东西或者更具体的东西,如果你以某种方式组织它们。至于正确使用 MVVM,只需确保您的源数据由 ViewModel 提供的LiveData 提供。
    • 非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2021-11-04
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    相关资源
    最近更新 更多