Skip to content

Instantly share code, notes, and snippets.

@jvalentik
Created February 25, 2018 12:57
Show Gist options
  • Save jvalentik/bbda72c16f030cb336cfa8464d022cfd to your computer and use it in GitHub Desktop.
Save jvalentik/bbda72c16f030cb336cfa8464d022cfd to your computer and use it in GitHub Desktop.
Android ViewHolder pattern
class CategoryAdapter(private val context: Context, private val categories: List<Category>) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val categoryView : View
val holder : ViewHolder
if (convertView == null) {
categoryView = LayoutInflater.from(this.context).inflate(R.layout.category_list_item, null)
holder = ViewHolder()
holder.categoryImage = categoryView.findViewById(R.id.categoryImage)
holder.categoryName = categoryView.findViewById(R.id.categoryTitle)
categoryView.tag = holder
} else {
holder = convertView.tag as ViewHolder
categoryView = convertView
}
val category = this.categories[position]
val resourceId = this.context.resources.getIdentifier(category.image, "drawable", this.context.packageName)
holder.categoryImage?.setImageResource(resourceId)
holder.categoryName?.text = category.title
return categoryView
}
override fun getItem(position: Int): Category {
return this.categories[position]
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getCount(): Int {
return this.categories.count()
}
private class ViewHolder {
var categoryImage : ImageView? = null
var categoryName : TextView? = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment