I have a RecyclerView . In the layout for the item, there is an ImageView on which hangs onClick , which calls the corresponding method in the class in which this RecyclerView initialized. In the method argument, I get the View element that the user clicked on. I need to know the position of the item in which this element is located.
I looked at the documentation and there is a getChildAdapterPosition method. I gave it a view to get the number of the item in which it is located, but when I click on this item, the application just crashes.
What am I doing wrong?
Here is the code for the click handler method:
public void onClickHandler(View view) { switch (view.getId()) { case R.id.status_img: if (view.getTag() instanceof Roll) { Roll roll = (Roll) view.getTag(); int recyclerPos = recyclerView.getChildAdapterPosition(view); } break; } }
position=recyclerView.findContainingViewHolder(view).getPosition();- Andrey MihalevgetAdapterPosition()method of the holder, sincegetPosition()is deprecated, then the solution makes sense. - pavlofffListView, but the principle is clear, I think. - pavlofff