Skip to content

Instantly share code, notes, and snippets.

@swarawan
Last active January 1, 2021 09:52
Show Gist options
  • Select an option

  • Save swarawan/6f80ae90751a000633f855dc1369e88a to your computer and use it in GitHub Desktop.

Select an option

Save swarawan/6f80ae90751a000633f855dc1369e88a to your computer and use it in GitHub Desktop.

Revisions

  1. swarawan revised this gist Jan 1, 2021. 1 changed file with 0 additions and 12 deletions.
    12 changes: 0 additions & 12 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,12 +0,0 @@
    /**
    * How to
    */
    private val studentDataAdapter by lazy {
    GeneralRecyclerView<SchoolInformationEntity>(
    diffCallback = diffCallback,
    holderResId = R.layout.item_student_confirmation,
    onBind = { data, view ->
    setupStudentData(data, view)
    }
    )
    }
  2. swarawan revised this gist Jan 1, 2021. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions gistfile1.txt
    Original 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)
    }
    )
    }
  3. swarawan created this gist Jan 1, 2021.
    23 changes: 23 additions & 0 deletions DiffCallback.kt
    Original 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]
    }

    }
    88 changes: 88 additions & 0 deletions GeneralRecyclerView.kt
    Original 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)
    }
    }
    }
    }