美文网首页Android
通用的recyclerViewAdapter(kotlin版)

通用的recyclerViewAdapter(kotlin版)

作者: Tenny1225 | 来源:发表于2015-12-25 09:19 被阅读1427次

    立即上代码

    public abstract class BaseRecyclerAdapter<T>(val datas: ArrayList<T>?, val ctx: Context?) : RecyclerView.Adapter<BaseViewHolder>() {
        override fun getItemCount(): Int {
            return datas?.size ?: 0
        }
    
        override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder? {
            var view = getViewResource(viewType)
            var holder = BaseViewHolder(view)
            addAllViewItems(holder?.viewMap,view)
            return holder
        }
    
        override fun onBindViewHolder(holder: BaseViewHolder?, position: Int) {
            bindData(holder?.viewMap, datas?.get(position), position)
        }
    
        /**
         * 添加item布局
         */
        abstract fun getViewResource(viewType: Int): View
    
        /**
         * 记录下item布局下所有控件
         */
        abstract fun addAllViewItems(viewMap: SparseArray<View>?,root:View)
    
        /**
         * 绑定item数据
         */
        abstract fun  bindData(viewMap: SparseArray<View>?, t: T?, position: Int)
    }
    
    public class BaseViewHolder : RecyclerView.ViewHolder {
        var viewMap= SparseArray<View>()
        constructor(itemView: View?) : super(itemView)
    }
    

    相关文章

      网友评论

        本文标题:通用的recyclerViewAdapter(kotlin版)

        本文链接:https://www.haomeiwen.com/subject/eanvhttx.html