kotlin中使用RecyclerView

布局

RecyclerView布局

<androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:itemCount="4"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/wx_bg"
                android:layout_marginStart="30dp"
                android:layout_marginEnd="30dp"
                tools:listitem="@layout/list_item"/>      //这里写列表中单个布局

RecyclerView 适配器

data存储bean的list,R.layout.list_item是上面的list布局文件,R.id.amount是list布局中的子view

class BaseAdapter(data: MutableList<Amount>) : BaseQuickAdapter<Amount, BaseViewHolder>(R.layout.list_item, data){
    override fun convert(holder: BaseViewHolder, item: Amount) {
        holder.setText(R.id.amount,item.amount)
        holder.setText(R.id.remark,item.remark)
        holder.setText(R.id.doorsill,item.doorsill
    }
}

给RecyclerView设置属性

var adapter = BaseAdapter(list)     //将数据放入适配器中
binding.rvList.adapter=  adapter    // 创建适配器并给RecyclerView适配器赋值
binding.rvList.layoutManager = GridLayoutManager(this,3)   //设置RecyclerView的布局管理器 ,这里设置的是网格布局,3列

设置适配器监听器

adapter.setOnItemClickListener { adapter, view, position ->
           if (position == 0 && AdConfig.banVideo) {
                ToastUtils.showToast("禁止${list[position].substance}展示广告")
                return@setOnItemClickListener
            }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值