什么是RecyclerView?
是比ListView更强大的Android滚动控件
如何使用RecyclerView?
1、首先我们需要在项目结构中的app路径下build.gradle文件中,导入RecyclerView的依赖,这里不做过多赘述,导入依赖成功后,不要忘记sync
2、然后就可以在布局中使用RecyclerView控件了
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/todolist_recycler"
android:layout_width="match_parent"
android:layout_height="800dp">
</androidx.recyclerview.widget.RecyclerView>
3、在写好RecyclerView后,我们需要为其每一个Item指定一个我们自定义的布局,以确保在RecyclerView中展示的item都是以我们想要的形式进行展示
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="110dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center">
<CheckBox
android:id="@+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:scaleX="1.7"
android:scaleY="1.7" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在这里添加您的事件"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="事件需要被提醒的时间"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
4、接下来我们需要为RecyclerView 准备一个Adapter(适配器),RecyclerView的适配器一套标准的写法,我们可以根据自己的需要,重写三个方法
class ToDoListAdapter(private val eventList: List<Event>) :
RecyclerView.Adapter<ToDoListAdapter.ViewHolder>() {
//定义一个继承自RecyclerView.ViewHolder的内部类ViewHolder,然后再主构造函数ViewHolder中传入一个参数view,这个view
//是RecyclerView子项的最外层布局,我们可以使用这个view通过findViewById()来获取我们所需要的实例
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val eventContent: TextView = view.findViewById(R.id.content)
val eventTime: TextView = view.findViewById(R.id.time)
}
//在onCreateViewHolder方法中,用于创建ViewHolder实例,在onCreateViewHolder中,我们把item的布局传进来,然后创建一个ViewHolder的实例,
//并把加载出来的布局传入构造函数中,最后返回ViewHoldeer的实例
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.todolist_item,parent,false)
return ViewHolder(view)
}
//onBindViewHolder()方法用于对RecyclerView子项的数据进行赋值,会在每个子项滚动到屏幕内的时候执行,我们可以通过position参数得到当前项的
//的实例,然后将我们业务所需的数据赋值到ViewHolder当中的控件中
override fun onBindViewHolder(holder: ToDoListAdapter.ViewHolder, position: Int) {
val event = eventList[position]
holder.eventContent.text = event.content
holder.eventTime.text = event.time
}
//获取RecyclerView一共有多少子项,并直接返回长度
override fun getItemCount(): Int {
return eventList.size
}
}
5、最后我们就可以在MainActivity中使用RecyclerView了
class MainActivity : AppCompatActivity() {
private val eventList = ArrayList<Event>()
@SuppressLint("ResourceType")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val layoutManager = LinearLayoutManager(this)
val eventAdapter = ToDoListAdapter(eventList)
recyclerView.adapter = eventAdapter
}
private fun initEvent(){
repeat(2){
eventList.add(Event("Meeting","20220606"))
eventList.add(Event("Eat","20210123"))
eventList.add(Event("Drive","20321512"))
}
}
}
6、效果展示