Created
January 12, 2021 09:12
-
-
Save hi-manshu/98df5a8a7e617b81aea18dfb279b7010 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Created by Himanshu Singh on 27-10-2020. | |
**/ | |
abstract class IBaseRecyclerViewAdapter<Item : IRecyclerItemViewModel?>(var items: MutableList<Item>) : RecyclerView.Adapter<BaseRecyclerViewAdapter.BaseBindingViewHolder>() { | |
private var position = 0 | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseBindingViewHolder { | |
// here the viewType is the actual layoutId to make it generic | |
val layoutInflater = LayoutInflater.from(parent.context) | |
val binding = | |
DataBindingUtil.inflate(layoutInflater, viewType, parent, false) as ViewDataBinding | |
val viewHolder = BaseBindingViewHolder(binding) | |
position = viewHolder.adapterPosition | |
registerItemClick(viewHolder, viewType, viewHolder.adapterPosition) | |
return viewHolder | |
} | |
override fun onBindViewHolder(holder: BaseBindingViewHolder, position: Int) { | |
holder.bindData(items[position]) | |
} | |
override fun getItemViewType(position: Int): Int { | |
return items[position]!!.getLayoutId() | |
} | |
fun getItem(holder: BaseBindingViewHolder): Item { | |
return items[holder.adapterPosition] | |
} | |
fun updateData(data: List<Item>) { | |
this.items.clear() | |
this.items.addAll(data) | |
notifyDataSetChanged() | |
} | |
fun addData(data: List<Item>) { | |
this.items.addAll(data) | |
notifyDataSetChanged() | |
} | |
fun addItem(item: Item) { | |
this.items.add(item) | |
notifyItemChanged(itemCount) | |
} | |
fun updateItem(item: Item, position: Int) { | |
if (this.items.size >= position) { | |
this.items[position] = item | |
notifyItemChanged(position) | |
} | |
} | |
fun removeItem(position: Int) { | |
this.items.removeAt(position) | |
notifyItemRemoved(position) | |
} | |
fun getData(): ArrayList<Item> { | |
return this.items.toList() as ArrayList<Item> | |
} | |
fun addData(data: Item) { | |
this.items.add(this.items.size - 1, data) | |
notifyItemChanged(this.items.size - 1) | |
} | |
abstract fun registerItemClick(holder: BaseBindingViewHolder, viewType: Int, position: Int) | |
override fun getItemCount(): Int { | |
return items.count() | |
} | |
class BaseBindingViewHolder internal constructor(var binding: ViewDataBinding) : | |
RecyclerView.ViewHolder(binding.root) { | |
fun bindData(item: Any?) { | |
(item as IRecyclerItemViewModel).let { | |
for (pair in it.getBindingPairs()) { | |
binding.setVariable(pair.first, pair.second) | |
} | |
} | |
binding.executePendingBindings() | |
} | |
} | |
// this method is use to register the listeners in viewBinding | |
open fun getListener(): Any? { | |
return null | |
} | |
open fun getPosition(): Int { | |
return position | |
} | |
} |
This file contains hidden or 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
interface IRecyclerItemViewModel{ | |
@LayoutRes | |
fun getLayoutId(): Int | |
fun getBindingPairs(): List<Pair<Int, Any>> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment