关于
(Published to MavenCentral) Kotlin way of building RecyclerView Adapter 🧩. You do not have to write RecyclerView Adapters again and again and suffer from handling of different view types. Kiel will help you.
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Kiel
Kiel is a RecyclerView.Adapter with a minimalistic and convenient Kotlin DSL which provides utility on top of Android's normal RecyclerView.Adapter.
Most of the time:
- We found ourselves repeating same boilerplate code for
RecyclerView.Adapter. - We have difficulty in handling
RecyclerView.Adapterwhen there are manyviewTypes.
But now, Kiel may help us to get rid of these problems. You may read the detailed story in this blog post.
Usage:
Basic Usage:
adapterOf
val recyclerViewAdapter = adapterOf<Text> {
register(
layoutResource = R.layout.adapter_message_text_item,
viewHolder = ::TextMessageViewHolder,
onViewHolderCreated = { vh->
//you may handle your on click listener
vh.itemView.setOnClickListener {
}
},
onBindViewHolder = { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
)
}
recyclerView.adapter = recyclerViewAdapter
pagingDataAdapterOf
val pagingDataAdapterOf = pagingDataAdapterOf<Text> {
register(
layoutResource = R.layout.adapter_message_text_item,
viewHolder = ::TextMessageViewHolder,
onViewHolderCreated = { vh->
//you may handle your on click listener
vh.itemView.setOnClickListener {
}
},
onBindViewHolder = { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
)
}
recyclerView.adapter = recyclerViewAdapter
Different View Types:
You may register different ViewHolders to your adapters.
register(
layoutResource = R.layout.adapter_message_text_item,
viewHolder = ::TextMessageViewHolder,
onBindViewHolder = { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
)
register(
layoutResource = R.layout.adapter_message_image_item,
viewHolder = ::ImageMessageViewHolder,
onBindViewHolder = { vh, _, item ->
vh.messageText.text = item.text
vh.sentAt.text = item.sentAt
Glide.with(vh.messageImage)
.load(item.imageUrl)
.into(vh.messageImage)
}
)
Handling Events:
As ViewHolder instance is accessible in:
onViewHolderCreatedonBindViewHolderonBindViewHolderWithPayload
You can handle the events in the same way how you did it before.
val recyclerViewAdapter = adapterOf<Text> {
register(
layoutResource = R.layout.adapter_message_text_it,
viewHolder = ::TextMessageViewHolder,
onViewHolderCreated = { vh->
vh.itemView.setOnClickListener {
}
vh.messageText.addTextChangedListener{text ->
}
},
onBindViewHolder = { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
)
}
recyclerView.adapter = recyclerViewAdapter
View Binding:
As ViewHolder instance is accessible in:
onViewHolderCreatedonBindViewHolderonBindViewHolderWithPayload
You may define your ViewBinding in your ViewHolder class and you can easily reach it:
class TextMessageViewHolder(view: View) : RecyclerViewHolder<Text>(view) {
val binding = AdapterTextItemBinding.bind(view)
}
val recyclerViewAdapter = adapterOf<Text> {
register(
layoutResource = R.layout.adapter_message_text_it,
viewHolder = ::TextMessageViewHolder,
onViewHolderCreated = { vh->
vh.binding.
},
onBindViewHolder = { vh, _, it ->
vh.binding.messageText.text = it.text
vh.binding.sentAt.text = it.sentAt
}
)
}
DiffUtil:
val recyclerViewAdapter = adapterOf<MessageViewState> {
diff(
areContentsTheSame = { old, new -> old == new },
areItemsTheSame = { old, new -> old.message.id == new.message.id },
getChangePayload = { oldItem, newItem ->
val diffBundle = Bundle()
if (oldItem.selectionState != newItem.selectionState) {
diffBundle.putParcelable(
TextMessageViewHolder.KEY_SELECTION,
newItem.selectionState
)
}
if (diffBundle.isEmpty) null else diffBundle
}
)
register (
layoutResource = R.layout.adapter_message_text_item,
viewHolder = ::TextMessageViewHolder,
onBindViewHolder = { vh, _, it ->
vh.messageText.text = it.message.text
vh.sentAt.text = it.message.sentAt
}
)
Download
implementation 'io.github.ibrahimyilmaz:kiel:latestVersion'
latestVersion = 1.2.1
License
Copyright 2020 Ibrahim Yilmaz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
探索相关项目
加载中…