您好,登录后才能下订单哦!
如何在Kotlin中封装RecyclerView Adapter?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
单类型的使用
val adapter=recyclerView.setUp(users, R.layout.item_layout, { holder, item -> var binding = DataBindingUtil.getBinding<ItemLayoutBinding>(holder.itemView) binding.nameText.text = item.name ... })
多类型的使用
recyclerView.setUP(users, listItems = *arrayOf( ListItem(R.layout.item_layout, { holder, item -> var binding = DataBindingUtil.getBinding<ItemLayoutBinding>(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<ITEM> constructor(protected var itemList: List<ITEM>) : RecyclerView.Adapter<AbstractAdapter.Holder>() { 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<ITEM>) { updateAdapterWithDiffResult(calculateDiff(items)) } private fun updateAdapterWithDiffResult(result: DiffUtil.DiffResult) { result.dispatchUpdatesTo(this) } private fun calculateDiff(newItems: List<ITEM>) = 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<View>() fun <T : View> 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<ITEM>(items: List<ITEM>, private val layoutResId: Int, private val bindHolder: (Holder, ITEM) -> Unit) : AbstractAdapter<ITEM>(items) { private var itemClick: (ITEM) -> Unit = {} constructor(items: List<ITEM>, 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<ViewDataBinding>(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<ITEM : ListItemI>(private val items: List<ITEM>, private val bindHolder: (Holder, ITEM) -> Unit) : AbstractAdapter<ITEM>(items) { private var itemClick: (ITEM) -> Unit = {} private lateinit var listItems: Array<out ListItem<ITEM>> constructor(items: List<ITEM>, listItems: Array<out ListItem<ITEM>>, 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<ViewDataBinding>(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 <ITEM> RecyclerView.setUp(items: List<ITEM>, layoutResId: Int, bindHolder: (AbstractAdapter.Holder, ITEM) -> Unit, itemClick: (ITEM) -> Unit = {}, manager: RecyclerView.LayoutManager = LinearLayoutManager(this.context)): AbstractAdapter<ITEM> { val singleAdapter by lazy { SingleAdapter(items, layoutResId, { holder, item -> bindHolder(holder, item) }, { itemClick(it) }) } layoutManager = manager adapter = singleAdapter return singleAdapter } fun <ITEM : ListItemI> RecyclerView.setUP(items: List<ITEM>, manager: RecyclerView.LayoutManager = LinearLayoutManager(this.context), vararg listItems: ListItem<ITEM>): AbstractAdapter<ITEM> { val multiAdapter by lazy { MultiAdapter(items, listItems, { holder, item -> var listItem: ListItem<ITEM>? = getListItem(listItems, item) listItem?.bindHolder?.invoke(holder, item) }, { item -> var listItem: ListItem<ITEM>? = getListItem(listItems, item) listItem?.itemClick?.invoke(item) }) } layoutManager = manager adapter = multiAdapter return multiAdapter } private fun <ITEM : ListItemI> getListItem(listItems: Array<out ListItem<ITEM>>, item: ITEM): ListItem<ITEM>? { var listItem: ListItem<ITEM>? = null listItems.forEach { if (it.layoutResId == item.getType()) { listItem = it return@forEach } } return listItem } class ListItem<ITEM>(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<ItemLayoutBinding>(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<ItemLayoutBinding>(holder.itemView) }, {//点击事件 Snackbar.make(window.decorView, it.name, Snackbar.LENGTH_SHORT).show() })
看完上述内容,你们掌握如何在Kotlin中封装RecyclerView Adapter的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。