Skip to content

Instantly share code, notes, and snippets.

@milhauscz
Created April 21, 2021 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milhauscz/1e8bc50799e51ee9b5b1749b1128162d to your computer and use it in GitHub Desktop.
Save milhauscz/1e8bc50799e51ee9b5b1749b1128162d to your computer and use it in GitHub Desktop.
Example implementation of MaterialSpinnerAdapter
class ExampleSpinnerArrayAdapter(context: Context, objects: List<ExampleItem>) : MaterialSpinnerAdapter<ExampleItem>(context, objects) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
// implement viewholder pattern
val view: View
// automatically generated binding class
val binding: ExampleItemBinding
if (convertView == null) {
// view is new - we have to inflate the binding object and set it as a tag
binding = ExampleItemBinding.inflate(inflater, parent, false)
view = binding.root
view.tag = binding
} else {
// reusing existing view - retrieve binding from the tag
view = convertView
binding = view.tag as ExampleItemBinding
}
// update UI - set the binding variables
binding.item = getItem(position)
binding.selected = position == selectedItemPosition
return view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment