Skip to content

Instantly share code, notes, and snippets.

View sandipsavaliya112's full-sized avatar
🎯
Focusing

Sandip Savaliya sandipsavaliya112

🎯
Focusing
View GitHub Profile
class GenericVH(val binding : ViewDataBinding): RecyclerView.ViewHolder(binding.root)
open class BaseModel(@LayoutRes var resId: Int) : BaseObservable()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GenericVH {
return GenericVH(DataBindingUtil.inflate(LayoutInflater.from(parent.context), viewType, parent, false))
}
override fun getItemViewType(position: Int) = items[position].resId
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
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()
}
}
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
{
data: {
"unreadTerm": "JP20180512"
}
}