# 实战:慕课 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 加载图片
更多关于 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' |
| } |