I am trying to scroll to first completely visible item while scrolling and I am doing following:
recyclerView.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager layoutManager = ((LinearLayoutManager) recyclerView
.getLayoutManager());
int position = layoutManager.findFirstCompletelyVisibleItemPosition();
if(position>0){
recyclerView.smoothScrollToPosition(position);
Log.e(TAG, "onScrolled: just called smooth scroll >> "+position );
}
}
});
I am getting correct position of firs completely visible item but scroll to it [trying to scroll it to the initial position of visible portion of the screen] is not working. I tried to achieve it by doing following:
1.
recyclerView.smoothScrollToPosition(position);
2.
layoutManager.smoothScrollToPosition(recyclerView, null, position);
they make no change!! I googled for solution but I am not getting the right one. Please, suggest me how can I achieve that ?