I am trying to achieve scroll tab layout change when recycler view is changed as well as change recycler view when scroll tab layout is changed.
Here is my code:
LinearLayoutManager llm = new LinearLayoutManager(mContext);
recyclerView.setLayoutManager(llm);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
isScrolling = false;
llm.scrollToPositionWithOffset(tab.getPosition(), 0);
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(mContext) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
smoothScroller.setTargetPosition(tab.getPosition());
llm.startSmoothScroll(smoothScroller);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
isScrolling = true;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isScrolling) {
int position = llm.findFirstVisibleItemPosition();
if (position != tabLayout.getSelectedTabPosition()) {
TabLayout.Tab tab = tabLayout.getTabAt(position);
tab.select();
}
}
}
});
Below is the animation what I want to achieve:
https://i.stack.imgur.com/E8rZ2.gif
If you know how to fix, please share it. Thank you in advance.