Skip to content

Instantly share code, notes, and snippets.

View sandipsavaliya112's full-sized avatar
🎯
Focusing

Sandip Savaliya sandipsavaliya112

🎯
Focusing
View GitHub Profile
{
data: {
"unreadTerm": "JP20180512"
}
}
val items = ArrayList<BaseModel>()
items.add(Trip().apply { from = "NewYork"; to = "Chicago"})
items.add(Trip().apply { from = "London"; to = "Pennsylvania"})
items.add(Progress())
binding.recyclerView.adapter = Adapter(items){ holder, model ->
when(holder.binding){
is ItemTripBinding -> holder.binding.item = model as Trip
is ProgressBinding -> holder.binding.item = model as Progress
class Trip : BaseModel(R.layout.item_trip){
var from : String = ""
var to : String = ""
fun onClick(view: View){
Toast.makeText(view.context,"Trip => from : $from, to : $to", Toast.LENGTH_SHORT).show()
}
}
class Adapter<T : BaseModel>(val items: List<T>, val callback : (GenericVH, T)-> Unit) : RecyclerView.Adapter<Adapter.GenericVH>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GenericVH {
return GenericVH(DataBindingUtil.inflate(LayoutInflater.from(parent.context), viewType, parent, false))
}
override fun getItemCount() = items.size
override fun getItemViewType(position: Int) = items[position].resId
override fun getItemViewType(position: Int) = items[position].resId
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GenericVH {
return GenericVH(DataBindingUtil.inflate(LayoutInflater.from(parent.context), viewType, parent, false))
}
open class BaseModel(@LayoutRes var resId: Int) : BaseObservable()
class GenericVH(val binding : ViewDataBinding): RecyclerView.ViewHolder(binding.root)