# RecyclerView 高级控件 & 案例实践
RecyclerView 是 Android 及其重要的一个高级 UI 控件,使用频率及其的高,APP 的 90% 以上的页面都会使用的到。由于其出色的性能和插拔式的架构设计,被全世界广大开发者一致称赞。
![RecyclerView族谱]()
# LinerLayoutManager 列表布局
| 纵向列表 | 横向列表 | 
|---|
| ![linear_vretical]() | ![linear_layout_horizontal]() | 
# 纵向列表
|  | recyclerView.layoutManager =LinearLayoutManager(context, VERTICAL, false) | 
|  | recyclerView.adapter = MyAdapter() | 
# 横向列表
|  | recyclerView.layoutManager =LinearLayoutManager(context, HORIZONTAL, false) | 
|  | recyclerView.adapter = MyAdapter() | 
# 数据源适配器
|  | inner class MyAdapter : RecyclerView.Adapter<ViewHolder>() { | 
|  |          | 
|  |         override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | 
|  |           val view =LayoutInflater.from(context).inflate(R.layout.item_view_staggered, parent, false)   | 
|  |           return ViewHolder(view) | 
|  |         } | 
|  |  | 
|  |          | 
|  |         override fun getItemCount(): Int { | 
|  |             return 20 | 
|  |         } | 
|  |  | 
|  |          | 
|  |         override fun onBindViewHolder(holder: ViewHolder, position: Int) { | 
|  |             holder.itemView.item_head.setImageResource(R.drawable.icon_jetpack) | 
|  |             holder.itemView.item_name.text = "【${position}】移动端架构师体系课" | 
|  |             holder.itemView.item_message.text = | 
|  |                 "移动开发“两极分化”,没有差不多的“中间层,唯有尽早成长为架构师,你的职业道路才能走的更远更稳" | 
|  |         } | 
|  |     } | 
|  |  | 
|  |      | 
|  |     inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | 
|  |  | 
|  |     } | 
# GridLayoutManager 网格布局
![grid_layout_vertical]()
|  | recyclerView.layoutManager = GridLayoutManager(context, 2) | 
|  | recyclerView.adapter = MyAdapter() | 
# StaggeredGridLayoutManager 瀑布流布局
|  | recyclerView.layoutManager =StaggeredGridLayoutManager(2, VERTICAL) | 
|  | recyclerView.adapter = MyAdapter() | 
![grid_layout_staggered]()
# Kotlin 扩展插件
可以直接使用布局中的控件 id 来操作 view 控件,不用再 findViewById。大大提高工作效率,减少模板代码量
需要在根目录下的 build.gradle 添加 kotlin-android-extensions 插件
|  | buildscript { | 
|  |     ext.kotlin_version = "1.3.72" | 
|  |     repositories { | 
|  |         google() | 
|  |         jcenter() | 
|  |     } | 
|  |     dependencies { | 
|  |         classpath "com.android.tools.build:gradle:4.1.0" | 
|  |         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | 
|  |         classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" | 
|  |     } | 
|  | } | 
需要在 app/build.gradle 应用 kotlin-android-extensions 插件
|  | plugins { | 
|  |     id 'com.android.application' | 
|  |     id 'kotlin-android' | 
|  |     id 'kotlin-android-extensions' | 
|  | } |