-
Notifications
You must be signed in to change notification settings - Fork 3
/
BaseRecyclerViewAdapter.kt
46 lines (34 loc) 路 1.37 KB
/
BaseRecyclerViewAdapter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.foxhole.gameskeeper.base
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.foxhole.gameskeeper.R
import com.foxhole.gameskeeper.databinding.LayoutItemScreenshotBinding
/**
* Created by Musfick Jamil on 4/29/2020$.
*/
abstract class BaseRecyclerViewAdapter<T, VB : ViewBinding> : RecyclerView.Adapter<BaseRecyclerViewAdapter.Companion.BaseViewHolder<VB>>() {
private var items = mutableListOf<T>()
private var lastPosition = -1
fun addItems(items: MutableList<T>) {
this.items = items
notifyDataSetChanged()
}
abstract fun setBinding(parent: ViewGroup): VB
abstract fun onBindData(holder: BaseViewHolder<VB>, item: T, position: Int)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<VB> {
return BaseViewHolder(setBinding(parent))
}
override fun onBindViewHolder(holder: BaseViewHolder<VB>, position: Int) {
onBindData(holder, items[position], position)
}
override fun getItemCount(): Int = items.size
companion object {
class BaseViewHolder<VB : ViewBinding>(val binding: VB) :
RecyclerView.ViewHolder(binding.root)
}
}