@@ -6,21 +6,26 @@ import android.view.ViewGroup
6
6
import android.widget.TextView
7
7
import androidx.recyclerview.widget.RecyclerView
8
8
import com.heycode.iporesult.R
9
+ import com.heycode.iporesult.databinding.BoidItemBinding
9
10
import de.hdodenhof.circleimageview.CircleImageView
10
11
11
12
class BoidAdapter (
12
13
private val names : Array <String >,
13
14
private val listener : OnItemClickListener ,
14
15
) : RecyclerView.Adapter<BoidAdapter.ViewHolder>() {
15
16
17
+
18
+ interface OnItemClickListener {
19
+ fun onItemClick (position : Int )
20
+ fun onItemLongClick (position : Int )
21
+ }
22
+
16
23
// create new views
17
24
override fun onCreateViewHolder (parent : ViewGroup , viewType : Int ): ViewHolder {
18
25
// inflates the card_view_design view
19
26
// that is used to hold list item
20
- val view = LayoutInflater .from(parent.context)
21
- .inflate(R .layout.boid_item, parent, false )
22
-
23
- return ViewHolder (view)
27
+ val binding = BoidItemBinding .inflate(LayoutInflater .from(parent.context),parent,false )
28
+ return ViewHolder (binding)
24
29
}
25
30
26
31
// binds the list items to a view
@@ -29,17 +34,15 @@ class BoidAdapter(
29
34
val item1 = names[position]
30
35
31
36
// sets the image to the imageview from our itemHolder class
32
- holder.image.setImageResource(R .drawable.logo)
33
- // Glide
34
- // .with(context)
35
- // .load(LOGO_IMG)
36
- // .fitCenter()
37
+ holder.apply {
38
+ image.setImageResource(R .drawable.logo)
39
+ title.text = item1
40
+ subtitle.text = item1
41
+ }
42
+ // Glide.with(context).load(LOGO_IMG).fitCenter()
37
43
// .placeholder(R.drawable.logo)
38
44
// .into(holder.image)
39
45
40
- // sets the text to the textview from our itemHolder class
41
- holder.title.text = item1
42
- holder.subtitle.text = item1
43
46
}
44
47
45
48
// return the number of the items in the list
@@ -48,30 +51,27 @@ class BoidAdapter(
48
51
}
49
52
50
53
// Holds the views for adding it to image and text
51
- inner class ViewHolder (ItemView : View ) : RecyclerView.ViewHolder(ItemView ) {
52
- val image: CircleImageView = ItemView .findViewById( R .id. ivBoidPic)
53
- val title: TextView = ItemView .findViewById( R .id. tvBoidTitle)
54
- val subtitle: TextView = ItemView .findViewById( R .id. tvBoidSubtitle)
54
+ inner class ViewHolder (binding : BoidItemBinding ) : RecyclerView.ViewHolder(binding.root),View.OnClickListener {
55
+ val image: CircleImageView = binding. ivBoidPic
56
+ val title: TextView = binding. tvBoidTitle
57
+ val subtitle: TextView = binding. tvBoidSubtitle
55
58
56
59
init {
57
- ItemView .setOnClickListener {
58
- val position = adapterPosition
59
- if (position != RecyclerView .NO_POSITION ) {
60
- listener.onItemClick(position)
61
- }
62
- }
63
- ItemView .setOnLongClickListener {
60
+ binding.clRecentItem.setOnClickListener (this )
61
+ binding.clRecentItem.setOnLongClickListener {
64
62
val position = adapterPosition
65
63
if (position != RecyclerView .NO_POSITION ) {
66
64
listener.onItemLongClick(position)
67
65
}
68
66
return @setOnLongClickListener true
69
67
}
70
68
}
71
- }
72
69
73
- interface OnItemClickListener {
74
- fun onItemClick (position : Int )
75
- fun onItemLongClick (position : Int )
70
+ override fun onClick (p0 : View ? ) {
71
+ val position = adapterPosition
72
+ if (position != RecyclerView .NO_POSITION ) {
73
+ listener.onItemClick(position)
74
+ }
75
+ }
76
76
}
77
77
}
0 commit comments