导入依赖:
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.cardview:cardview:1.0.0"
activity:
package com.example.zkotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_weather.*
class WeatherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_weather)
val examplelist = generateDummyList(50)
recycler_view.adapter = ExampleAdapter(examplelist)
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.setHasFixedSize(true)
}
private fun generateDummyList(size: Int): List<ExampleItem> {
val list = ArrayList<ExampleItem>()
for (i in 0 until size) {
val drawable = when (i % 3) {
0 -> R.drawable.ic_android
1 -> R.drawable.ic_audio
2 -> R.drawable.ic_sun
else -> R.mipmap.ic_launcher
}
val item = ExampleItem(drawable, "Item $i", "Line 2 ")
list += item
}
return list
}
}
数据类:
package com.example.zkotlin
data class ExampleItem(val imageResuorce: Int, val text1: String, val text2: String)
适配器:
package com.example.zkotlin
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.example_item.view.*
class ExampleAdapter(private val exampleList: List<ExampleItem>) :
RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>() {
class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.image_view
val textView1: TextView = itemView.text_view_1
val textView2: TextView = itemView.text_view_2
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView =
LayoutInflater.from(parent.context).inflate(R.layout.example_item, parent, false)
return ExampleViewHolder(itemView)
}
override fun getItemCount(): Int {
return exampleList.size
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResuorce)
holder.textView1.text = currentItem.text1
holder.textView2.text = currentItem.text2
}
}
activity布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="4dp"
tools:listitem="@layout/example_item"
/>
</RelativeLayout>
item布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/image_view"
android:text="line 1"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_1"
android:layout_toEndOf="@id/image_view"
android:text="line 2" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
效果图: