RecyclerViewAdapter
Introduction: 科学分包,动态扩展,功能完善,书写简洁,按需依赖,合理抽象,超高内聚,超低耦合,没有最好,只有更好,希望能帮助到您,🙏❤️🙏❤️
Tags:
It has been written 100% in Kotlin. ❤️ 重新定义 RecyclerView Adapter 的封装,追求既简单又实用,结合 Kotlin 的高级特性,优化代码书写方式,真正做到高内聚低耦合
设计图

特点
- DSL 写法,简单易用
- 充分考虑 ViewHolder 复用逻辑,科学防止触碰事件抖动问题
- 抽象合理,高可复用
- 分包依赖,也可以一次性依赖
- 自带 Lifecycle 生命周期管理
- 动态扩展,耦合降低到了极致
- 部分真泛型,优化泛型问题
库大小
| 名字 | release aar size | 其他 |
|---|---|---|
| Core | 32kb | 核心库目前包含 ListAdapter 的实现,最基础且最实用的扩展 |
| DataBinding | 22kb | 适配 DataBinding 布局,适用本项目所有 Adapter 扩展 |
| Sorted | 10kb | SortedListAdapter 扩展实现 |
| Paging | 13kb | PagingListAdapter 扩展适配 |
| Diff | 6kb | 适配 DiffUtil,目前适用 ListAdapter |
| FlexBox | 9kb | 适配 FlexBox 布局 |
| Selectable | 8kb | 动态扩展单选、多选、最大可选项功能 |
| Expandable | 8kb | 动态扩展可展开功能,支持仅单展开或多展开配置 |
| UI | 17kb | 扩展空布局 |
| Animators | 14kb | 扩展动画,包括布局初加载动画,添加 Item 动画,更新 Item 动画等 |
| Filter | 9kb | 按照条件过滤列表子项 |
各个 Adapter 的优势在哪,如何选择?
| 名字 | 优势 | 劣势 | 适合做什么 |
|---|---|---|---|
| ListAdapter | 简单实用,易扩展,ViewHolder 复用率高,DSL 支持写法优美 | 对于需要排序的列表处理麻烦性能低 | 不考虑排序的一般列表 |
| SortedListAdapter | 排序超级容易 | 侵入性高,需要继承 SortedModel 实现 Model 层,目前优化为接口,增加了可使用范围 | 任何需要排序的列表 |
| PagingListAdapter | 自带加载状态,后台计算完成后通知刷新,加载效率高 | 侵入性高,需要 Model 层继承实现,学习成本高,掌握难度高 | 适合自动加载分页的列表 |
各个 ViewModel
适用范围 | 名字 | ListAdapter | SortedListAdapter | PagingListAdapter | | ---- | ---- | ---- | ---- | | LayoutViewModel | 支持 | 支持 | 支持 | | BindingViewModel | 支持 | 支持 | 支持 |
如何选择 | 名字 | 功能 | 优势 | 其他 | | ---- | ---- | ---- | ---- | | LayoutViewModel | 加载 XML 布局 | 灵活,易懂,易用 | | | BindingViewModel | 加载 DataBinding 布局 | 省去绑定 View 的过程,代码逻辑简洁 | |
如何依赖?
提示:按需依赖哦,没必要全部依赖哦
allprojects {
repositories {
// 首先项目根目录的 build.gradle 文件中加入这一行
maven { url 'https://jitpack.io' }
}
}
//一次性依赖,会自动引入全部 AAR
implementation 'com.github.ibaozi-cn.RecyclerViewAdapter:1.2.5'
//以下为分包依赖
def adapterVersion = '1.2.5'
//核心库
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-core:$adapterVersion"
//下面都是可选项
//diffutil 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-diff:$adapterVersion"
//data binding 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-binding:$adapterVersion"
// paging3 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-paging:$adapterVersion"
// sortedlist 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-sorted:$adapterVersion"
// flexbox 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-flex:$adapterVersion"
// UI 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-ui:$adapterVersion"
// Selectable 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-selectable:$adapterVersion"
// Expandable 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-expandable:$adapterVersion"
// Animators 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-animators:$adapterVersion"
// Filter 扩展
implementation "com.github.ibaozi-cn.RecyclerViewAdapter:adapter-filter:$adapterVersion"
环境需要
- Kotlin
- JAVA
- AndroidX 抱歉目前按照最新的 AndroidX 适配的,如有其他需要请私聊我。
怎么用?
看一个最简单的例子 DSL 的支持
//xml 布局加载示例
fun createViewModelList(max: Int = 10) = (0..max).map { _ ->
layoutViewModelDsl(R.layout.item_test, ModelTest("title", "subTitle")) {
onBindViewHolder {
val model = getModel<ModelTest>()
getView<TextView>(R.id.tv_title).text = model?.title
getView<TextView>(R.id.tv_subTitle).text = model?.subTitle
}
itemView.setOnClickListener {
val vm = getViewModel<LayoutViewModel<ModelTest>>()
//修改 Model 数据
vm?.model?.title = "测试更新${Random.nextInt(10000)}"
//用 Adapter 更新数据
getAdapter<ListAdapter>()?.set(adapterPosition, vm)
}
}
}
//binding layout 布局加载示例
fun createBindingViewModelList(max: Int = 10) = (0..max).map {
bindingViewModelDsl(
R.layout.item_binding_layout,
BR.model,
ModelTest("title", "bindingViewModelDsl")
) {
itemView.setOnClickListener {
val viewModel = getViewModel<BindingViewModel<ModelTest>>()
viewModel?.model?.title = "${java.util.Random().nextInt(100)}"
getAdapter<ListAdapter>()?.set(adapterPosition, viewModel)
}
}
}
//添加到 Adapter 中,并绑定 RecyclerView
listAdapter {
addAll(createViewModelList(3))
addAll(createBindingViewModelList(3))
// 绑定 RecyclerView
into(rv_list_dsl)
}
开发者
- i 校长
License
Copyright 2020 Mike Penz
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.
