drakeet / MultiType

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

新版的注册方法不能使用继承的对象。 #297

Closed zzw3239 closed 4 years ago

zzw3239 commented 4 years ago

Feature Request

zzw3239 commented 4 years ago

改了自己的代码,不再用继承。

drakeet commented 4 years ago

目前 MultiType 对于继承类对象的默认行为是优先寻找对等类型,如果找不到且其父类有注册,则使用父类 Delegate,因此你无需在 register 阶段绑定它们的关系,只要注册其父类 - 父类 ViewDelegate 即可。

zzw3239 commented 4 years ago

@drakeet 抱歉,我可能没有把问题描述清楚,详情如下: open class ParentModel class ChildModel : ParentModel()

open class ParentViewBinder : ItemViewBinder<ParentModel, ParentViewBinder.ViewHolder>() { override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder { val view = inflater.inflate(R.layout.view_alert_item, parent, false) return ViewHolder(view) }

override fun onBindViewHolder(holder: ViewHolder, item: ParentModel) {
    println("this is parent view binder")
}

class ViewHolder(view: View) : RecyclerView.ViewHolder(view)

}

class ChildViewBinder : ParentViewBinder() { override fun onBindViewHolder(holder: ViewHolder, item: ParentModel) { //TODO something of child println("this is child view binder") } }

如上所示,ChildModel继承自ParentModel,ChildViewBinder继承自ParentViewBinder。先前的版本可以这样注册:adapter.register(ChildModel::class.java, ChildViewBinder())。

zzw3239 commented 4 years ago

发现声明ParentViewBinder时变通一下,即可解决上述使用问题。ParentViewBinder声明如下: open abstract class ParentViewBinder 《T》 : ItemViewBinder<T, ParentViewBinder.ViewHolder>() { override fun onBindViewHolder(holder: ViewHolder, item: T) { println("this is parent view binder") } ... } 还是自己功力太弱,才会有上述困扰,打扰了。