问题
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).
回答1:
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.
来源:https://stackoverflow.com/questions/5172777/smooth-horizontal-scrolling-in-webview