Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – viewholder init for onItemClick is not working

Posted on November 11, 2022 By

Solution 1 :

tried android_clickable=”true” in your xml?

Solution 2 :

As the documentation suggest for:

RecyclerView.Adapter.html#onBindViewHolder

Called by RecyclerView to display the data at the specified position.
This method should update the contents of the itemView to reflect the
item at the given position.

And for the RecyclerView.Adapter.html#onCreateViewHolder

Called when RecyclerView needs a new RecyclerView.ViewHolder of the
given type to represent an item.

This new ViewHolder should be constructed with a new View that can
represent the items of the given type. You can either create a new
View manually or inflate it from an XML layout file.

As onCreateViewHolder is a method where we return the type of ViewHolder only while in onBindViewHolder we update or initialise the data to display.

It means no data is binded in onCreateViewHolder and binding anything in this method will have no effect.

Problem :

I am researching on how to add an onClick event on my recyclerview properly,

currently I am using the interface inside my customAdapter

class CategoryAdapter(val categoryList : List<CategoryObject>, val context: Context, val mItemClickListener: MainInterface) : RecyclerView.Adapter<CategoryAdapter.ViewHolder>() {

interface MainInterface {
    fun onCategoryItemClick(categoryKey: Int)
}

override fun getItemCount(): Int {
    return categoryList.size
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(LayoutInflater.from(context).inflate(R.layout.listview_category, parent, false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder?.categoryName?.text = categoryList[position].categoryName
//        holder?.categoryName.setOnClickListener{
//            mItemClickListener.onCategoryItemClick(position)
//        }
//        holder?.categoryName?.setOnClickListener { listener(categoryList[position]) }
}

inner class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
    val categoryName = view.lv_txt_category_name

    init {
        view.setOnClickListener {
            mItemClickListener.onCategoryItemClick(adapterPosition)
        }
    }
}

}  

viewHolder’s onClick does not register on my activity ovveride function.

but putting the onClick inside onBindViewHolder works perfectly,

I’m not sure which is more efficient, if the onBindViewHolder onClick is the right answer, then I’ll stick with it, but if the viewHolder is the right one, why it does not work properly?

READ  [FIXED] android - Pick multiple images from gallery and show in RecyclerView
Powered by Inline Related Posts

Thanks in advance!

Update
This is the stackoverflow post I’m using to research things

RecyclerView itemClickListener in Kotlin

Comments

Comment posted by hamid_c

have you tried to set click listener on itemView instead of view???

Comment posted by MaChee Neraid

@hamid_c, itemView is not working 🙁

Comment posted by hamid_c

you mean you used

Comment posted by MaChee Neraid

@hamid_c, yes, but still, my override function in activity is not called.

Comment posted by hamid_c

can you add a

Comment posted by MaChee Neraid

I think you don’t need to add this but it won’t harm to try, still… not working

Comment posted by MaChee Neraid

so, does it means even I put my onClicklistener inside my OnBindViewHolder, it won’t create memory-leak-ish events??

Comment posted by stackoverflow.com/questions/35086967/…

@MaCheeNeraid very nice question indeed. Although many articles/blogs use this approach and I would say it’s a best practice to set clickListener’s in

Comment posted by MaChee Neraid

Thanks for the link! just to add, actually I also used the lambda expression “(category) -> Unit?”. This is the quickest and simpliest way but android profiler shows that my activity is not garbage collected even I forced it. That’s why I’m avoiding the lambda.

Comment posted by Tenfour04

The RecyclerView Adapter is not going to leak your Activity if it is referenced only by the Activity itself. A lambda compiles to an implementation of an interface, so that wasn’t the cause of your leak, if you indeed had one.

Comment posted by Tenfour04

I don’t think this explanation is the reason for it not working. There is nothing in RecyclerView.Adapter that is going to go through all the views and remove click listeners that you have set before

READ  [FIXED] Floating Action Button (FAB) appears in rectangle shape by default - Android Studio
Powered by Inline Related Posts

Android Tags:android, android-recyclerview, kotlin, onclicklistener

Post navigation

Previous Post: [FIXED] android – Convert BMP image to PNG or JPG in Java without loading the entire image in memory
Next Post: [FIXED] xml – Android – prevent TextView to push out another element inside LinearLayout

Related Posts

[FIXED] java – How to save the state of several EditTexts (to avoid user’s mobile rotation) which were created on run time on a button press? Android
[FIXED] android – How to add an listener to when onBindViewHolder is called after calling recyclerViewAdapter.notifyItemChanged()? Android
[FIXED] java – I am trying to print or write protected file /data/data/com.package/file in android logs Android
[FIXED] kotlin – Mapbox Android : Adjust Opacity of background layer Android
[FIXED] java – Android Firestore querying particular value in Array of Objects Android
[FIXED] search – SearchView AutoComplete in Android Studio Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme