I would like to have a linearlayout with a header section on top and a webview below. The header will be short and the webview may be longer and wider than the screen.
What is the best way to get horizontal and vertical scrolling? Is a ScrollView nested inside a HorizontalScrollView a good idea?
Yes, and no.
Yes, my understanding is that
ScrollView
andHorizontalScrollView
can be nested.No, AFAIK, neither
ScrollView
norHorizontalScrollView
work withWebView
.I suggest that you have your
WebView
fit on the screen.For a while I've been trying solutions from here, but the one that worked best still had one problem: It ate all events, none were making it through to elements within the scroller.
So I've got ... yet another answer, in Github and well-commented at least hopefully: https://github.com/Wilm0r/giggity/blob/master/app/src/main/java/net/gaast/giggity/NestedScroller.java
Like all solutions, it's a nested HorizontalScrollview (outer) + ScrollView (inner), with the outer receiving touch events from Android, and the inner receiving them only internally from the outer view.
Yet I'm relying on the ScrollViews to decide whether a touch event is interesting and until they accept it, do nothing so touches (i.e. taps to open links/etc) can still make it to child elements.
(Also the view supports pinch to zoom which I needed.)
In the outer scroller:
vscroll here is the "InnerScroller", subclassed from ScrollView, with a few changes to event handling: I've done some terrible things to ensure incoming touch events directly from Android are discarded, and instead it will only take them from the outer class - and only then pass those on to the superclass:
I've try both wasikuss and user1684030 solutions and I had to adapt them because of one warning log:
HorizontalScrollView: Invalid pointerId=-1 in onTouchEvent
, and because I wasn't fan of this need of creating 2 scroll views.So here is my class:
And when using it in XML, you have nothing to do if the content of this scroll view is set in here. Otherwise, you just need to call the method
setContent(View content)
in order to let thisScrollView2D
knows what is its content.For instance:
Two years further down the line I think the open source community might have to your rescue: 2D Scroll View.
Edit: The Link doesn't work anymore but here is a link to an old version of the blogpost;
There is an easy workaround: In you activity get a reference to the outer scrollView (I'm going to assume a vertical scrollview) and a reference to the first child of that scroll view.
One could argue that this solution is a bit hacky. But it has worked great for me in several applications!
Late to answer, but hopefully might be helpful to someone. You can check out droid-uiscrollview. This is heavily based on @MrCeeJ's answer, but I seemed to have a lot of trouble getting the actual content to be rendered. Hence I pulled in the latest source from HorizontalScrollView & ScrollView to create droid-uiscrollview. There are a few
todo's
left which I haven't gotten around to finish, but it does suffice to get content to scroll both horizontally & vertically at the same time