Karthik T
Karthik T

Reputation: 31972

How to disable scrolling on a scroll view without blocking scrolling

I am working on an app on android which simulates parallax scrolling with a top scrollview, the content and the bottom scroll view.

I would like to scroll using only the middle scrollview, but I am able to independently scroll the top view manually. This I would like to prevent.

I tried this from another related question, but this seems to disable scrolling in this case.. What I would like is for the touch to fall through to the 2nd layer and for that to handle the scrolling.

.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return true; } });

Is this a call for onInterceptTouchEvent? I am still trying to understand that. Let me know if this is not enough information or if it isnt clear.

tl;dr - I want my scroll view to be completely passive touch wise, act as if it doesn't exist, and pass the touch to lower layers.

Upvotes: 0

Views: 859

Answers (2)

Karthik T
Karthik T

Reputation: 31972

My solution is inspired by @Parvaz Bhaskar's solution, but a lot simpler... indeed so simple that i'm surprised that it didn't occur to me when i thought of my question.

To pass the touch to the layer below... I just pass the touch to the layer below

grassView.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
           mViewPager.onTouchEvent(motionEvent); //Scroll the view pager so that scrolling works through the grass.
           return true; // NO SCROLLING FOR YOU!  http://stackoverflow.com/a/10481239/1520364
       }
 });

In my case I have a layer on top which is the grassView here, and a view pager below which has my actual tabs that that need to scroll. So I swallow the touch on the grass level, and pass it to my ViewPager

Upvotes: 0

Parvaz Bhaskar
Parvaz Bhaskar

Reputation: 1367

This is just an idea and I haven't tested it but I think the following steps should work:

1.On your top scrollView,intercept the onTouch event(monitor especially the ACTION_MOVE event);

2.Take a handle of the scrollview that you actually want to move. and use ScrollView.smoothScrollto(int x,int y) function to move that scrollView programmatically.Pass the getX() and getY() values that you receive in this motionEvent, thus allowing you to pass the touch to lower layers.

refer the android doc of smoothScrollto for clarity.

3.Obviously as a last note , you should be swallowing the touches on the lower scrollView that you'll be moving programmatically.This will avoid the lower scrollView to move on its own onTouch.

Since you said you want to implement parallax you can multiply the x and y values received in step #2 by some factor thus creating a parrallax effect.

You can easily find code snippets for above steps on SO. Best of luck!

EDIT : refer this link if you want to get the onScroll event when the uppermost scrollView scrolls.

Upvotes: 1

Related Questions