This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathBrowseAdapter.kt
More file actions
48 lines (39 loc) · 1.41 KB
/
BrowseAdapter.kt
File metadata and controls
48 lines (39 loc) · 1.41 KB
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
package androidx.viewpager2.integration.testapp
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class BrowseAdapter(
private val data: List<Map<String, Any>>,
private val onItemClickListener: (item: Map<String, Any>) -> Unit
) : RecyclerView.Adapter<BrowseAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
itemView = LayoutInflater.from(parent.context)
.inflate(android.R.layout.simple_list_item_1, parent, false)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(data[position]["title"] as? String ?: "")
}
override fun getItemCount(): Int {
return data.size
}
inner class ViewHolder(
itemView: View
) : RecyclerView.ViewHolder(itemView) {
private val text1 = itemView.findViewById<TextView>(android.R.id.text1)
init {
text1.setOnClickListener {
val adapterPosition = bindingAdapterPosition
if (adapterPosition != RecyclerView.NO_POSITION) {
onItemClickListener.invoke(data[adapterPosition])
}
}
}
fun bind(text: String) {
text1.text = text
}
}
}