Guest User

Untitled

a guest
Aug 10th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. ### Whats going on here?
  2.  
  3. Yo guys here is an efficient way for implementing `OnclickListener` for each view in One items of `RecyclerView`.
  4.  
  5. a common and simple way of implementing this function is to create new `OnclickListener` for each instanse of `view` on `onBindViewHolder` function/method like this:
  6.  
  7.  
  8. ```kotlin
  9.  
  10. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  11. holder.rootItem.text= items[position].second
  12. holder.rootItem.setOnClickListener(object:View.OnClickListener{
  13. override fun onClick(p0: View?) {
  14. // Imp your logic
  15. }
  16.  
  17. })
  18. }
  19.  
  20. ```
  21. * I know I can use lambda form instead of this bunch of codes!;)
  22.  
  23. If the count of items in your recyclerView is not more than 15 or 20 this method is an easy way out! but if your items are more than this and you have more than 2 OnClickListener on each item it's not the best practice!
  24.  
  25. For example, I have a `recyclerView` For a list of foods in a menu and I want two buttons (add and remove) on each item:
  26.  
  27. ```kotlin
  28.  
  29. class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  30. internal var rootItem = itemView
  31.  
  32. internal var add = itemView.findViewById<AppCompatImageButton>(R.id.graphics_btn_add)
  33. internal var remove = itemView.findViewById<AppCompatImageButton>(R.id.graphics_btn_remove)
  34.  
  35. //------------------
  36. internal var counter = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_counter)
  37. internal var price = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_desc)
  38. internal var name = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_name)
  39. internal var desc = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_desc)
  40. internal var icon = itemView.findViewById<AppCompatImageView>(R.id.graphics_img_icon)
  41.  
  42.  
  43. }
  44. ```
  45.  
  46. I want to add a function for each of this two buttons and use a general `OnClickListener` for all views of all items! as a simple example, add button add +1 to count of selected food and change the color of itself and remove button decrease number of selected food!at the first step,I need to add some fields to my Adapter:
  47.  
  48. ```kotlin
  49. private var recyclerView: RecyclerView? = null
  50. private var count: IntArray = IntArray(items.size)
  51. private var viewHolders: HashMap<Int, GMenuFoodAdapter.ViewHolder> = HashMap()
  52.  
  53. ```
  54.  
  55. Next step is implementing `View.OnClickListener` interface by the Adapter:
  56.  
  57. ```Kotlin
  58. class GMenuFoodAdapter(private var items: Array<Food>, private var context: Context,
  59. private var listener: GItemClickListener) :
  60. RecyclerView.Adapter<GMenuFoodAdapter.ViewHolder>(), View.OnClickListener {
  61.  
  62. ```
  63.  
  64. * Don't pay attention to GItemClickListener for now!
  65.  
  66. now set the General Onclick listerner for all your views in `OnCreateViewHolder` like this:
  67.  
  68. ```kotlin
  69.  
  70. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  71. val rootView = LayoutInflater.from(context)
  72. .inflate(R.layout.graphics_adapter_menu_food, parent, false)
  73. graphicTools.setTypeface(rootView, typeface.YEKAN_REG)
  74.  
  75. val vh = ViewHolder(rootView)
  76.  
  77. //this 2 lines
  78. vh.add.setOnClickListener (this)
  79. vh.remove.setOnClickListener (this)
  80. //
  81.  
  82. graphicTools.setTypeface(rootView, typeface.IRAN_SANS_REG) //ignore this line please!
  83. return vh
  84. }
  85.  
  86. ```
  87.  
  88. and on `OnBindViewHolder` fill the `viewHolders` array:
  89.  
  90. ```kotlin
  91. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  92. val item = items[position]
  93. viewHolders[position] = holder
  94. }
  95.  
  96. ```
  97.  
  98. and here we are! the final step is implementin `onClick`:
  99.  
  100. ```kotlin
  101. override fun onClick(p0: View?) {
  102. recyclerView.let {
  103. // you should call parent until reach the root view of the item!
  104. val itemPosition = recyclerView!!.getChildLayoutPosition(p0!!.parent!!.parent!!.parent as View)
  105. when (p0.id) {
  106. R.id.graphics_btn_add -> {
  107. // here is logic for my add button
  108. listener.onItemClick(items[itemPosition], itemPosition, 1)
  109. count[itemPosition] += 1
  110. }
  111. R.id.graphics_btn_remove -> {
  112. //logic for my remove button
  113. if (count[itemPosition] > 0) {
  114. listener.onItemClick(items[itemPosition], itemPosition, -1)
  115. count[itemPosition] -= 1
  116. }
  117. }
  118. }
  119. //common opration for all clicks!
  120. updateView(itemPosition)
  121. }
  122.  
  123. }
  124.  
  125. ```
  126.  
  127. and in my `updateView` func i change the color of add button:
  128.  
  129. ```kotlin
  130. private fun updateView(itemPosition: Int) {
  131. val vh = viewHolders[itemPosition]
  132. vh.let {
  133. vh!!.counter.text = count[itemPosition].toString()
  134. if (count[itemPosition] > 0)
  135. ImageViewCompat.setImageTintList(vh!!.add,
  136. (ColorStateList.valueOf(ContextCompat.getColor(context,
  137. R.color.colorAccent))))
  138. else
  139. ImageViewCompat.setImageTintList(vh!!.add,
  140. (ColorStateList.valueOf(ContextCompat.getColor(context,
  141. R.color.colorTextDarkSecondary))))
  142. }
  143. }
  144.  
  145. ```
  146.  
  147. its all done!
Add Comment
Please, Sign In to add comment