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只会调用限定次数。
相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
千里马学框架2 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台2 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone2 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
致远ccc2 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁2 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
ttyyttemo2 天前
Kotlin 协程中的 Job 结构化并发与取消
android
孙启超2 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust