Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Whats going on here?
- Yo guys here is an efficient way for implementing `OnclickListener` for each view in One items of `RecyclerView`.
- 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:
- ```kotlin
- override fun onBindViewHolder(holder: ViewHolder, position: Int) {
- holder.rootItem.text= items[position].second
- holder.rootItem.setOnClickListener(object:View.OnClickListener{
- override fun onClick(p0: View?) {
- // Imp your logic
- }
- })
- }
- ```
- * I know I can use lambda form instead of this bunch of codes!;)
- 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!
- 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:
- ```kotlin
- class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
- internal var rootItem = itemView
- internal var add = itemView.findViewById<AppCompatImageButton>(R.id.graphics_btn_add)
- internal var remove = itemView.findViewById<AppCompatImageButton>(R.id.graphics_btn_remove)
- //------------------
- internal var counter = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_counter)
- internal var price = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_desc)
- internal var name = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_name)
- internal var desc = itemView.findViewById<AppCompatTextView>(R.id.graphics_txt_desc)
- internal var icon = itemView.findViewById<AppCompatImageView>(R.id.graphics_img_icon)
- }
- ```
- 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:
- ```kotlin
- private var recyclerView: RecyclerView? = null
- private var count: IntArray = IntArray(items.size)
- private var viewHolders: HashMap<Int, GMenuFoodAdapter.ViewHolder> = HashMap()
- ```
- Next step is implementing `View.OnClickListener` interface by the Adapter:
- ```Kotlin
- class GMenuFoodAdapter(private var items: Array<Food>, private var context: Context,
- private var listener: GItemClickListener) :
- RecyclerView.Adapter<GMenuFoodAdapter.ViewHolder>(), View.OnClickListener {
- ```
- * Don't pay attention to GItemClickListener for now!
- now set the General Onclick listerner for all your views in `OnCreateViewHolder` like this:
- ```kotlin
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
- val rootView = LayoutInflater.from(context)
- .inflate(R.layout.graphics_adapter_menu_food, parent, false)
- graphicTools.setTypeface(rootView, typeface.YEKAN_REG)
- val vh = ViewHolder(rootView)
- //this 2 lines
- vh.add.setOnClickListener (this)
- vh.remove.setOnClickListener (this)
- //
- graphicTools.setTypeface(rootView, typeface.IRAN_SANS_REG) //ignore this line please!
- return vh
- }
- ```
- and on `OnBindViewHolder` fill the `viewHolders` array:
- ```kotlin
- override fun onBindViewHolder(holder: ViewHolder, position: Int) {
- val item = items[position]
- viewHolders[position] = holder
- }
- ```
- and here we are! the final step is implementin `onClick`:
- ```kotlin
- override fun onClick(p0: View?) {
- recyclerView.let {
- // you should call parent until reach the root view of the item!
- val itemPosition = recyclerView!!.getChildLayoutPosition(p0!!.parent!!.parent!!.parent as View)
- when (p0.id) {
- R.id.graphics_btn_add -> {
- // here is logic for my add button
- listener.onItemClick(items[itemPosition], itemPosition, 1)
- count[itemPosition] += 1
- }
- R.id.graphics_btn_remove -> {
- //logic for my remove button
- if (count[itemPosition] > 0) {
- listener.onItemClick(items[itemPosition], itemPosition, -1)
- count[itemPosition] -= 1
- }
- }
- }
- //common opration for all clicks!
- updateView(itemPosition)
- }
- }
- ```
- and in my `updateView` func i change the color of add button:
- ```kotlin
- private fun updateView(itemPosition: Int) {
- val vh = viewHolders[itemPosition]
- vh.let {
- vh!!.counter.text = count[itemPosition].toString()
- if (count[itemPosition] > 0)
- ImageViewCompat.setImageTintList(vh!!.add,
- (ColorStateList.valueOf(ContextCompat.getColor(context,
- R.color.colorAccent))))
- else
- ImageViewCompat.setImageTintList(vh!!.add,
- (ColorStateList.valueOf(ContextCompat.getColor(context,
- R.color.colorTextDarkSecondary))))
- }
- }
- ```
- its all done!
Add Comment
Please, Sign In to add comment