Skip to content

Instantly share code, notes, and snippets.

@tobioyelekan
Created May 24, 2020 13:53
Show Gist options
  • Save tobioyelekan/d4ae79eb72669f3cd157c43cb05feb54 to your computer and use it in GitHub Desktop.
Save tobioyelekan/d4ae79eb72669f3cd157c43cb05feb54 to your computer and use it in GitHub Desktop.
class CourseAdapter(private val viewModel: CourseViewModel) :
ListAdapter<Course, ViewHolder>(CourseDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(inflater.inflate(R.layout.course_item, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(viewModel, getItem(position))
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(viewModel: CourseViewModel, course: Course) {
with(itemView) {
courseTitle.text = course.courseTitle
courseThumbnail.loadImage(course.thumbnail)
modules.text = GeneralUtil.getNumber(course.numberOfModules, "Module")
courseLessons.text = GeneralUtil.getNumber(course.numberOfLessons, "Lesson")
startCourse.setOnClickListener {
viewModel.openCourseModule(
course.id,
course.courseTitle,
course.numberOfLessons
)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment