将水平 recylerview 的滚动与 viewpager 滚动同步 [英] Sync scroll of horizontal recylerview with viewpager scroll

查看:22
本文介绍了将水平 recylerview 的滚动与 viewpager 滚动同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些值的视图寻呼机,回收器视图上方的回收器视图的计数相同(就像一个标签布局)

我已经使用 snap helper 实现了该功能,所有工作正常我唯一想要的是我想将 recyclerview 的滚动与 viewpager 滚动同步,这是我无法实现的.

以下是我想要达到的目标

但我得到的是如下所示

我覆盖了视图页面的滚动但没有用

 @Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {myRecyler..scrollBy(positionOffsetPixels,0);}

但它的奇怪行为我也试过 scrollToPositionWithOffset

layoutManagerStart.scrollToPositionWithOffset(position,positionOffsetPixels);

但都失败了,我使用 snap helpers,所以所选项目将位于第一个位置.

谁能帮我实现上面那个效果.

解决方案

我也遇到了同样的问题,因为你没有提供正确的信息我添加了以下代码片段

您需要将 viewpager 的 positionoffset 传递给函数,因为您说 scrollToPositionWithOffset 是正确的方法,但您需要在此之前进行一些计算.

检查以下代码

protected void scrollToTab(int position, float positionOffset) {int 滚动偏移 = 0;if (mItemWidth == 1 && yourrecylerview != null) {查看孩子 = yourrecylerview.getChildAt(0);如果(孩子!= null){mItemWidth = child.getMeasuredWidth() * 2;}}查看 selectedView = yourlayoutmanager.findViewByPosition(position);查看 nextView = yourlayoutmanager.findViewByPosition(position + 1);如果(下一个视图!= null){if (selectedView != null) {int width = yourrecylerview.getMeasuredWidth();浮动 sLeft = (位置 == 0) ?24:宽度/mItemWidth - selectedView.getMeasuredWidth()/mItemWidth;//选中标签的左边缘浮动 sRight = sLeft + selectedView.getMeasuredWidth();//选中标签的右边缘浮动 nLeft = 宽度/mItemWidth - nextView.getMeasuredWidth()/mItemWidth;//下一个标签的左边缘浮动距离 = sRight - nLeft;//到下一个选项卡所需的总距离浮动 dx = 距离 * 位置偏移;scrollOffset = (int) (sLeft - dx);} 别的 {scrollOffset = (mItemWidth/2) * (-1);//mRequestScrollToTab = true;}如果(位置!= 0){//scrollOffset=scrollOffset-10;}mIndicatorPosition = 位置;yourrecylerview.stopScroll();如果((位置!= mOldPosition || scrollOffset != mOldScrollOffset)){yourlayoutmanager.scrollToPositionWithOffset(position, scrollOffset);}}别的{//有时视图为空以处理该情况yourrecylerview.scrollToPosition(位置);yourviewpager.setCurrentItem(位置);}mOldPosition = 位置;mOldScrollOffset = 滚动偏移;mOldPositionOffset=位置偏移;}

在viewpager的addOnPageChangeListener上添加上面的方法

 @Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {scrollToTab(位置,位置偏移);}

如果您想进一步验证,我已经制作了一个样本,可以帮助您

希望对你有帮助

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 only thing i want is i want to sync the scroll of the recyclerview with viewpager scroll, which i cant able to achive.

The below is what i want to achive

But what i am getting is like below

i override the scroll of view page but have no use

 @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

myRecyler..scrollBy(positionOffsetPixels,0);
}

But its making strange behavior i also tried scrollToPositionWithOffset

layoutManagerStart.scrollToPositionWithOffset(position,positionOffsetPixels);

but both failed, i use snap helperso the selected item will be on the first position.

Can any one help me to achieve the effect like the above one.

解决方案

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

protected void scrollToTab(int position, float positionOffset) {
    int scrollOffset = 0;

    if (mItemWidth == 1 && yourrecylerview != null) {
        View child = yourrecylerview.getChildAt(0);
        if (child != null) {
            mItemWidth = child.getMeasuredWidth() * 2;
        }
    }

    View selectedView = yourlayoutmanager.findViewByPosition(position);
    View nextView = yourlayoutmanager.findViewByPosition(position + 1);

    if (nextView != null) {
        if (selectedView != null) {
            int width = yourrecylerview.getMeasuredWidth();
            float sLeft = (position == 0) ? 24 : width / mItemWidth - selectedView.getMeasuredWidth() / mItemWidth; // left edge of selected tab
            float sRight = sLeft + selectedView.getMeasuredWidth(); // right edge of selected tab
            float nLeft = width / mItemWidth - nextView.getMeasuredWidth() / mItemWidth; // left edge of next tab
            float distance = sRight - nLeft; // total distance that is needed to distance to next tab
            float dx = 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

        @Override
        public void onPageScrolled(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

这篇关于将水平 recylerview 的滚动与 viewpager 滚动同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆