创建子项布局对应的实体类
class Fruit(val fruitPicetureId :Int,val fruitName:String) {
}
在主界面main.xml中加入RecyclerView控件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/mainConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
编写子项布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<ImageView
android:id="@+id/vfruitImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/vfruitNmae"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp" />
</LinearLayout>
编写适配器(其中包含了外层view视图的点击事件和子项图片的点击事件)
package com.example.recyclerviewtest
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
class FruitAdapter(val fruitList : List<Fruit>) :
RecyclerView.Adapter<FruitAdapter.ViewHolder>() {
//界面控件绑定
inner class ViewHolder(view:View):RecyclerView.ViewHolder(view){
val fruitImage : ImageView = view.findViewById(R.id.vfruitImage)
val fruitName : TextView = view.findViewById(R.id.vfruitNmae)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val fruit = fruitList[position]
holder.fruitImage.setImageResource(fruit.fruitPicetureId)
holder.fruitName.text = fruit.fruitName
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.fruit_item_vertical,parent,false)
//实现点击事件响应,RecyclerView中的灵活性更大,可以为外层布局和子项内部元素设立不同的相应事件
val viewHolder = ViewHolder(view)
viewHolder.itemView.setOnClickListener {
val position = viewHolder.adapterPosition
val fruit = fruitList[position]
Toast.makeText(parent.context,"you clicked view ${fruit.fruitName}",Toast.LENGTH_SHORT).show()
}
viewHolder.fruitImage.setOnClickListener {
val position = viewHolder.adapterPosition
val fruit = fruitList[position]
Toast.makeText(parent.context,"you clicked image ${fruit.fruitName}",Toast.LENGTH_SHORT).show()
}
//return ViewHolder(view)
return viewHolder
}
override fun getItemCount() = fruitList.size
}
在MainActivity中创建数据源并完成调用
package com.example.recyclerviewtest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import java.lang.StringBuilder
class MainActivity : AppCompatActivity() {
//数据源列表
private val fruitList = ArrayList<Fruit>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//控件绑定
val recyclerView : RecyclerView = findViewById(R.id.recyclerView)
//初始化水果数据
initFruits()
//因为RecyclerView的布局排列是通过layoutManager管理的,所以要把LayoutManager和适配器都配给它
//val layoutManager = LinearLayoutManager(this)
//实现横向布局
//layoutManager.orientation = LinearLayoutManager.HORIZONTAL
//实现瀑布流布局
val layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
recyclerView.layoutManager = layoutManager
val adapter = FruitAdapter(fruitList)
recyclerView.adapter = adapter
}
//初始化水果信息
private fun initFruits(){
repeat(10){
fruitList.add(Fruit(R.drawable.apple_pic,getRandomLengthString("Apple")))
fruitList.add(Fruit(R.drawable.pear_pic,getRandomLengthString("Pear")))
fruitList.add(Fruit(R.drawable.watermelon_pic,getRandomLengthString("Watermelon")))
}
}
//产生长度不等的水果名字
private fun getRandomLengthString(fruitName:String):String{
val n = (1..10).random()
val builder = StringBuilder()
repeat(n){
builder.append(fruitName)
}
return builder.toString()
}
}