Search code examples
androidhorizontalscrollview

synchronize two horizontal scroll view android


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?


Solution

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