Created
April 21, 2021 12:41
-
-
Save milhauscz/1e8bc50799e51ee9b5b1749b1128162d to your computer and use it in GitHub Desktop.
Example implementation of MaterialSpinnerAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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