recyclerView(kotlin)

recyclerView的优点

  1. 使用viewHolder+RecycledViewPool的方式复用资源,提高性能
  2. 利用LayoutManager,可根据不同需求使用不同的布局,且可以方便使用对应布局提供的方法,如快速定位item等。
  3. RecyclerView 提供了一个 ItemAnimator 接口,可以轻松地实现添加、删除和移动项的动画。DefaultItemAnimator 是其默认实现,开发者可以自定义自己的动画。
  4. 多类型视图支持,RecyclerView 可以轻松支持不同类型的视图,通过在 Adapter 中重写 getItemViewType 方法来实现多个视图类型。
  5. 事件处理,ItemTouchHelper:RecyclerView 提供了 ItemTouchHelper 类来实现拖拽和滑动删除功能。

简单使用

  1. 数据实体类(根据需求自定义)
kotlin 复制代码
data class Item(val title: String, val description: String)
  1. 创建单项布局(每一项的样式)
xml 复制代码
<!-- res/layout/item_layout.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />
</LinearLayout>
  1. RecyclerView 适配器Adapter(设置每一项的显示内容)
kotlin 复制代码
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class ItemAdapter(private val itemList: List<Item>) : RecyclerView.Adapter<ItemAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title: TextView = itemView.findViewById(R.id.title)
        val description: TextView = itemView.findViewById(R.id.description)
    }

    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 item = itemList[position]
        holder.title.text = item.title
        holder.description.text = item.description
    }

    override fun getItemCount(): Int {
        return itemList.size
    }
}
  1. activity(给recyclerView设置adapter和layoutManager')
kotlin 复制代码
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView: RecyclerView = findViewById(R.id.activity_recycler_view)

        // 创建数据
        val itemList = listOf(
            Item("Title 1", "Description 1"),
            Item("Title 2", "Description 2"),
            Item("Title 3", "Description 3")
        )

        // 设置 LayoutManager
        recyclerView.layoutManager = LinearLayoutManager(this)

        // 设置 Adapter
        recyclerView.adapter = ItemAdapter(itemList)
    }
}
  1. activity的布局文件
xml 复制代码
<!-- res/layout/activity_recycler_view.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

效果

原理

  1. RecycledViewPool,在item超出显示后会自动将viewHolder回收,在获取viewHolder时,会先尝试从池里拿,拿不到才会调用用户重写的onCreateViewHolder,所以onBindViewHolder有多少个item就会调用至少多少次,而onCreateViewHolder只会调用限定次数。
相关推荐
是立不是利13 分钟前
大文件导致请求被拒的连锁反应
开发语言·前端·javascript
野生技术架构师31 分钟前
1200 道 JAVA 面试题(各大企业常见面试题及答案)
java·开发语言
“AI国潮设计-小江”41 分钟前
【Python/SDXL实战】潮汕国潮IP视觉落地:普宁美食猫IP & 创意甜品设计(附ComfyUI工作流思路)
开发语言·人工智能·python·prompt·aigc
邪修king1 小时前
Re:Linux系统篇(二十六):文件系统(二):Ext 文件系统底层详解:从 inode、块组到软硬链接,结合 Windows 讲透文件管理本质
android·java·linux
Yyyyyy~1 小时前
【java】类和对象
java·开发语言
草莓熊Lotso1 小时前
【Redis 进阶】主从复制深度解析:从配置落地到 PSYNC 同步原理
linux·开发语言·网络·数据库·redis·缓存·php
又见情义1 小时前
RK3568 Android 13 版本号管理实战:基于 ROCKCHIP_BUILD_NUMBER 的定制化方案
android
Joy T1 小时前
Spring AI 2.0 进阶入门:Workflow、Routing、Task State 与可控 Agent
开发语言·人工智能·workflow·routing·springai·orchestrator·evaluator
JMchen1 小时前
第 9 篇|网络请求基础 —— 从 HttpURLConnection 到 OkHttp
kotlin·android studio
重生之小比特1 小时前
【Java SE】抽象类和接口
java·开发语言