drakeet / MultiType

Flexible multiple types for Android RecyclerView.
Apache License 2.0
5.76k stars 751 forks source link

Add MultiTypeDelegate for management Types #325

Open qdsfdhvh opened 3 years ago

qdsfdhvh commented 3 years ago

我想结合paging3与MultiType一起使用,但是自定义Adapter上一直找不到很好的方案;

现在我的想法是把MultiTypeAdapter中Types的处理整理成了一个MultiTypeDelegate接口,基于这个接口再自定义adapter; 这样只要ViewDelegate不使用adapter与adapterItems,就可以较低成本的接收PagingData再用MultiType加载。

所以尝试提了这个PR。

abstract class MultiPagingDataAdapter<T : Any>(
  diffCallback: DiffUtil.ItemCallback<T>,
  initialCapacity: Int = 0,
  override val types: Types = MutableTypes(initialCapacity),
) : PagingDataAdapter<T, ViewHolder>(diffCallback), MultiTypeDelegate {

  override fun getItemViewType(position: Int): Int {
    return indexInTypesOf(position, getItem(position)!!)
  }

  override fun onCreateViewHolder(parent: ViewGroup, indexViewType: Int): ViewHolder {
    return getOutDelegate(indexViewType).onCreateViewHolder(parent.context, parent)
  }

  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    onBindViewHolder(holder, position, emptyList())
  }

  override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: List<Any>) {
    getOutDelegateByViewHolder(holder).onBindViewHolder(holder, getItem(position)!!, payloads)
  }

  override fun onViewRecycled(holder: ViewHolder) {
    getOutDelegateByViewHolder(holder).onViewRecycled(holder)
  }

  override fun onFailedToRecycleView(holder: ViewHolder): Boolean {
    return getOutDelegateByViewHolder(holder).onFailedToRecycleView(holder)
  }

  override fun onViewAttachedToWindow(holder: ViewHolder) {
    getOutDelegateByViewHolder(holder).onViewAttachedToWindow(holder)
  }

  override fun onViewDetachedFromWindow(holder: ViewHolder) {
    getOutDelegateByViewHolder(holder).onViewDetachedFromWindow(holder)
  }
}