Sam*_*ere 1 android scrollview android-layout android-viewpager android-recyclerview
我有一个带有一些值的视图寻呼机,回收器视图上方的回收器视图的计数相同(就像一个tablayout)
我已经使用 snap helper 实现了该功能,并且一切正常,唯一我想要的就是我想将 recyclerview 的滚动与 viewpager 滚动同步,这是我无法实现的。
以下是我想要达到的目标
但我得到的是如下
我覆盖了视图页面的滚动但没有用
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
myRecyler..scrollBy(positionOffsetPixels,0);
}
Run Code Online (Sandbox Code Playgroud)
但它的奇怪行为我也尝试过scrollToPositionWithOffset
layoutManagerStart.scrollToPositionWithOffset(position,positionOffsetPixels);
Run Code Online (Sandbox Code Playgroud)
但都失败了,我使用 snap helpers 所以所选项目将位于第一个位置。
任何人都可以帮助我达到上述效果。
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;
}
Run Code Online (Sandbox Code Playgroud)
Add the above methord on you addOnPageChangeListener of viewpager
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
scrollToTab(position, positionOffset);
}
Run Code Online (Sandbox Code Playgroud)
如果您想进一步验证RecyclerParallelViewPager 转到链接查看解决方案,我制作了一个示例可以帮助您
希望能帮到你
归档时间: |
|
查看次数: |
632 次 |
最近记录: |