0

I'm updating my list when the user reaches the end of the scroll to get a pagination effect.

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            if (!recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN)) 
 {  pageNumber++
                    loadIntroProducts(pageNumber)
                }
            }
        }

Here is where i'm getting the data

onProductSearchSuccess(data: Data){
productList.addAll(data.products)
productSimpleAdapter.updateProductItems(productList)

In adapter I'm setting the new data with DiffUtil

fun updateProductItems(products: ArrayList<Products>) {
    val diffCallback = ProductDiffCallback(this.productList, products)
    val diffResult = DiffUtil.calculateDiff(diffCallback)
    // this.productList.clear()
    //  this.productList.addAll(products)
    setNewData(products)
    diffResult.dispatchUpdatesTo(this)
}

private fun setNewData(newProducts: ArrayList<Products>) {
    this.productList = newProducts
}

override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
    holder.bindView(holder.adapterPosition)
}

override fun onBindViewHolder(holder: ProductViewHolder, position: Int, payloads: MutableList<Any>) {
    if (payloads.isNullOrEmpty())
        super.onBindViewHolder(holder, position, payloads)
    else {
        holder.bindView(holder.adapterPosition, payloads)
    }
}

ProductDiffCallback

class ProductDiffCallback() :
DiffUtil.Callback() {
lateinit var oldProductList: ArrayList<Products>
lateinit var newProductList: ArrayList<Products>

constructor(oldProductList: ArrayList<Products>, newProductList: ArrayList<Products>) : this() {
    this.oldProductList = oldProductList
    this.newProductList = newProductList
}

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
    return oldProductList[oldItemPosition].productId == newProductList[newItemPosition].productId
}

override fun getOldListSize(): Int {
    return oldProductList.size
}

override fun getNewListSize(): Int {
    return newProductList.size
}

override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
    return oldProductList[oldItemPosition].productId == newProductList[newItemPosition].productId && oldProductList[oldItemPosition].numberOfCartItem == newProductList[newItemPosition].numberOfCartItem
}

override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Bundle? {
    val oldItem = oldProductList[oldItemPosition]
    val newItem = oldProductList[newItemPosition]
    val bundle = Bundle()

    if (oldItem.numberOfCartItem != newItem.numberOfCartItem)
        bundle.putInt("numberOfCartItems", newItem.numberOfCartItem)
    return bundle
    }
    }

When i debugged, i got the new list has all the items but not triggering the adapter class

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77

1 Answers1

0

Put below code inside your activity then try it by just calling with in activity.:

fun updateProductItems(products: ArrayList<Products>) {
    ArrayList<Products> oldList = productSimpleAdapter.getData();
    val diffCallback = ProductDiffCallback(oldList, products)
    val diffResult = DiffUtil.calculateDiff(diffCallback)
    setNewData(products)
    diffResult.dispatchUpdatesTo(productSimpleAdapter)
}

It might work.