4

I have a RecycleView and when it is scrolled, other RecycleView needs to be scrolled programmatically too. I have implemented the following, where recList is a RecycleView. As you can see when X coordinate is queried, it is 419 value, but set 419 for an other RecycleView has no impact, other RecycleView is not moving. Why?

Why this line has no effect:

recyclerView0.scrollTo(recyclerView.computeHorizontalScrollOffset(), 0);

enter image description here

enter image description here

enter image description here

János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

1

You should try

((LinearLayoutManager)(recyclerView.getLayoutManager()))
    .scrollToPositionWithOffset(0, (-1)*recyclerView.computeHorizontalOffset());
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • That's really strange. This worked for me when I specified the pixels by which I wanted to offset the view from the start. Are you sure `recyclerView.computeHorizontalOffset()` returns the value you need? You can also try removing the `(-1)*`. – EpicPandaForce Jul 08 '16 at 08:33
  • Yes returned value is valid, tried without (-1), and also changed x, y coordinates. – János Jul 08 '16 at 08:38
  • Are you sure your code shouldn't be in `onScrolled()` instead of `onScrollStateChanged()`? – EpicPandaForce Jul 08 '16 at 08:40
  • You need to supply the *total scrolling offset from the 0th element* for this to work. – EpicPandaForce Jul 11 '16 at 12:09
  • I do like the *total offset* approach. But `scrollTo` method has no effect, it is not shifting, only, `smoothScrollBy` but it accept difference value only. Should I infere from total, how much is the difference? but for that I need to store the initial position of the view .. complicate. – János Jul 13 '16 at 14:47
  • You could get the total offset by either keeping track yourself or by [using a library that does that already for you](https://github.com/ksoichiro/Android-ObservableScrollView/blob/master/library/src/main/java/com/github/ksoichiro/android/observablescrollview/ObservableRecyclerView.java) – EpicPandaForce Jul 13 '16 at 15:39
0

Try also to getting other scroll state. For me, works perfect method:

RecycleView activeRecycleView;

// ......

for (RecycleView nextView : ViewHolder.getActiveRecycleViews()) {
    nextView.scrollTo(activeRecycleView.getScrollX, 0)
    // or.....
    nextView.smoothScrollTo(activeRecycleView.getScrollX, 0)
}

Update: Also please check this Answer!

Community
  • 1
  • 1
GensaGames
  • 5,538
  • 4
  • 24
  • 53
  • Funny with `smoothScrollTo` and `onScrolled` works, all the `nextView` are moving, but, they seem shifted onto different distances. `Recycleview`s are under each other, but they are not aligned. How is it possible? `dx` is always the same. – János Jul 08 '16 at 09:44
  • @János Based on Doc https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html "This method will always be invoked before listeners." If you have several RecycleViews, Did they all scrolled with different distance? – GensaGames Jul 08 '16 at 10:00