kotlin版本的RecyclerView Adapter

 

之前刚用kotlin 的时候,很多语言的特性,由于不熟悉都没有应用,最近修改了一下

abstract class BaseAdapter<T, B : ViewBinding?> :
    RecyclerView.Adapter<BaseAdapter.ViewHolder<B>> {
    lateinit var onItemClick: (view: View, pos:Int)->Unit
    protected var mDatas: ArrayList<T>? = null
    protected var context: android.content.Context
    fun getmDatas(): ArrayList<T>? {
        return mDatas
    }

    fun setmDatas(mDatas:ArrayList<T>?){
        this.mDatas = mDatas
    }

    constructor(context: android.content.Context) {
        this.context = context
    }

    constructor(context: android.content.Context, mDatas: ArrayList<T>?) {
        this.mDatas = mDatas
        this.context = context
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseAdapter.ViewHolder<B> {
        val inflater = LayoutInflater.from(context)
        val binding = setBinding(inflater, parent)
        val viewHolder =
            BaseAdapter.ViewHolder<B>(binding!!.root,onItemClick)
        viewHolder.setBindingInstance(binding)
        return viewHolder
    }

    override fun onBindViewHolder(
        holder: ViewHolder<B>,
        position: Int
    ) {
        val myholder =
            holder
        onBindHolder(myholder, position)
    }

    protected abstract fun onBindHolder(
        holder: BaseAdapter.ViewHolder<B>,
        position: Int
    )

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    protected abstract fun setBinding(inflater: LayoutInflater, parent: ViewGroup): B
    override fun getItemCount(): Int {
        return if (mDatas == null) 0 else mDatas!!.size
    }

    class ViewHolder<B : ViewBinding?>(itemView:View,var onItemClick:(view: View, pos:Int)->Unit) :
        RecyclerView.ViewHolder(itemView), View.OnClickListener {
        var binding: B? = null

        fun setBindingInstance(binding: B): kotlin.Unit {
            this.binding = binding
        }

        fun setClick(vararg views:View){
            if (onItemClick != null) {
                for (view in views) {
                    view.setOnClickListener(this)
                }
            }
        }

        override fun onClick(v: View) {
            onItemClick(v, getAdapterPosition())
        }
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RecyclerView is a UI component that is used to display large sets of data in an efficient and scrollable manner. The RecyclerView widget is a more advanced and flexible version of ListView. To use RecyclerView, you need to create an adapter that will hold the data and create the views to be displayed. Here is an example of a RecyclerView adapter in Kotlin: ``` class MyAdapter(private val dataList: List<MyData>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val data = dataList[position] holder.bind(data) } override fun getItemCount() = dataList.size inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(data: MyData) { // bind data to views itemView.textViewTitle.text = data.title itemView.textViewDescription.text = data.description } } } ``` In this example, `MyData` is a data class that holds the data to be displayed in the RecyclerView. The `MyAdapter` class takes a list of `MyData` objects as a parameter in the constructor. The `onCreateViewHolder` method inflates a layout file (`item_layout.xml` in this case) and returns a `ViewHolder` object. The `onBindViewHolder` method binds the data to the views in the `ViewHolder`. Finally, the `getItemCount` method returns the number of items in the list, which is the size of the `dataList`. The `ViewHolder` class holds the views that will be displayed in the RecyclerView and the `bind` method binds the data to the views.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值