luizgrp / SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.
MIT License
1.68k stars 372 forks source link

Getting adapter position sometimes produces IndexOutOfBoundsException #19

Closed aubryll closed 7 years ago

aubryll commented 7 years ago

How do you implement a click listener on an adapter if am wrong? I implemented SectionedGridview, it works superb even the search funtionality except the clicklistener, here is how I made mine

holder.btn_option.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickedListener.onItemClicked(v, holder.getAdapterPosition()); } });

where the onItemClickedListener is basically an interface, I get wrong positions

aubryll commented 7 years ago

solved it, actually getAdapterPosition() returns the position inclusive of the headers, so what I did was I moved my clicklistener into the onBindItemViewHolder here is a snippet

holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickedListenerAdapter.onItemClicked(v, position); } });

and it worked, I urge everyone to avoid using getAdapterPosition() unless they are up for some serious drama