I write the click in the adapter.(This adapter deal with an image and a text.)
class FruitAdapter (val fruitList:List<Fruit>)://Fruit is a data class for an image with its text
RecyclerView.Adapter<FruitAdapter.ViewHolder>() {
//not very important here I guess
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val fruitImage: ImageView = view.findViewById(R.id.fruitImage)
val fruitName: TextView = view.findViewById(R.id.fruitName)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.fruit_item, parent, false)
val viewholder=ViewHolder(view)
viewholder.fruitImage.setOnClickListener{
//here I try to get the position
val position=viewholder.absoluteAdapterPosition
Log.e("a",position.toString())}
...
}
and it just output -1 to me in logcat, why??
I try to change the “.absoluteAdapterPosition” to “.bindingAdapterPosition” or “.layoutPosition”, but they all -1.
New contributor
Weird_dog is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.