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

请问BRVAH的多布局中也可以直接用吗 #35

Closed
HeJingWei opened this issue Oct 19, 2021 · 4 comments
Closed

请问BRVAH的多布局中也可以直接用吗 #35

HeJingWei opened this issue Oct 19, 2021 · 4 comments

Comments

@HeJingWei
Copy link

多布局的样式是继承的BaseItemProvider,重写的方法是onCreateViewHolder,
也可以直接这样写吗?
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
return super.onCreateViewHolder(parent, viewType).withBinding { ItemFooBinding.bind(it) }
}

@DylanCaiCoding
Copy link
Owner

写法没问题,不过重写两个参数的 onCreateViewHolder 方法不知道有没什么隐患。我看了下 BRVAH 的文档是重写只有一个 ViewGroup 参数的方法,最好按照官方的来。

image

@HeJingWei
Copy link
Author

我现在用的3.0以上版本,官方这个可重写的方法是2个参数的,一个参数的方法以及没了,我先试一试可以不可以

@DylanCaiCoding
Copy link
Owner

我现在用的3.0以上版本,官方这个可重写的方法是2个参数的,一个参数的方法以及没了,我先试一试可以不可以

那应该要判断一下是不是当前的类型:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
  return when (viewType) {
    itemViewType -> super.onCreateViewHolder(parent, viewType).withBinding { ItemFooBinding.bind(it) }
    else -> super.onCreateViewHolder(parent, viewType)
  }
}

@HeJingWei
Copy link
Author

HeJingWei commented Oct 19, 2021

我参考adapter封装了一个多布局的基类,现在能正常显示

abstract class BaseBindingItemProvider<T, VB : ViewBinding>
(private val inflate: (LayoutInflater, ViewGroup, Boolean) -> VB) : BaseItemProvider<T>() {
override val layoutId: Int
    get() = -1

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
    return BaseBindingHolder(inflate(LayoutInflater.from(parent.context), parent, false))
}

class BaseBindingHolder(private val binding: ViewBinding) : BaseViewHolder(binding.root) {
    constructor(itemView: View) : this(ViewBinding { itemView })

    @Suppress("UNCHECKED_CAST")
    fun <VB : ViewBinding> getViewBinding() = binding as VB
}

override fun convert(helper: BaseViewHolder, item: T) {
    convert(helper as BaseBindingHolder,item)
}

abstract fun convert(helper: BaseBindingHolder, item: T)

}

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

No branches or pull requests

2 participants