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

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

问题描述

我有一个视图分页器,其中包含一些值,在recylerview上方的回收者视图中具有相同的计数(就像一个tabLayout)

i have a view pager with some values, same count for the recycler view abovethe recylerview(Act like a tablayout)

我已经使用snap helper实现了该功能,所有我想做的唯一正常的事情就是我想将recyclerview的滚动与viewpager滚动同步,而我无法实现.

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.

以下是我要实现的目标

但是我得到的就像下面的

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);
}

但是它令人奇怪的行为我也尝试了 scrollToPositionWithOffset

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

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

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.

检查以下代码

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;

}

在viewpager的addOnPageChangeListener上添加上述方法

Add the above methord on you addOnPageChangeListener of viewpager

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

我制作了一个样本,如果您需要进一步的验证,可以为您提供帮助 RecyclerParallelViewPager 转到链接以查看解决方案

I have made a sample which help you if you want further verification RecyclerParallelViewPager go to link to see the solution

希望这对您有帮助

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

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