Last active
June 28, 2019 15:27
-
-
Save Papashkin/11991bf75717a7d839718836571a2dc7 to your computer and use it in GitHub Desktop.
Adapter realization based on RecyclerView.Adapter using DiffUtil callback
This file contains 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 ITestBaseAdapter<T : Any> { | |
fun add(item: T) | |
fun addAll(items: List<T>) | |
fun addTo(position: Int = 0, item: T) | |
fun remove(position: Int) | |
fun getList(): List<T> | |
fun update(newList: List<T>, callback: ApolloBaseDiffUtilCallback<T>) | |
fun clear() | |
} |
This file contains 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
abstract class TestBaseAdapter<VH : TestBaseViewHolder> : RecyclerView.Adapter<VH>(), ITestBaseAdapter<Any> { | |
private val items: MutableList<Any> = mutableListOf() | |
override fun add(item: Any) { | |
items += item | |
notifyItemInserted(items.lastIndex) | |
} | |
override fun addAll(items: List<Any>) { | |
if (items.isEmpty()) return | |
val startPosition = this.items.lastIndex + 1 | |
for (item in items) { | |
this.items += item | |
} | |
notifyItemRangeInserted(startPosition, items.size) | |
} | |
override fun addTo(position: Int, item: Any) { | |
items.add(position, item) | |
notifyItemChanged(position) | |
} | |
override fun remove(position: Int) { | |
items.removeAt(position) | |
notifyItemRemoved(position) | |
} | |
override fun clear() { | |
val removedSize = items.size | |
items.clear() | |
notifyItemRangeRemoved(0, removedSize) | |
} | |
override fun update(newList: List<Any>, callback: ApolloBaseDiffUtilCallback<Any>) { | |
callback.setLists(items, newList) | |
val diffResult = DiffUtil.calculateDiff(callback) | |
clear() | |
addAll(newList) | |
diffResult.dispatchUpdatesTo(this) | |
} | |
override fun getList(): List<Any> = items | |
override fun getItemCount(): Int = items.size | |
protected fun inflate(parent: ViewGroup, @LayoutRes viewType: Int): View = | |
LayoutInflater.from(parent.context).inflate(viewType, parent, false) | |
override fun onBindViewHolder(holder: VH, position: Int) { | |
holder.bind(items[position], position) | |
} | |
} |
This file contains 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
abstract class TestBaseViewHolder(view: View) : RecyclerView.ViewHolder(view) { | |
abstract fun bind(T: Any, position: Int) | |
} |
This file contains 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
abstract class ApolloBaseDiffUtilCallback<T : Any> : DiffUtil.Callback() { | |
var oldList: List<T> = listOf() | |
var newList: List<T> = listOf() | |
fun setLists(oldList: List<T>, newList: List<T>) { | |
this.oldList = oldList | |
this.newList = newList | |
} | |
override fun getNewListSize(): Int = this.newList.size | |
override fun getOldListSize(): Int = this.oldList.size | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment