android RecyclerView adapter 封装

package com.durang.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

class BaseAdapter(val layoutResourceId: Int, val items: List, val init: (View, T) -> Unit) :
    RecyclerView.Adapter>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(layoutResourceId,null)
        return ViewHolder(view, init)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindForecast(items[position])
    }

    override fun getItemCount() = items.size

    class ViewHolder(view: View, val init: (View, T) -> Unit) : RecyclerView.ViewHolder(view) {
        fun bindForecast(item: T) {
            with(item) {
                init(itemView, item)
            }
        }
    }
}
var adapter: BaseAdapter? = null
adapter = BaseAdapter(R.layout.item_goods_release,goods){
        view, goods ->
    view.tvGtitle.text = goods.gtitle
    view.tvGdesc.text = goods.gdesc
    view.tvGTime.text = MyUtils.TimestampToDate(goods.gtime!!,"yyyy/MM/dd HH:mm")
    view.tvGbrowse.text = "浏览量:"+goods.gbrowse
}

你可能感兴趣的:(RecyclerView,RecyclerView,Adapter,Adapter,封装,android)