如何在Kotlin中封装RecyclerViewAdapter-创新互联
如何在Kotlin中封装RecyclerView Adapter?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
成都创新互联公司专注于企业成都全网营销推广、网站重做改版、新平网站定制设计、自适应品牌网站建设、html5、成都商城网站开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为新平等各大城市提供网站开发制作服务。单类型的使用
val adapter=recyclerView.setUp(users, R.layout.item_layout, { holder, item -> var binding = DataBindingUtil.getBinding(holder.itemView) binding.nameText.text = item.name ... })
多类型的使用
recyclerView.setUP(users, listItems = *arrayOf( ListItem(R.layout.item_layout, { holder, item -> var binding = DataBindingUtil.getBinding(holder.itemView) binding?.nameText?.text = item.name ... }, { Snackbar.make(window.decorView, it.name, Snackbar.LENGTH_SHORT).show() }), ListItem(R.layout.item_layout2, { holder, item -> val nameText: TextView = holder.getView(R.id.nameText) nameText.text = item.name ... }, { }) ))
使用就是如此简单,再来看下代码是不是过度封装
Adapter的基类
abstract class AbstractAdapter- constructor(protected var itemList: List
- ) : RecyclerView.Adapter
() { override fun getItemCount() = itemList.size override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder { val view = createItemView(parent, viewType) val viewHolder = Holder(view) val itemView = viewHolder.itemView itemView.setOnClickListener { val adapterPosition = viewHolder.adapterPosition if (adapterPosition != RecyclerView.NO_POSITION) { onItemClick(itemView, adapterPosition) } } return viewHolder } fun update(items: List - ) { updateAdapterWithDiffResult(calculateDiff(items)) } private fun updateAdapterWithDiffResult(result: DiffUtil.DiffResult) { result.dispatchUpdatesTo(this) } private fun calculateDiff(newItems: List
- ) = DiffUtil.calculateDiff(DiffUtilCallback(itemList, newItems)) fun add(item: ITEM) { itemList.toMutableList().add(item) notifyItemInserted(itemList.size) } fun remove(position: Int) { itemList.toMutableList().removeAt(position) notifyItemRemoved(position) } final override fun onViewRecycled(holder: Holder) { super.onViewRecycled(holder) onViewRecycled(holder.itemView) } protected open fun onViewRecycled(itemView: View) { } protected open fun onItemClick(itemView: View, position: Int) { } protected abstract fun createItemView(parent: ViewGroup, viewType: Int): View class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val views = SparseArray
() fun getView(viewId: Int): T { var view = views[viewId] if (view == null) { view = itemView.findViewById(viewId) views.put(viewId, view) } return view as T } } }
子类的实现和RecyclerView的扩展
class SingleAdapter- (items: List
- , private val layoutResId: Int, private val bindHolder: (Holder, ITEM) -> Unit) : AbstractAdapter
- (items) { private var itemClick: (ITEM) -> Unit = {} constructor(items: List
- , layoutResId: Int, bindHolder: (Holder, ITEM) -> Unit, itemClick: (ITEM) -> Unit = {}) : this(items, layoutResId, bindHolder) { this.itemClick = itemClick } override fun createItemView(parent: ViewGroup, viewType: Int): View { var view = parent inflate layoutResId if (view.tag?.toString()?.contains("layout/") == true) { DataBindingUtil.bind
(view) } return view } override fun onBindViewHolder(holder: Holder, position: Int) { bindHolder(holder, itemList[position]) } override fun onItemClick(itemView: View, position: Int) { itemClick(itemList[position]) } } class MultiAdapter - (private val items: List
- , private val bindHolder: (Holder, ITEM) -> Unit) : AbstractAdapter
- (items) { private var itemClick: (ITEM) -> Unit = {} private lateinit var listItems: Array
> constructor(items: List - , listItems: Array
>, bindHolder: (Holder, ITEM) -> Unit, itemClick: (ITEM) -> Unit = {}) : this(items, bindHolder) { this.itemClick = itemClick this.listItems = listItems } override fun createItemView(parent: ViewGroup, viewType: Int): View { var view = parent inflate getLayoutId(viewType) if (view.tag?.toString()?.contains("layout/") == true) { DataBindingUtil.bind (view) } return view } private fun getLayoutId(viewType: Int): Int { var layoutId = -1 listItems.forEach { if (it.layoutResId == viewType) { layoutId = it.layoutResId return@forEach } } return layoutId } override fun getItemViewType(position: Int): Int { return items[position].getType() } override fun onBindViewHolder(holder: Holder, position: Int) { bindHolder(holder, itemList[position]) } override fun onItemClick(itemView: View, position: Int) { itemClick(itemList[position]) } } fun - RecyclerView.setUp(items: List
- , layoutResId: Int, bindHolder: (AbstractAdapter.Holder, ITEM) -> Unit, itemClick: (ITEM) -> Unit = {}, manager: RecyclerView.LayoutManager = LinearLayoutManager(this.context)): AbstractAdapter
- { val singleAdapter by lazy { SingleAdapter(items, layoutResId, { holder, item -> bindHolder(holder, item) }, { itemClick(it) }) } layoutManager = manager adapter = singleAdapter return singleAdapter } fun
- RecyclerView.setUP(items: List
- , manager: RecyclerView.LayoutManager = LinearLayoutManager(this.context), vararg listItems: ListItem
- ): AbstractAdapter
- { val multiAdapter by lazy { MultiAdapter(items, listItems, { holder, item -> var listItem: ListItem
- ? = getListItem(listItems, item) listItem?.bindHolder?.invoke(holder, item) }, { item -> var listItem: ListItem
- ? = getListItem(listItems, item) listItem?.itemClick?.invoke(item) }) } layoutManager = manager adapter = multiAdapter return multiAdapter } private fun
- getListItem(listItems: Array
>, item: ITEM): ListItem - ? { var listItem: ListItem
- ? = null listItems.forEach { if (it.layoutResId == item.getType()) { listItem = it return@forEach } } return listItem } class ListItem
- (val layoutResId: Int, val bindHolder: (holder: AbstractAdapter.Holder, item: ITEM) -> Unit, val itemClick: (item: ITEM) -> Unit = {}) interface ListItemI { fun getType(): Int }
ok,所有核心代码,没有了,也不打算发布rar,要用的直接clone下来引入项目,这是最好的方式,因为不复杂,要改随时可以改。
看上面的多类型的使用,可以发现它是支持普通Layout和DataBinding Layout的,这也是本库的一个特色,不需要多余的处理。
1.普通的Layout 这样处理
ListItem(R.layout.item_layout2, { holder, item -> val nameText: TextView = holder.getView(R.id.nameText) nameText.text = item.name }
通过Holder来操作View,里面有做缓存的。
DataBinding Layout ListItem(R.layout.item_layout, { holder, item -> var binding = DataBindingUtil.getBinding(holder.itemView) binding.nameText.text = item.name }
是不是只要自己知道是哪中Layout,对应处理就可以了,Holder处理方式也是可以处理DataBinding Layout的,要知晓。
这里提下,可能有人会问干嘛不直接用Kotlin的Layout View 查找方法???
那样代码看起来是简单,但是现在的Studio 对这个的支持不是很好,经常报红,程序员看到红会烦躁啊!!如果还是喜欢的话实现也很简单,改成View的扩展返回就可以了,可以自己动手试下哦。
因为这里只是对不变的部分进行了封装,没有很多华丽丽的添加头部、脚部啥的功能,点击事件倒是内置了一种,当然点击事件还可以用ItemTouchHelper实现,都是可以的。
这样每次就不用写一大串的Adaper了,是不是可以开心地泡壶茶,吹口气了。
别的库都可以Item复用的,你的可以吗?
嗯嗯、、?可以的
比如
val item: (AbstractAdapter.Holder, User) -> Unit = { holder, user -> }
再比如
ListItem(R.layout.item_layout, { holder, item -> var binding = DataBindingUtil.getBinding(holder.itemView) }, {//点击事件 Snackbar.make(window.decorView, it.name, Snackbar.LENGTH_SHORT).show() })
看完上述内容,你们掌握如何在Kotlin中封装RecyclerView Adapter的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网站名称:如何在Kotlin中封装RecyclerViewAdapter-创新互联
网页路径:http://azwzsj.com/article/dodjoj.html