RecycleView简介
增强版的ListView
能够实现横向滚动
RecyclerView是support-v7包中的新组件
是一个强大的滑动组件
与经典的ListView相比,同样拥有item回收复用的功能
RecyclerView封装了viewholder的回收复用
也就是说RecyclerView标准化了ViewHolder
编写Adapter面向的是ViewHolder
提供了一种插拔式的体验,高度的解耦
可以控制Item增删的动画,可以通过ItemAnimator这个类进行控制
样例效果
实现图书列表,点击一个图书,可查看详细图书信息
实现过程
1. 添加库引用
在build.gradle文件中添加依赖
dependencies { implementation 'androidx.recyclerview:recyclerview:1.1.0' }
2. 布局使用RecycleView控件
拉入一个recyclerView
或是直接编辑代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
3. 自定义适配器Adapter
创建一个book_item.xml作为每一本书籍的显示格式
其内容为
创建一个Book书籍类
package com.example.myapplication3 class Book(val name: String, val imageId: Int, val intro: String) { }
创建一个BookAdapter适配器
4. 实现内部类ViewHolder
class BookAdapter(val bookList: List<Book>): RecyclerView.Adapter<BookAdapter.ViewHolder>(){ inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){ val bookImage: ImageView = view.findViewById(R.id.book_image) val bookName: TextView = view.findViewById(R.id.book_name) val bookIntro: TextView = view.findViewById(R.id.book_intro) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { } override fun onBindViewHolder(holder: ViewHolder, position: Int) { } override fun getItemCount(): Int {
} }
5. 实现3个方法
在onBindViewHolder中实现页面的跳转
使用intent的putExtra字符串数据
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.book_item, parent, false) val viewHolder = ViewHolder(view) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val book = bookList[position] holder.bookImage.setImageResource(book.imageId) holder.bookName.text = book.name holder.bookIntro.text = book.intro holder.itemView.setOnClickListener{ val position = holder.adapterPosition val book = bookList[position] val intent = Intent(holder.itemView.context, DetailActivity::class.java) intent.putExtra("book_imageId", book.imageId.toString()) intent.putExtra("book_name", book.name) intent.putExtra("book_intro", book.intro) holder.itemView.context.startActivity(intent) } holder.bookImage.setOnClickListener{ //同上holder.itemView.setOnListener内容 } } override fun getItemCount(): Int = bookList.size
6. 使用布局和适配器
主页代码
MainActivity.kt
package com.example.myapplication3 import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { private val BookList = ArrayList<Book>() private val bookImageIds = intArrayOf( R.drawable.book1, R.drawable.book2, R.drawable.book3, R.drawable.book4, R.drawable.book5, R.drawable.book6, R.drawable.book7, R.drawable.book8, R.drawable.book9, R.drawable.book10 ) private val bookNames = arrayOf( "Java编程思想", "程序员修炼之道", "零基础入门学习Python", "Java从入门到精通" , "深入理解Java虚拟机", "算法导论", "Android开发艺术探索", "Android进阶之光" , "疯狂安卓讲义", "Android开发从入门到精通" ) private val bookIntros = arrayOf( "Java编程思想", "程序员修炼之道", "零基础入门学习Python", "Java从入门到精通" , "深入理解Java虚拟机", "算法导论", "Android开发艺术探索", "Android进阶之光" , "疯狂安卓讲义", "Android开发从入门到精通" ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) init() val layoutManager = LinearLayoutManager(this) recyclerView.layoutManager = layoutManager val adapter = BookAdapter(BookList) recyclerView.adapter = adapter } fun init(){ for(i in 0 until bookNames.size){ val book = Book(bookNames[i], bookImageIds[i], bookIntros[i]) BookList.add(book) } } }
详细页代码
DetailActivity.kt
使用intent的getStringExtra方法获取上一页数据
注意数据名称要
package com.example.myapplication3 import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_detail.* class DetailActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_detail) val name = intent.getStringExtra("book_name") val intro = intent.getStringExtra("book_intro") val imageId = intent.getStringExtra("book_imageId").toInt() imageView.setImageResource(imageId) book_introView.text = intro book_nameView.text = name } }
详情页布局随意
activity_detail.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".DetailActivity"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="vertical"> <TextView android:id="@+id/book_nameView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/book_introView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> </LinearLayout> </LinearLayout>
参考资料:
第一行代码 Android(第3版本)郭霖
GDUFE移动应用开发课程