Home > Software engineering >  Need the correct clicked position within a RecyclerView
Need the correct clicked position within a RecyclerView

Time:01-11

When I click on an image within a RecyclerView, I try to enlarge it. So far everything works wonderfully. The only problem I have now is that the image of its output position is always the same. In other words, if I click on the 3rd picture, the 2nd picture enlarges with the correct content but in the wrong position. The same applies to the click on the 1st picture.

This is my AdapterDetail.kt class:

class AdapterDetail(val context: Context, private val listImg:ArrayList<String>,private var listen: CustomerAdapter.OnItemClickListener): RecyclerView.Adapter<AdapterDetail.MyCustomViewHolder>() {

...

inner class MyCustomViewHolder(view: View):RecyclerView.ViewHolder(view){

    val preview_img:ImageView = view.findViewById(R.id.preview_img)

    fun bind(item: String){
        imgView  = itemView.findViewById(R.id.preview_img)
        imgView.setOnClickListener {
            listen.onItemClick(imgView, adapterPosition)
        }
    }

}


 override fun onBindViewHolder(holder: MyCustomViewHolder, position: Int) {
    val item = listImg[position]
    holder.bind(item)
   
}

In the Activity i need the correct ImageView position to inflate them:

adapterDetail = AdapterDetail(this, house!!.objektBilder as ArrayList<String>,object:  CustomerAdapter.OnItemClickListener{
        override fun onItemClick(view: View, position: Int) {
            //Here I need the ImageView that was clicked on in the RecyclerView
            var viewPosition = view

            zoomImageFromThumb(viewPosition, house!!.objektBilder!![position])

        }

})

CodePudding user response:

Sounds like an off by one issue, since you are using the deprecated adapterPosition. Use bindingAdapterPosition instead, which gives the position as RecyclerView sees it:

listen.onItemClick(imgView, bindingAdapterPosition)
  • Related