Skip to content

Commit

Permalink
Merge pull request #1 from SiebelsTim/generics
Browse files Browse the repository at this point in the history
Add generic type parameter to SimpleRecyclerViewAdapter
  • Loading branch information
OrhanTozan authored Mar 22, 2018
2 parents 68f70c4 + d083866 commit 023752b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

abstract class SimpleRecyclerViewAdapter<VH : SimpleRecyclerViewAdapter.SimpleViewHolder>
abstract class SimpleRecyclerViewAdapter<T, VH : SimpleRecyclerViewAdapter.SimpleViewHolder<T>>
(
private val models: List<Any>
private val models: List<T>
) : RecyclerView.Adapter<VH>()
{
abstract fun getViewLayoutResource(): Int
Expand All @@ -24,8 +24,8 @@ abstract class SimpleRecyclerViewAdapter<VH : SimpleRecyclerViewAdapter.SimpleVi

override fun onBindViewHolder(holder: VH, position: Int) = holder.bind(models[position])

abstract class SimpleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
abstract class SimpleViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView)
{
abstract fun bind(model: Any)
abstract fun bind(model: T)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import kotlinx.android.synthetic.main.viewholder_animal.view.*
class AnimalAdapter
(
animals: ArrayList<Animal>
) : SimpleRecyclerViewAdapter<SimpleRecyclerViewAdapter.SimpleViewHolder>(animals)
) : SimpleRecyclerViewAdapter<Animal, AnimalAdapter.AnimalViewHolder>(animals)
{
override fun getViewLayoutResource() = R.layout.viewholder_animal

override fun onInstantiateViewHolder(itemView: View) = AnimalViewHolder(itemView)

class AnimalViewHolder(itemView: View) : SimpleViewHolder(itemView)
class AnimalViewHolder(itemView: View) : SimpleViewHolder<Animal>(itemView)
{
override fun bind(model: Any)
override fun bind(model: Animal)
{
model as Animal
itemView.name.text = model.name
itemView.amountLegs.text = model.amountLegs.toString()
itemView.lifeExpectancy.text = model.lifeExpectancy.toString()
}
}
}
}

0 comments on commit 023752b

Please sign in to comment.