I'm working with recyclerview with snaphelper. This is my recyclerview:
final LinearLayoutManager lm = new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false);
recyclerview.setLayoutManager(lm);
final PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerview);
When the user scrolls to another cell I need to do something with the new cell position.
This is what I do to get the position:
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState != RecyclerView.SCROLL_STATE_IDLE) {
return;
}
if recyclerview == null) {
return;
}
//recyclerview.clearOnChildAttachStateChangeListeners();
int firstVisible = ((LinearLayoutManager) viewerRv.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
if (firstVisible == RecyclerView.NO_POSITION) {
return;
}
doSomething(firstVisible);
}
The problem is the firstVisible var does not always give me the right position for example when i scroll from position 0 to 9 this can be the output: 0,1,2,3,4,4,5,9
Is there another way to get the right current position?
What are the best practices for that?