getItemCount или onBindViewHolder не вызывается с помощью DiffUtil - PullRequest
0 голосов
/ 04 июля 2019

Я обновляю свой список, когда пользователь достигает конца прокрутки, чтобы получить эффект нумерации страниц.

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

Здесь я получаю данные

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

В адаптере я устанавливаю новые данные с помощью 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
    }
    }

При отладке я получил новый список, содержащий все элементы, но не вызывающий класс адаптера

1 Ответ

0 голосов
/ 04 июля 2019

Поместите ниже код в вашей деятельности, затем попробуйте его, просто позвонив с в деятельности .:

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)
}

Это может сработать.

...