7

I have a scrollview inside a scrollview . The xml is like this

<RelativeLayout ....
    <ScrollView.....
         <RelativeLayout ....
           <Button.....
           <Button ....
           <ScrollView
             <RelativeLayout ....
                 ..........
               </RelativeLayout>
             </ScrollView>
         </RelativeLayout>
     </ScrollView>
 </RelativeLayout>

in this second scrollview in not scrolling smoothly. can give a solution for that. I tried a lot of solution given in the internet but not working.

6
  • 3
    i dont think scroll view inside scrollview will work Commented Jul 16, 2013 at 8:02
  • can u tell me the exact requirmrnt Commented Jul 16, 2013 at 8:02
  • 2
    Its not a good practice to have 2 scrollViews which scroll in the same direction inside one another, even if it works Commented Jul 16, 2013 at 8:03
  • You do not need to use scrollview inside scrollview use can scroll inner scroll from the outer scroll. Commented Jul 16, 2013 at 8:04
  • its not a good practice to have scrollview inside a scrollview, you might want to think of your layout design again. :) Commented Jul 16, 2013 at 8:09

3 Answers 3

21

Try this code. It is working for me`

 parentScrollView.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event)
{
    findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event)
{

// Disallow the touch request for parent scroll on touch of
// child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});`
Sign up to request clarification or add additional context in comments.

2 Comments

for me this does noting, the childScrollView wont receive its ontouch whatever i do.
Can anyone explain what does it do to the parent of the childscroll.. How does it avoid the parent scroll view to stop scrolling and allow the child to scroll
7

A different solution is to use this class as the parent class

public class NoInterceptScrollView extends ScrollView {

    public NoInterceptScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

}

1 Comment

I would prefer this as a simpler solution
0

I had to improve Deepthi's solution since it didn't work for me; I guess because my child scrollview is full of views (I mean child views use all of the scrollview drawing space). To make it fully functional, I had to also disallow the touch request for parent scroll on touch of all children views inside the child scroll view:

parentScrollView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event)
    {
        findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false);
        return false;
    }
});
childScrollView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event)
    {
        // Disallow the touch request for parent scroll on touch of
        // child view
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});`
childScrollviewRecursiveLoopChildren(parentScrollView, childScrollView);
public void childScrollviewRecursiveLoopChildren(final ScrollView parentScrollView, View parent) {
    for (int i = ((ViewGroup) parent).getChildCount() - 1; i >= 0; i--) {
        final View child = ((ViewGroup) parent).getChildAt(i);
        if (child instanceof ViewGroup) {
            childScrollviewRecursiveLoopChildren(parentScrollView, (ViewGroup) child);
        } else {
            child.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event)
                {
                    // Disallow the touch request for parent scroll on touch of
                    // child view
                    parentScrollView.requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.