准备正式使用Kotlin做为项目的开发语言。对以前的封装类进行转换

abstract class CommonAdapter<T : ViewDataBinding, E : Any> : BaseAdapter {

    protected var binding: T? = null
    protected var context: Context? = null
    protected var datas: List<E>? = null
    protected var inflater: LayoutInflater? = null

    constructor(context: Context?, datas: List<E>?) : super() {
        this.context = context
        this.datas = datas
        this.inflater = LayoutInflater.from(context)
    }

    abstract fun getLayoutId(): Int

    @SuppressLint("ViewHolder")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        if (getLayoutId() <= 0) throw RuntimeException("layoutId not found")
        binding = DataBindingUtil.inflate(inflater!!, getLayoutId(), parent, false)
        convertView(binding!!, getItem(position), position)
        return binding!!.root
    }

    override fun getItem(position: Int): E? = datas?.get(position)

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getCount(): Int = if (datas?.isEmpty()!!) 0 else datas!!.size

    fun updateNotes(datas: List<E>) {
        this.datas = datas
        notifyDataSetChanged()
    }

    abstract fun convertView(t: T, item: E?, position: Int)
}
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐