0

Let's say I have an adapter for a RecyclerView that looks like this:

class ContentAdapter(
    private val data: List<ContentModel>,
    private val itemSelectedListener: OnItemSelected<ContentModel>,
    private val activityUIFocusListener: ActivityUIFocusListener? ): RecyclerView.Adapter<ContentViewHolder>() {

    /**
     * For paging signalling purposes
     */
    var pagingOffsetFromItemCount = 1
    var pagingListener : (() -> Unit)? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_main_content, parent, false)
        return ContentViewHolder(view, itemSelectedListener, activityUIFocusListener)
    }

    override fun onBindViewHolder(holder: ContentViewHolder, position: Int) {
        holder.setUpModelToView(data[position])
        if(position == itemCount - pagingOffsetFromItemCount) {
            pagingListener?.invoke()
        }
    }

    override fun getItemCount(): Int = data.size
}

The data variable in the constructor for this adapter references a MutableList<ContentModel> managed by another class, the BaseMediaListPagingPresenter:

interface PagingAdapterInterface {
    fun initAdapter(contentModels : List<ContentModel>)
    fun notifyItemsInserted(startIndex : Int, numInserted : Int)
}

open class BaseMediaListPagingPresenter(private val adapterInterface : PagingAdapterInterface) {
    private var dataContent : MutableList<ContentModel> = mutableListOf()
    private var mediaEntityUnderlyingList : MutableList<MediaEntity> = mutableListOf()

    var hasMore = true

    fun populateWithPagedData(entityList : List<MediaEntity>) {
        mediaEntityUnderlyingList.addAll(entityList)

        val oldListNum = dataContent.size
        dataContent.addAll(
            entityList.map {
                return@map it.asContentModel()
            }
        )

        adapterInterface.notifyItemsInserted(oldListNum, entityList.size)
        hasMore = entityList.isNotEmpty()
    }

    init {
        adapterInterface.initAdapter(dataContent)
    }

    fun findCorrespondingMediaEntityOrNull(contentModel : ContentModel) : MediaEntity? {
        val index = dataContent.indexOf(contentModel)
        return if(index > -1) mediaEntityUnderlyingList[index] else null
    }
}

More specifically, the dataContent in the above. Meanwhile, the Fragment implementing the PagingAdapterInterface has the following implementation of notifyItemsInserted:

override fun notifyItemsInserted(startIndex: Int, numInserted: Int) {
    if(startIndex == 0) {
        adapter.notifyDataSetChanged()
    } else {
        adapter.notifyItemRangeInserted(startIndex, numInserted)
    }
}

adapter being the ContentAdapter instance.

Since DiffUtil.ItemCallback seems to be designed for data swapping instead of appending, with this setup, do I actually need the DiffUtil.ItemCallback?

(And do I need the PagedList instead?)

Gensoukyou1337
  • 1,507
  • 1
  • 13
  • 31
  • "Since DiffUtil.ItemCallback seems to be designed for data swapping instead of appending, with this setup, do I actually need the DiffUtil.ItemCallback?" -- your code does not appear to contain an `ItemCallback`. That is used by `ListAdapter` and `PagedListAdapter`, not directly by `RecycleView.Adapter`. – CommonsWare Feb 03 '20 at 12:20
  • I know, that's because I've used `ListAdapter` before but reverted it back to this code because, considering the circumstances, I thought using `ListAdapter` for this setup was unnecessary. – Gensoukyou1337 Feb 04 '20 at 02:59

0 Answers0