极简Kotlin RecyclerView Adapter库:S
2019-07-22 本文已影响6人
ayvytr
SmartAdapter
使用Kotlin编写的极简使用RecyclerView Adapter,无需创建adapter,直接调用RecyclerView.bind().build(),返回创建好的SmartAdapter.
导入 :
Android:
implementation "com.ayvytr:smart-adapter:0.1.0"
Androidx:
implementation "com.ayvytr:smart-adapter-androidx:0.1.0"
Javadoc
使用:
//单个item type:
recycler_view.bind(list, R.layout.item) { item: Item ->
item_text.text = item.value
}}
//创建adapter,最后一步一定要调用!
.build()
//多种 item view:
recycler_view.bind(list, R.layout.item, 1) { item: Item ->
item_text.text = item.value
}
//添加item view的方法
.map(R.layout.item_second, 2) { item: Item ->
item_second_text.text = item.value
}
.map(R.layout.item_custom, 3) { item: Item ->
item_custom_text.text = item.value
}
.map(BindMap4())
//添加自定义DiffCallback
// .diff({ oldItem, newItem -> oldItem.type == newItem.type },
// { oldItem, newItem -> oldItem.value == newItem.value },
// { oldItem, newItem ->
// if (oldItem.value != newItem.value) {
// newItem
// } else null
// },
// { holder, item, payloads -> holder.bind(item) })
//另一种添加自定义DiffCallback的方法
.diff(Diff())
//如何在你的item获取item view type
.type { it.type }
//item click listener
.click { item: Item, i: Int ->
toast("clicked $i $item")
}
//item long click listener
.longClick { item: Item, i: Int ->
toast("long clicked $i $item")
}
//取消DiffCallback
//.cancelDiff()
//创建adapter,最后一步一定要调用!
.build()
class Diff : SmartDiffCallback<Item>({ oldItem, newItem -> oldItem === newItem },
{ oldItem, newItem -> oldItem === newItem && oldItem.value == newItem.value },
{ item: Item, item1: Item -> },
{ param: Any, item: Item, mutableList: MutableList<Any> -> }
)
class BindMap4 : SmartContainer<Item>(R.layout.item_4, 4, { item_text_4.text = it.value }) {
}