Last active
January 1, 2021 09:52
-
-
Save swarawan/6f80ae90751a000633f855dc1369e88a to your computer and use it in GitHub Desktop.
Revisions
-
swarawan revised this gist
Jan 1, 2021 . 1 changed file with 0 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +0,0 @@ -
swarawan revised this gist
Jan 1, 2021 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ /** * How to */ private val studentDataAdapter by lazy { GeneralRecyclerView<SchoolInformationEntity>( diffCallback = diffCallback, holderResId = R.layout.item_student_confirmation, onBind = { data, view -> setupStudentData(data, view) } ) } -
swarawan created this gist
Jan 1, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ class DiffCallback : DiffUtil.Callback() { private var oldList: List<Any> = emptyList() private var newList: List<Any> = emptyList() fun setList(oldList: List<Any>, newList: List<Any>) { this.oldList = oldList this.newList = newList } override fun getOldListSize(): Int = oldList.size override fun getNewListSize(): Int = newList.size override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { return oldList[oldItemPosition] == newList[newItemPosition] } override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { return oldList[oldItemPosition] == newList[newItemPosition] } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,88 @@ class GeneralRecyclerView<T : Any>( private val diffCallback: DiffCallback, @LayoutRes private val holderResId: Int, @IdRes private val specificResViewId: Int? = null, private val onBind: (T, View) -> Unit, private val itemListener: (T, pos: Int, View) -> Unit = { _, _, _ -> kotlin.run {} }, private val specificViewListener: (T, pos: Int, View) -> Unit = { _, _, _ -> kotlin.run {} } ) : RecyclerView.Adapter<GeneralRecyclerView.ViewHolder<T>>() { private val data = mutableListOf<T>() override fun onCreateViewHolder(container: ViewGroup, position: Int): ViewHolder<T> { val layoutInflater = LayoutInflater.from(container.context) val view = layoutInflater.inflate(holderResId, container, false) var specificView: View? = null specificResViewId?.let { specificView = view.findViewById(it) } return ViewHolder(view, specificView) } override fun onBindViewHolder(viewHolder: ViewHolder<T>, position: Int) { viewHolder.bindView( data[viewHolder.adapterPosition], onBind, itemListener, specificViewListener ) } override fun getItemCount(): Int = data.size fun setData(datas: List<T>) { calculateDiff(datas) } fun addData(newDatas: List<T>) { val list = ArrayList(this.data) list.addAll(newDatas) calculateDiff(list) } fun refreshList() { calculateDiff(data) } fun getData(): List<T> = this.data fun clearData() { calculateDiff(emptyList()) } private fun calculateDiff(newData: List<T>) { diffCallback.setList(data, newData) val result = DiffUtil.calculateDiff(diffCallback) with(data) { clear() addAll(newData) } result.dispatchUpdatesTo(this) } class ViewHolder<T : Any>(view: View, private val specificView: View? = null) : RecyclerView.ViewHolder(view) { private var itemPosition: Int = 0 fun bindView( data: T, onBind: (T, View) -> Unit, itemListener: (T, pos: Int, View) -> Unit, specificViewListener: (T, pos: Int, View) -> Unit ) { itemPosition = adapterPosition with(itemView) { onBind.invoke(data, this) setOnClickListener { itemListener.invoke(data, adapterPosition, this) } } specificView?.setOnClickListener { specificViewListener.invoke(data, adapterPosition, itemView) } } } }