假设我有一个 RecyclerView 的适配器,如下所示:
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
}
data
此适配器的构造函数中的变量引用MutableList<ContentModel>
由另一个类管理的 a,即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
}
}
更具体地说,dataContent
在上面。同时,实现 的 FragmentPagingAdapterInterface
具有以下实现notifyItemsInserted
:
override fun notifyItemsInserted(startIndex: Int, numInserted: Int) {
if(startIndex == 0) {
adapter.notifyDataSetChanged()
} else {
adapter.notifyItemRangeInserted(startIndex, numInserted)
}
}
adapter
是 ContentAdapter 实例。
由于DiffUtil.ItemCallback
似乎是为数据交换而不是附加而设计的,因此使用此设置,我真的需要DiffUtil.ItemCallback
吗?
(我是否需要 PagedList 代替?)