Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultiTypeDelegate for management Types #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

qdsfdhvh
Copy link

我想结合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)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant