current item Position in RecyclerView with interface Android

Issue

Here I am not getting my current clicked item position. It some time shows incorrect position and some time make crash.
My Adapter Code is as below.

Some time I clicked in 1st position it will work fine. But I clicked again at any position from the item it shows it’s previous item.

Thanks in advance for your answer or suggestions.

class MyAdapter(private val mList: ArrayList<MyModel>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    private lateinit var mListener: OnItemClickListener

    interface OnItemClickListener : AdapterView.OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mListener = mListener
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_all_brands, parent, false)
        return ViewHolder(view, mListener)
    }


    @SuppressLint("SetTextI18n")
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.ivBrandImage.setImageResource(mList[position].image)
        holder.tvBrandName.text = mList[position].name

        holder.tvBrandName.isSelected = true
        holder.tvBrandName.setSingleLine()

    }


    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


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

    class ViewHolder(ItemView: View, listener: OnItemClickListener) :
        RecyclerView.ViewHolder(ItemView) {
        val ivBrandImage: ImageView = itemView.findViewById(R.id.ivBrandImage)
        val tvBrandName: TextView = itemView.findViewById(R.id.tvBrandName)

        init {
            itemView.setOnClickListener {
                listener.onItemClick(adapterPosition)
            }
        }

    }

}

Solution

There is one mistake you do in the setOnItemClickListener Method.

Remove :

 fun setOnItemClickListener(listener: OnItemClickListener) {
    mListener = mListener
 }

Replace :

fun setOnItemClickListener(listener: OnItemClickListener) {
    mListener = listener
}

So your total code should look like

class MyAdapter(private val mList: ArrayList<MyModel>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    private lateinit var mListener: OnItemClickListener

    interface OnItemClickListener : AdapterView.OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        mListener = listener
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_all_brands, parent, false)
        return ViewHolder(view, mListener)
    }


    @SuppressLint("SetTextI18n")
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.ivBrandImage.setImageResource(mList[position].image)
        holder.tvBrandName.text = mList[position].name

        holder.tvBrandName.isSelected = true
        holder.tvBrandName.setSingleLine()

    }


    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


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

    class ViewHolder(ItemView: View, listener: OnItemClickListener) :
        RecyclerView.ViewHolder(ItemView) {
        val ivBrandImage: ImageView = itemView.findViewById(R.id.ivBrandImage)
        val tvBrandName: TextView = itemView.findViewById(R.id.tvBrandName)

        init {
            itemView.setOnClickListener {
                listener.onItemClick(adapterPosition)
            }
        }

    }

}

Answered By – Bhavin Solanki

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published