从新定义RecyclerView Adapter的封装,追求既简单又实用,结合Kotlin的高级特性,优化代码书写方式,真正作到高内聚低耦合android
设计图 github
如今有ArrayListAdapter,AnkoListAdapter,将来会有SortedListAdapter,PagingAdapter,为啥这么设计呢?bash
你是否是以为少了点什么功能?空布局,上拉加载,下拉加载,拖动,头布局,脚布局,展开折叠,分割线,动画等等,这些后期都会带着大家去实现,上面的规划更偏向于底层框架的封装,而这些功能更偏向业务组件,方向不一样,不要着急哦,带着大家一步步完善,来体验封装Adapter中的乐趣app
名字 | release aar size | 其余 |
---|---|---|
Core | 25kb | 核心库目前包含ArrayListAdapter的实现 |
Anko | 12kb | anko扩展库包含AnkoListAdapter |
Sorted | 0kb | 待实现 |
.. | .. | 待实现 |
建立xml布局,和以前同样的布局方式框架
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp">
<LinearLayout
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="22sp" />
<TextView
android:id="@+id/tv_subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
复制代码
定义ViewModel及Model,能够看到,逻辑简单明了,刷新本身的时候只须要更新Model,并reBindView便可,刷新别人的话,须要经过Adapter去更新,复杂页面只须要再新建一个ArrayItemViewModel的子类便可,并建立一个新的XML布局,从这里的代码能够看出,一样一个ViewModel将来能够复用不少XML布局,彻底作到了ViewModel、View、Model三个角色的任意复用。为业务多样化提供最底层的支持。dom
/**
* Model
*/
data class ModelTest(var title: String, var subTitle: String)
/**
* ViewModel
*/
class ArrayViewModelTest : ArrayItemViewModel<ModelTest>() {
var index = 0
override fun onBindView(adapter: ArrayListAdapter?) {
viewHolder.itemView.apply {
tv_title.text = model.title
tv_subTitle.text = model.subTitle
cardItem.setOnClickListener {
model.title = "${index++}"
reBindView()
}
}
}
override fun getLayoutRes() = R.layout.item_test
}
复制代码
复用逻辑以下图: ide
Activity 中增删改,增删改都是对ViewModel层的操做,简单实用。布局
/**
* Activity
*/
class ArrayListActivity : AppCompatActivity() {
private val mArrayListAdapter by lazy {
ArrayListAdapter()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_array_list)
rv_list.bindListAdapter(mArrayListAdapter)
// 新增一个
new_add.setText("新增").setOnClickListener {
mArrayListAdapter.add(ArrayViewModelTest().apply {
model = ModelTest("标题", "副标题")
})
}
// 删除第一个
delete.setText("删除").setOnClickListener {
if (mArrayListAdapter.size > 0)
mArrayListAdapter.removeAt(0)
else
toast("请添加新用例后再试")
}
// 随机更新
var updateSize = 0
update.setText("更新").setOnClickListener {
updateSize++
if (mArrayListAdapter.size > 0) {
val randomInt = Random.nextInt(0, mArrayListAdapter.size)
mArrayListAdapter.set(randomInt, ArrayViewModelTest().apply {
model = ModelTest("标题$updateSize", "副标题$updateSize")
})
} else {
toast("请添加新用例后再试")
}
}
}
}
复制代码
定义AnkoLayoutpost
/**
* AnkoItemView
*/
class AnkoItemView(val itemClick: () -> Unit) : AnkoComponent<ViewGroup> {
var tvTitle: TextView? = null
var tvSubTitle: TextView? = null
var view: View? = null
@SuppressLint("ResourceType")
override fun createView(ui: AnkoContext<ViewGroup>) = with(ui) {
cardView {
layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
).apply {
margin = dip(5)
}
verticalLayout {
setOnClickListener {
itemClick()
}
val typedValue = TypedValue()
context.theme
.resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true)
val attribute = intArrayOf(android.R.attr.selectableItemBackground)
val typedArray =
context.theme.obtainStyledAttributes(typedValue.resourceId, attribute)
background = typedArray.getDrawable(0)
layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
).apply {
padding = dip(10)
}
tvTitle = textView {
textSize = px2dip(60)
textColorResource = R.color.colorPrimary
}.lparams(matchParent, wrapContent)
tvSubTitle = textView {
textSize = px2dip(45)
textColorResource = R.color.colorAccent
}.lparams(matchParent, wrapContent)
}
}
}
}
复制代码
定义ViewModel,Model,这里有个细节须要说一下,在ArrayListAdapter的例子中我是在onBindView里设置的点击事件,这样就有个坏处就是致使每次从新onBindView都会致使设置点击事件,这样其实很很差,因此在Anko版本里我作了优化,在onCreateView处理点击事件,这里就作到了设置一次。
/**
* Model
*/
data class ModelTest(var title: String, var subTitle: String)
/**
* ViewModel
*/
class AnkoViewModelTest : AnkoItemViewModel<ModelTest, AnkoItemView>() {
var index = 0
override fun onBindView(adapter: AnkoListAdapter) {
ankoView.tvTitle?.text = model.title
ankoView.tvSubTitle?.text = model.subTitle
}
override fun onCreateView(): AnkoItemView {
return AnkoItemView{
model.title = "${index++}"
reBindView()
}
}
}
复制代码
Activity 中增删改
/**
* Activity
*/
class AnkoLayoutActivity : AppCompatActivity() {
private val mAnkoListAdapter by lazy {
AnkoListAdapter()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AnkoLayoutComponent(mAnkoListAdapter).setContentView(this).apply {
// 新增一个
new_add.setText("新增").setOnClickListener {
mAnkoListAdapter.add(AnkoViewModelTest().apply {
model = ModelTest("标题", "副标题")
})
}
// 删除第一个
delete.setText("删除").setOnClickListener {
if (mAnkoListAdapter.size > 0)
mAnkoListAdapter.removeAt(0)
else
toast("请添加新用例后再试")
}
// 随机更新
var updateSize = 0
update.setText("更新").setOnClickListener {
updateSize++
if (mAnkoListAdapter.size > 0) {
val randomInt = Random.nextInt(0, mAnkoListAdapter.size)
mAnkoListAdapter.set(randomInt, mAnkoListAdapter.getItem(randomInt).apply {
model.also {
it as ModelTest
it.title = "$updateSize"
}
})
} else {
toast("请添加新用例后再试")
}
}
}
}
}
/**
* View
*
*/
class AnkoLayoutComponent(private val ankoListAdapter: AnkoListAdapter) : AnkoComponent<AnkoLayoutActivity> {
override fun createView(ui: AnkoContext<AnkoLayoutActivity>) = with(ui) {
verticalLayout {
recyclerView {
bindListAdapter(ankoListAdapter)
}.lparams(matchParent) {
weight = 1F
}
// Anko 兼容 xml布局的加载
include<View>(R.layout.include_button_bottom)
}
}
}
复制代码
一个资深的Android是否是应该学会本身作一个超级的RecyclerView.Adapter