昨日主要学习了ROOM和协程,今天补充一些协程的知识,并且学习RecyclerView, Adapter和WorkManager。
RecyclerView#
在app中,有很多地方都需要用到列表来展示多个item,android提供了RecyclerView来支持大型列表,并且RecyclerView支持滑动,在显示有限个数量的item后,剩下的item需要滑动来获取。今天在昨天的睡眠质量的app的基础上将首页的历史记录改成使用RecyclerView来展示。
Adapter#
适配器模式#
在Android的学习中,有很多的设计模式可以学习,这次来到了适配器模式。
- 定义:用包装类来包装不兼容的对象,将一个类的接口转换成客户希望的另外一个接口。
举一个所有博文中都会举的一个例子,比如说在国际旅行的时候,在不同的国家充电器的规格都不一样,如果想要充电的话就需要转换器,那么这个转换器就相当于一个中间层,这个中间层就是Adapter层,通过这层来进行一个接口的转换达到兼容的目的。
关于具体的思想在下面的慢慢使用中体会
RecyclerView的使用#
Step 1: 将所需要使用RecyclerView的布局添加RecyclerView
1
2
3
4
5
6
7
8
9
|
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/sleep_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/clear_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/stop_button"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
|
Step 2:创建单个的Item的View,这里是TextView
1
2
3
4
5
6
|
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="24sp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:layout_width="match_parent" android:layout_height="wrap_content" />
|
Step 3: 创建ViewHolder和Adapter,这里有必要说一下ViewHolder,ViewHolder用来绑定Adapter,主要是绑定Adapter和Adapter的布局文件,以及将item中的数据填充到Adapter中的布局文件,具体看代码吧。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.example.android.trackmysleepquality.sleeptracker
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 com.example.android.trackmysleepquality.R
import com.example.android.trackmysleepquality.convertDurationToFormatted
import com.example.android.trackmysleepquality.convertNumericQualityToString
import com.example.android.trackmysleepquality.database.SleepNight
class SleepNightAdapter: RecyclerView.Adapter<SleepNightAdapter.ViewHolder>() {
var data = listOf<SleepNight>()
set(value) {
field = value
notifyDataSetChanged()
}
override fun getItemCount() = data.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position]
holder.bind(item)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder.from(parent)
}
class ViewHolder private constructor(itemView: View) : RecyclerView.ViewHolder(itemView){
val sleepLength: TextView = itemView.findViewById(R.id.sleep_length)
val quality: TextView = itemView.findViewById(R.id.quality_string)
val qualityImage: ImageView = itemView.findViewById(R.id.quality_image)
fun bind(item: SleepNight) {
val res = itemView.context.resources
sleepLength.text = convertDurationToFormatted(item.startTimeMilli, item.endTimeMilli, res)
quality.text = convertNumericQualityToString(item.sleepQuality, res)
qualityImage.setImageResource(when (item.sleepQuality) {
0 -> R.drawable.ic_sleep_0
1 -> R.drawable.ic_sleep_1
2 -> R.drawable.ic_sleep_2
3 -> R.drawable.ic_sleep_3
4 -> R.drawable.ic_sleep_4
5 -> R.drawable.ic_sleep_5
else -> R.drawable.ic_sleep_active
})
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater
.inflate(R.layout.list_item_sleep_night, parent, false)
return ViewHolder(view)
}
}
}
}
|
Step 4:绑定RecyclerView和Adapter,绑定就比较简单,RecyclerView有adapter属性,然后创建一个Adpater赋值即可,如果数据有概统则更新adapter的数据。
1
2
3
4
5
6
7
8
|
val adapter = SleepNightAdapter()
binding.sleepList.adapter = adapter
sleepTrackerViewModel.nights.observe(viewLifecycleOwner, Observer {
it?.let {
adapter.data = it
}
})
|
整体就完成了RecyclerView的创建,虽然能够使用,但是对于其原理还是一知半解,所以下面进行RecyclerView的原理剖析。
RecyclerView原理#
这里是直接看的博文,说的还是很清楚,下面在总结一下,同样也将之前有些模糊的地方在学习一下。
https://www.jianshu.com/p/c52b947fe064