布局
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
}
}