Android中RecyclerView使用详解(一)

概述

RecyclerView 是 Android 开发中一个非常强大且灵活的组件,用于在有限的窗口中展示大量数据集。它提供了一种比传统的 ListView 更高效的方式来滚动大量数据项。RecyclerView 不仅可以像 ListView 那样垂直滚动,还可以水平滚动,甚至支持更复杂的布局,如网格布局(Grid Layout)、瀑布流布局(Staggered Grid Layout)。

优点

  • ViewHolder对视图复用;
  • 可以垂直滚动和水平滚动;
  • 支持多种布局;

列表布局RecyclerView

今天实现一个简单的RecyclerView,效果如下图所示:

在这里插入图片描述

一、创建RecyclerView并且在布局中绑定

   <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rlv_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 rlv1 = findViewById(R.id.rlv_1)

二、实现RecyclerView单个item的布局

```kotlin
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#cccccc"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingRight="20dp"
    android:paddingLeft="20dp">
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/fangyuan" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="小白"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_content"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:textSize="14sp"
            android:textColor="#666666"
            android:text="今天上班有摸鱼吗?"/>

    </LinearLayout>
    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:layout_marginTop="20dp"
        android:textColor="#999999"
        android:textSize="14sp"
        android:text="2024年7月9日"/>

</LinearLayout>

三、给RecyclerView写一个对应的适配器Adapter

1.创建自定义的ViewHolder
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var ivIcon: ImageView? = null
    var tvName: TextView? = null
    var tvContent: TextView? = null
    var tvTime: TextView? = null
    init {
        ivIcon = itemView.findViewById(R.id.iv_icon)
        tvName = itemView.findViewById(R.id.tv_name)
        tvContent = itemView.findViewById(R.id.tv_content)
        tvTime = itemView.findViewById(R.id.tv_time)
    }

}
2.继承Adapter,泛型使用我们自定义的ViewHolder
class MyAdapter(
): RecyclerView.Adapter<MyViewHolder>() {
}
3.重写Adapter的三个方法
onCreateViewHolder

顾名思义,创建我们自定义ViewHolder的实例,也就是将item的布局作为itemView。ViewHolder是列表中每个item。
inflate,三个参数,
第一个为加载的布局id;第二个为该布局的外部是否嵌套一层父布局,不用就是null;第三个,是否给加载的布局添加一个root的外层容器。

 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false)
        val myViewHolder = MyViewHolder(view)
        return myViewHolder
    }
getItemCount

item的条数

  override fun getItemCount(): Int {
        return nameList.size
    }
onBindViewHolder

通过onCreateViewHolder()绑定了布局之后,接下来对数据和布局里面的控件进行绑定。

   override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.tvName?.text = nameList[position]
        holder.tvContent?.text = contentList[position]
        holder.tvTime?.text = timeList[position]
    }

四、RecyclerView绑定布局和适配器

   private fun initData() {
        val nameList = ArrayList<String>()
        val contentList = ArrayList<String>()
        val timeList = ArrayList<String>()
        for (i in 0..19) {
            nameList.add("方正")
            contentList.add("送外卖第${i}天")
            timeList.add("${1}${i + 1}日")
        }
        val adapter = MyAdapter(this,nameList,contentList,timeList)
        val manager = LinearLayoutManager(this)
        rlv1?.layoutManager = manager
        rlv1?.adapter = adapter

    }

至此,已经实现了开头的列表效果。

五、RecyclerView单个item点击事件

1.创建监听接口
interface OnItemClickListener {
    fun onItemClick(position: Int)
}
2.Activity中实现接口并且传给Adapter
3.绑定事件中监听
holder.itemView.setOnClickListener{
            listener.onItemClick(position)
        }
4.当点击时,执行的是Activity中实现的方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值