1.导入依赖包
在gradle(app)中:
dependencies {
......
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha06'
}
(版本地址:https://developer.android.com/jetpack/androidx/releases/recyclerview)
2.创建子部件视图
1.在layout文件夹下创建子视图 children_view.xmlchildren_view.xml
2.写子视图布局
<?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="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/children_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="24dp"/>
</LinearLayout>
children_view.png
3.创建RecyclerView的适配器
1.创建RecyclerViewAdapter类,继承自RecyclerView.Adapter
package com.ed.myapplication
import androidx.recyclerview.widget.RecyclerView
class RecyclerViewAdapter : RecyclerView.Adapter {}
image.png
报错:适配器需要类型参数<VH : RecyclerView.ViewHolder>.这里传的参数为【RecyclerView.ViewHolder】
2.创建一个类RecyclerViewHolder继承RecyclerView.ViewHolder。用于获取资源(也可创建在RecyclerViewAdapter内部)
class RecyclerViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
val children_textView: TextView =
itemView.findViewById(R.id.children_textView)
}
目录
3.将RecyclerViewHolder类传入RecyclerViewAdapter缺少的参数中,重写方法
class RecyclerViewAdapter : RecyclerView.Adapter<RecyclerViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
TODO("Not yet implemented")
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
TODO("Not yet implemented")
}
override fun getItemCount(): Int {
TODO("Not yet implemented")
}
}
4.在适配器中填写逻辑
package com.ed.myapplication
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class RecyclerViewAdapter : RecyclerView.Adapter<RecyclerViewHolder>() {
//1.模拟数据
private val dataList = arrayListOf(
"Tom Sachs SOLO",
"Andy Sachs RTC",
"Tony Sachs FDPE",)
//3.将子布局与适配器连接起来
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.children_view, parent, false)
//此处可以设置各种事件
return RecyclerViewHolder(view)
}
//4.将数据赋值到视图里
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
var dataCurrentView = dataList[position]
holder.children_textView.text = dataCurrentView
}
//2.返回数据大小
override fun getItemCount(): Int {
return dataList!!.size
}
}
4.更改主视图activity_mian.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_recycler_view"
app:layout_constraintTop_toBottomOf="@id/title"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
5.写主类逻辑。将适配器与主视图连接
package com.ed.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val layoutManager = LinearLayoutManager(this) //设置布局方式
main_recycler_view.layoutManager = layoutManager
val mainAdapter = RecyclerViewAdapter() //传入适配器
main_recycler_view.adapter = mainAdapter
}
}