Skip to content

Instantly share code, notes, and snippets.

@sockeqwe
Created July 22, 2019 08:19
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sockeqwe/30ab1b5695f7f4718f3d85fa245909bb to your computer and use it in GitHub Desktop.
Save sockeqwe/30ab1b5695f7f4718f3d85fa245909bb to your computer and use it in GitHub Desktop.
AdapterDelegates Kotlin DSL
/**
* Let's say we want to display a list of Animals in a RecyclerView.
* The list can contain items of type Dog, Cat and Snake.
*/
// AdapterDelegate for Dog.
fun dogAdapterDelegate(picasso : Picasso) = adapterDelegate<Dog, Animal>(R.layout.item_dog){ // Generics Types means this AdapterDelegate is used if item is instanceof Dog (whole list is List<Anmial>)
// this block is run once in onCreateViewHolder. Think of it as an intializer for a ViewHolder.
val name = findViewById(R.id.name)
val image = findViewById(R.id.image)
bind {
// This method is called every time onBindViewHolder is called
name.text = item.text // item is set to the current bound item from List<Animal>. Item is of type Dog.
picasso.load(item.image.url).into(image)
}
}
// AdapterDelegate for Cat which uses kotlin android extensions by using adapterDelegateLayoutContainer() instead of adapterDelegate()
fun catAdapterDelegate( catClicked : (Cat) -> Unit) = adapterDelegateLayoutContainer<Cat, Animal>(R.layout.item_cat){ // Generics Types means this AdapterDelegate is used if item is instanceof Cat (whole list is List<Anmial>)
// this block is run once in onCreateViewHolder. Think of it as an intializer for a ViewHolder.
name.setOnClickListener { // name is imported from kotlinx.android.synthetic.main.item_cat.* if you use kotlin android extensions
catClicked(item) // item is set to the current bound item from List<Animal>. Item is of type Cat.
}
bind {
// This method is called every time onBindViewHolder is called
name.text = item.name // item is bound automatically to the current item in the List<Animal>. Item is of type Cat.
catBreed.text = item.breed
}
}
// AdapterDelegate for Snake
fun snakeAdapterDelegate() = adapterDelegateLayoutContainer(R.layout.item_snake) {
bind {
name.text = item.name
age.text = item.age.toString()
age.setTextColor(
getColor( // convenience method / shortcut for context.getColor(id). It's part of the DSL.
if (item.age < 3)
R.color.snake_young
else
R.color.snake_old
)
)
}
}
class AnimalsActivity : Activity() {
overrid onCreate(b : Bundle?){
super(b)
setContentView(R.layout.activity_animal)
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
// Setup the adapter with the AdapterDelegates we have defined above.
val adapter = ListDelegationAdapter(
dogAdapterDelegate( Picasso.get() ),
catAdapterDelegate( this::onCatClicked ),
snakeAdapterDelegate()
)
recyclerView.setAdapter(adapter)
}
private fun onCatClicked(cat : Cat) {
...
}
}
@sockeqwe
Copy link
Author

The DSL has been added in AdapterDelegates 4.1.0

https://github.com/sockeqwe/AdapterDelegates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment