-
Notifications
You must be signed in to change notification settings - Fork 19
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
与 MultiType 的异同 #1
Comments
@huclengyue 1. 我们最终是要形成一种数据与视图的对应关系,即 MoreType 的 data 和 viewType。
2. 更简单的实现视图 只需要继承
可以看到,我们并不用去重复写 ViewHolder,如果配合 class MessageInViewType : MoreViewType<Message>() {
override fun getViewLayout(): Int = R.layout.item_view_multi_message_in
override fun getViewModel(): KClass<Message> = Message::class
override fun bindData(data: Message, holder: MoreViewHolder) {
holder.itemView.message_icon.setImageURI(data.icon)
if (data.showTime) {
holder.itemView.message_time.visibility = View.VISIBLE
holder.itemView.message_time.text = Utils.sendTime(data.time.toLong() * 1000)
} else {
holder.itemView.message_time.visibility = View.INVISIBLE
}
holder.itemView.message_content_layout.removeAllViews()
var currentLayout: RelativeLayout? = null
when (data.messageType) {
"text" -> {
currentLayout = LayoutInflater.from(holder.itemView.context).inflate(R.layout.widget_view_message_in_text, holder.itemView.message_content_layout, false) as RelativeLayout
val text = currentLayout.findViewById<AppCompatTextView>(R.id.message_in_text)
text.text = data.text
}
"image" -> {
currentLayout = LayoutInflater.from(holder.itemView.context).inflate(R.layout.widget_view_message_in_image, holder.itemView.message_content_layout, false) as RelativeLayout
val image = currentLayout.findViewById<SimpleDraweeView>(R.id.message_in_image)
image.setImageURI(data.url)
setImgSize(data.width, data.height, image)
}
else -> {
}
}
holder.itemView.message_content_layout.addView(currentLayout)
}
fun setImgSize(width: String, height: String, image: SimpleDraweeView) {
val size = Utils.getIMImageSize(width.toDouble(), height.toDouble())
val lp = image.layoutParams
lp.width = size.width
lp.height = size.height
image.layoutParams = lp
}
} 3. adapter 链式使用 先看一个完整的使用例子 adapter.userSoleRegister()
.register(TitleViewType())
.register(CompleteViewType())
.register(HorizontalViewType())
.multiRegister(Message::class, object : MultiLink<Message> {
override fun link(data: Message): MoreViewType<Message>? {
if (data.me) {
return MessageOutViewType()
} else {
return MessageInViewType()
}
}
})
.renderWithAnimation()
.startAnimPosition(1)
.attachTo(complete_list)
4. 强大的 Kotlin 这个应该是最大的不同的, kotlin 真的是一门写起来让人很爽的语言,得益于 Kotlin ,MoreType 可以更加自如的操作数据,内置了以下一些方法:
通过这些方法,可以更方便快速的操作数据,完成下拉刷新,上滑加载等功能,可以参考这里的实现CompleteActivity.kt 开始你会发现很像,确实 MultiType 的思路在我看来是目前有关组合布局显示上实践最好的,当然如果你去查大量源码,你会发现很多库都是这个思路,并不是 MultiType 独创,好的技术当然值得学习。 MoreType 的目的是为了更简洁的方便人们使用,当你看完 sample 中的示例代码,你会发现唯一的不同就是处处不同 😄 |
这个和MultiType有什么区别呢?
The text was updated successfully, but these errors were encountered: