如何为图像实现 DiffUtil

Posted

技术标签:

【中文标题】如何为图像实现 DiffUtil【英文标题】:How to implement DiffUtil for images 【发布时间】:2022-01-04 18:15:20 【问题描述】:

您好,我听说这个库 DiffUtil 提高了回收器视图的性能,因此我的回收器视图包含图像,我最好实现它,但我不知道如何

注意:我没有包含片段代码,所以问题没有得到 很长,但如果您想更多地参考代码,请告诉我,我会的 更新问题

PostAdapter_Home.kt

class PostAdapter_Home(var mcontext: Context, var mUploads: MutableList<Upload?>?) :
    RecyclerView.Adapter<PostAdapter_Home.PostViewHolder>() 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder 
        val view: View
        view = LayoutInflater.from(mcontext)
            .inflate(R.layout.post_item_container_home_ex, parent, false)
        return PostViewHolder(view)
    

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) 
        val shimmer = ColorHighlightBuilder()
            .setBaseColor(Color.parseColor("#F3F3F3"))
            .setBaseAlpha(1f)
            .setHighlightColor(Color.parseColor("#E7E7E7"))
            .setHighlightAlpha(1f)
            .setDropoff(50f)
            .build()
        val shimmerDrawable = ShimmerDrawable()
        shimmerDrawable.setShimmer(shimmer)
        val uploadCurrent = mUploads?.get(position)
        Glide.with(mcontext)
            .load(uploadCurrent?.getmImageUrl())
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
            .placeholder(shimmerDrawable)
            .fitCenter()
            .into(holder.imageView)

    


    override fun getItemCount(): Int 
        return mUploads?.size!!
    

    //    public long getId() 
    //        return this.id;
    //    
    //
    //    @Override
    //    public long getItemId(int position) 
    //        return mUploads.get(position).Id;
    //    
    fun setUploads(uploads: MutableList<Upload?>?) 
        mUploads = uploads
    
    

    class PostViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 
        val imageView: ShapeableImageView

        init 
            imageView = itemView.findViewById(R.id.imagePostHome)
        
    

上传.kt

package com.example.myappnotfinal.AdaptersAndMore

import com.google.firebase.database.Exclude

class Upload 
    internal var mImageUrl: String? = null
    private var mKey: String? = null

    constructor() 
    constructor(imageUrl: String?) 
        mImageUrl = imageUrl
    

    fun getmImageUrl(): String? 
        return mImageUrl
    

    fun setmImageUrl(mImageUrl: String?) 
        this.mImageUrl = mImageUrl
    

    @Exclude
    fun getmKey(): String? 
        return mKey
    

    @Exclude
    fun setmKey(Key: String?) 
        mKey = Key
    


【问题讨论】:

【参考方案1】:

这是 DiffUtil.ItemCallback 的实现。

Diff Util 回调类

class PostDiffUtil : DiffUtil.ItemCallback<Upload>() 
    override fun areItemsTheSame(oldItem: Upload, newItem: Upload): Boolean = oldItem == newItem
    override fun areContentsTheSame(oldItem: Upload, newItem: Upload): Boolean =
        oldItem.toString() == newItem.toString()

上传数据类

data class Upload(
    var mImageUrl: String? = null,
    @Exclude @set:Exclude @get:Exclude
    var mKey: String? = null
)

适配器类。

class PostAdapter_Home : RecyclerView.Adapter<PostAdapter_Home.PostViewHolder>() 
    private val postDiffUtil = PostDiffUtil()
    val postListDiffer = AsyncListDiffer(this, postDiffUtil)
    override fun getItemCount(): Int = postListDiffer.currentList.size
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder =
        PostViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.post_item_container_home_ex, parent, false))

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) 
        holder.bindView(postListDiffer.currentList[position])
    

    class PostViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 
        val imageView: ShapeableImageView = itemView.findViewById(R.id.imagePostHome)
        fun bindView(upload: Upload) 
            val shimmer = ColorHighlightBuilder()
                .setBaseColor(Color.parseColor("#F3F3F3"))
                .setBaseAlpha(1f)
                .setHighlightColor(Color.parseColor("#E7E7E7"))
                .setHighlightAlpha(1f)
                .setDropoff(50f)
                .build()
            val shimmerDrawable = ShimmerDrawable()
            shimmerDrawable.setShimmer(shimmer)
            Glide.with(imageView.context)
                .load(upload?.getmImageUrl())
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .placeholder(shimmerDrawable)
                .fitCenter()
                .into(imageView)
        
    

更新列表。在 Activity 或 Fragment 中引用适配器,然后使用您的值提交列表

val postAdapter = PostAdapter_Home()
postAdapter.postListDiffer.submitList(< Your List Here >)

【讨论】:

recycler view 太麻烦了,非常感谢 嘿兄弟,我问了一个与分页相关的问题***.com/questions/70096562/…你能帮我解决一下吗 如果你能帮助我进行分页,我将不胜感激,我从过去 4 个月开始就一直坚持下去 @VasantRaval 我会看看的。

以上是关于如何为图像实现 DiffUtil的主要内容,如果未能解决你的问题,请参考以下文章

如何为列表视图创建自定义光标适配器以用于图像和文本?

iPhone/iOS:如何为 Scrollview 的子视图实现拖放?

如何为 xib 文件的 2 个图像之间的 2 个标签实现约束

如何为图像中的每个水平线数据实现一维 FFT 滤波器

你如何为 iPhone 实现横向启动屏幕?

iOS - 如何为头像创建分层图像