Created
April 17, 2019 08:12
-
-
Save krzdabrowski/e497f51e9ffe4b50cc8625b49f5b7db9 to your computer and use it in GitHub Desktop.
This file contains 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 GenericAdapter<T>(private val context: Context, private val items: List<T>) | |
: RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
return when { | |
items.all { it is Rocket } -> RocketViewHolder(LayoutInflater.from(context).inflate(R.layout.item_rocket, parent, false)) | |
items.all { it is Flight } -> FlightViewHolder(LayoutInflater.from(context).inflate(R.layout.item_flight, parent, false)) | |
else -> EventViewHolder(LayoutInflater.from(context).inflate(R.layout.item_event, parent, false)) | |
} | |
} | |
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | |
(holder as Binder<T>).bind(items[position]) | |
} | |
override fun getItemCount(): Int = items.size | |
internal interface Binder<T> { | |
fun bind(data: T) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment