Sync Scroll Of Horizontal Recylerview With Viewpager Scroll
i have a view pager with some values, same count for the recycler view abovethe recylerview(Act like a tablayout) i have achieved the function with snap helper and all working fine
Solution 1:
I also had the same problem as you havent provided proper info am adding the following snippet
You need to pass the positionoffset of viewpager to the funtion as you said scrollToPositionWithOffset is the correct way but you need to do some calculation before that.
Check the following code
protectedvoidscrollToTab(int position, float positionOffset) {
intscrollOffset=0;
if (mItemWidth == 1 && yourrecylerview != null) {
Viewchild= yourrecylerview.getChildAt(0);
if (child != null) {
mItemWidth = child.getMeasuredWidth() * 2;
}
}
ViewselectedView= yourlayoutmanager.findViewByPosition(position);
ViewnextView= yourlayoutmanager.findViewByPosition(position + 1);
if (nextView != null) {
if (selectedView != null) {
intwidth= yourrecylerview.getMeasuredWidth();
floatsLeft= (position == 0) ? 24 : width / mItemWidth - selectedView.getMeasuredWidth() / mItemWidth; // left edge of selected tabfloatsRight= sLeft + selectedView.getMeasuredWidth(); // right edge of selected tabfloatnLeft= width / mItemWidth - nextView.getMeasuredWidth() / mItemWidth; // left edge of next tabfloatdistance= sRight - nLeft; // total distance that is needed to distance to next tabfloatdx= distance * positionOffset;
scrollOffset = (int) (sLeft - dx);
} else {
scrollOffset = (mItemWidth/2) * (-1);
//mRequestScrollToTab = true;
}
if (position != 0) {
// scrollOffset=scrollOffset-10;
}
mIndicatorPosition = position;
yourrecylerview.stopScroll();
if ((position != mOldPosition || scrollOffset != mOldScrollOffset)) {
yourlayoutmanager.scrollToPositionWithOffset(position, scrollOffset);
}
}
else
{
//Sometimes view go null for handling that situvation
yourrecylerview.scrollToPosition(position);
yourviewpager.setCurrentItem(position);
}
mOldPosition = position;
mOldScrollOffset = scrollOffset;
mOldPositionOffset=positionOffset;
}
Add the above methord on you addOnPageChangeListener of viewpager
@OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
scrollToTab(position, positionOffset);
}
I have made a sample which help you if you want further verification RecyclerParallelViewPager go to link to see the solution
Hope this will help you
Post a Comment for "Sync Scroll Of Horizontal Recylerview With Viewpager Scroll"