实战:慕课App 学习Tab页数据请求与绑定
接口编写
private var retrofit: Retrofit = Retrofit.Builder()
.client(client)
.baseUrl("https://www.songyubao.com/")
.addConverterFactory(GsonConverterFactory.create()) //数据转换适配器
.build()
class ApiService{
@GET(value = "static/book/assets/study.json")
fun getStudy(): Call<List<Course>>
}
数据模型
data class Course(
val label: String,
val poster: String,
val progress: String,
val title: String
)
数据绑定
override fun onBindViewHolder(holder: StudyViewHolder, position: Int) {
val course = datas[position]
holder.itemView.item_course_title.text = course.title
holder.itemView.item_course_label.text = course.label
holder.itemView.item_course_progress.text = "已学.${course.progress}"
}
图片加载
在app/build.gradle中添加以下配置。使用Glide加载图片
apply plugin: 'kotlin-kapt'
android{
...
}
dependencies {
...
implementation 'com.github.bumptech.glide:glide:4.9.0'
kapt 'com.github.bumptech.glide:compiler:4.9.0'
}