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 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker2 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95273 小时前
Andorid Google 登录接入文档
android
黄林晴5 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab17 小时前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿20 小时前
Android MediaPlayer 笔记
android
Jony_20 小时前
Android 启动优化方案
android
阿巴斯甜21 小时前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇21 小时前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_1 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android