-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListAdapterTemplate.kt
74 lines (58 loc) · 2.11 KB
/
ListAdapterTemplate.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
#parse("File Header.java")
class ${NAME}(private val interaction: Interaction? = null) :
RecyclerView.Adapter<RecyclerView.ViewHolder>(){
val DIFF_CALLBACK = object: DiffUtil.ItemCallback<${Model_Class}>(){
override fun areItemsTheSame(oldItem: ${Model_Class}, newItem: ${Model_Class}): Boolean {
TODO("not implemented")
}
override fun areContentsTheSame(oldItem: ${Model_Class}, newItem: ${Model_Class}): Boolean {
TODO("not implemented")
}
}
private val differ = AsyncListDiffer(this, DIFF_CALLBACK)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ${ViewHolder_Class}(
LayoutInflater.from(parent.context).inflate(
R.layout.${Item_Layout_ID},
parent,
false
),
interaction
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(holder){
is ${ViewHolder_Class} -> {
holder.bind(differ.currentList.get(position))
}
}
}
override fun getItemCount(): Int {
return differ.currentList.size
}
fun submitList(list: List<${Model_Class}>){
differ.submitList(list)
}
class ${ViewHolder_Class}
constructor(
itemView: View,
private val interaction: Interaction?
) : RecyclerView.ViewHolder(itemView){
fun bind(item: ${Model_Class}) = with(itemView) {
itemView.setOnClickListener {
interaction?.onItemSelected(adapterPosition, item)
}
TODO("bind view with data")
}
}
interface Interaction {
fun onItemSelected(position: Int, item: ${Model_Class})
}
}