Search code examples
androidandroid-layoutscrollviewhorizontalscrollview

Is it possible to have a ScrollView inside another ScrollView?


I want to learn how to solve this problem. I want to have a Horizontal scrollview with the scroll blocked (the user should not be able to scroll it) and inside that horizontal scrollview i want to have another horizontal scroll view, and this scrollview must be able to be scrolled by the user (it haves more content that the width of the screen).

Is it possible to do it?

I tried using this on the parent horizontal scroll view:

((HorizontalScrollView) view).setHorizontalScrollBarEnabled(false);
((HorizontalScrollView) view).setEnabled(false);
((HorizontalScrollView) view).setFocusable(false);
((HorizontalScrollView) view).setFocusableInTouchMode(false);

and this on the child horizontal scroll view:

((HorizontalScrollView) view).requestFocus();

It is not working, the child appears to have a scroll bar, but it cannot be scrolled.

How can this be solved?

PD: I know that this is not a good practice, but I want to learn how to achieve this goal.


Solution

  • You should never use a HorizontalScrollView with a ListView, since ListView takes care of its own scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by HorizontalScrollView.

    http://developer.android.com/reference/android/widget/HorizontalScrollView.html

    UPDATE:
    
    Since you may be forced to use a two dimensional scrollview, you may consider using this:
    

    http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/

    I haven't used this but it may be a reasonable approach.