0

I need to synchronize the scrolling positions of two ScrollViews.

Both ScrollViews contain their individual RecyclerView (I can't put them inside same ScrollView due to design requirements).

How can I achieve that?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121

3 Answers3

0

I highly recommend that you CHANGE YOUR DESIGN but there is a way to achieve this

for first scroll view, define two variable x,y and get current scroll position

x = firstScrollView.getScrollX();
y = firstScrollView.getScrollY();

and pass these values to other scrollView "scrollTo" method.

like:

ScrollView secondScrollView = (ScrollView)findViewById(R.id.scrollView);
secondScrollView.scrollTo(x, y);

if didn't work try:

secondScrollView.post(new Runnable() {
      @Override
      public void run() {
       secondScrollView.scrollTo(x, y);
              }
          });
Hoven
  • 563
  • 1
  • 5
  • 24
0

First of all, use NestedScrollView from Support library since it enables us to listen scroll events easily. Then, set onScrollChange listeners for both NestedScrollView you have. When you receive scroll change for scrollView1 for instance, call scrollTo(...) for scrollView2:

scrollView1.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            scrollView2.setOnScrollChangeListener(null);
            scrollView2.scrollTo(scrollX, scrollY);
            scrollView2.setOnScrollChangeListener(...); //SET SCROLL LISTENER AGAIN

        }
});

Before calling scrollTo(..) for scrollView2, remove its listener, then add it again. Otherwise, scrollTo(..) you call can cause infinite calls in both listeners of NestedScrollViews.

And of course, you need to write similar above code for your scrollView2.

Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
  • no critique just an honest question: why using a ScrollView at all and then even a NestedScrollView? A RecyclerView has its own scroll logic by default (see my answer). – Willi Mentzel Aug 08 '16 at 13:34
  • The question is about syncing ScrollViews. I don't care what s/he wants to put inside them. – Ugurcan Yildirim Aug 08 '16 at 13:40
  • i see, this way your answer would be more universal – Willi Mentzel Aug 08 '16 at 13:43
  • I know that RecyclerView has its own scroll logic by default. But the app I am making demands such action. Actually the design is much complex.... I asked this question to solve a specific design problem which I'm facing. @progressive_overload – Aritra Bhattacharya Aug 09 '16 at 09:00
0

Attach an RecyclerView.OnScrollListener to both RecyclerViews via addOnScrollListener(RecyclerView.OnScrollListener listener).

Update the position of the other one when the event is fired via: scrollTo(int x, int y).

Remember to somehow distinguish between a user-triggered scroll and a programmtic scroll (from the other RecyclerViews event).

One way would be, like Ugurcan Yildirim suggested, to detach the listener of the ScrollView you want to update.

Another option is a boolean flag isUserTriggered. A third way would be to determine which ScrollView currently has the focus to distinguish (see here).

Community
  • 1
  • 1
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121