6

i have two horizontal scroll views each containing a linear layout item under it. how is it possible to synchronize the scroll, when either of it is scrolled, the other is also automatically scrolled. any help?

i_raqz
  • 2,919
  • 10
  • 51
  • 87

2 Answers2

3

What you could do is on the onTouch of the first Horizontal Scroll view, record the X position that it started with for an action of Down. Then when you have an action of Move, record the change in the X position. Then you can call the second horizontal scroll view's scrollBy (deltaX, 0). On an action of Up or Cancel, make sure to reset your state variables.

I've done this with a List View scrolling a vertical scroll, just using Y positions instead of X. Here is my code to accomplish this. The concurrentScroller is my vertical view.

if(concurrentScroller != null) {
            int deltaY = (int) (startTouchConcurrentY - ev.getY());
            startTouchConcurrentY = ev.getY();
            concurrentScroller.scrollBy(0, deltaY);                             
        } 
C Nick
  • 2,500
  • 2
  • 17
  • 21
  • This solution doesn't work when flipping, the first view keeps going while as the second just move an deltaY movement. :( – thanhbinh84 Jan 03 '12 at 10:10
  • any idea about this one ? https://stackoverflow.com/questions/58848391/how-do-i-synchronize-scrollview-positions-of-dynamic-horizontal-scroll-views-ins –  Nov 14 '19 at 11:22
1

I would implement onScrollListener for each of the views to call scrollTo on the other.

Alexandru Cristescu
  • 3,898
  • 3
  • 19
  • 22
  • Which views? There is no `onScrollListener` method for `HorizontalScrollView`. – calinb Feb 10 '17 at 18:01
  • ```scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener(){...});``` – Alexandru Cristescu Feb 11 '17 at 22:05
  • Hi Alexandru, any idea about this one ? https://stackoverflow.com/questions/58848391/how-do-i-synchronize-scrollview-positions-of-dynamic-horizontal-scroll-views-ins –  Nov 14 '19 at 11:23