3

I have a wevbiew with a number of 'pages' (columns of text) in it, and want to be able to scroll horizontally between columns in response to Fling gestures. I can do this fine using scrollTo(), but that is rather sudden and I'd really like to be able to do a smooth transition from one 'page' to the next.

The problem as I see it: you can't attach a Scroller to a WebView, and shouldn't nest a WebView in a ScrollView.

Does anyone have any good ideas for implementing smooth or animated horizontal scrolling in a WebView?

Edited to add: I'm trying to implement an idea of knotmanish's where I use an unattached Scroller to compute the distance..

    // vx and vy are from the onfling event of the gesture detector 
    scroller.fling(page*x, 0, vx, vy, page*(x + 1), page*(x + 1), 0, 0);

    while(scroller.computeScrollOffset()){
        webview.scrollTo(scroller.getCurrX(), 0);
        webview.invalidate();
    }

... but the while loop seems too slow to capture the scroller's movement, or something, because the behavior looks just like the scrollTo() method (the view jump to the end of the scroll).

Turnsole
  • 3,422
  • 5
  • 30
  • 52

1 Answers1

4

You cannot attach a scroller to a webview however you can make use of the gesture to do the same.

The gesture detects the fling using this method

onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)

Give the velocityX, velocityY to the scoller method along with other parameters

fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)

scan the computeScrollOffset() value to find the getCurrX(),getCurrY() positions.

Extend the webview and override the methods onTouchEvent(MotionEvent ev), dispatchTouchEvent(MotionEvent ev) to return false so that by default the touch events are not consumed by the webview itself. Now implement the gestureListener and override the onfling method mentioned above. pass the velocity and other parameters to the scroller. Start a loop to scan the computeScrollOffset() value to find the getCurrX() and getCurrY() and invalidate the view each time. Pass this value to scroll the webview as needed.

Please let me know in case you need anything.

Manish Khot
  • 3,027
  • 2
  • 29
  • 37
  • Okay, I'll give that a try in the morning, and post my results. – Turnsole Mar 04 '11 at 07:38
  • Eh... what can I attach it to that would make the webview scroll? If it's not attached to something, nothing happens, and the layout containing the webview never stretches so it's not going to scroll. – Turnsole Mar 04 '11 at 21:39
  • You might need to set your zoom level. Try setting it to 1. – Lucas B Mar 07 '11 at 18:30
  • I think I understand ... essentially have the scroller just to calculate the right scroll positions, not attached to anything, and use those values in scrollTo? I'll give that a try. – Turnsole Mar 09 '11 at 02:52
  • I've tried this (code snippet above) and it goes straight to the end of the fling, in effect, the same jerky behavior. Can you spot something I did wrong, or do you think the loop just isn't capturing the scroller's movement as fast as it would need to? – Turnsole Mar 09 '11 at 06:40
  • Well, I don't think I'm going to get any real answer on this, so I'll keep working on it. Thanks for the effort. – Turnsole Mar 12 '11 at 03:52
  • i had tried this while loop at the end of onDraw() method and it worked for me (Not for webview but for TableLayout). – Manish Khot Mar 12 '11 at 17:32
  • Which onDraw() method were you overriding? – Turnsole Mar 14 '11 at 03:17