简体   繁体   中英

How to improve scrolling in android webview?

i'm loading a website into an android webview and i would like to improve scrolling performance. In other words i would like to make scrolling faster and smoother for the user. Any ideas? Thanks in advance

I also encountered sluggish scrolling with erratic fling scrolling when embedding WebView in a fragment inside a HorizontalScrollView, but it seems to happen in other cases also. After days of trial and error, here is a solution that seems to work. The critical part is the OnTouchListener; I don't believe that the WebView settings are that important. It is counterintuitive, but you have to explicitly tell the WebView to scroll... seems like an Android bug.

WebView myWebView;
VelocityTracker mVelocityTracker;

myWebView.setOnTouchListener(new WebViewOnTouchListener());

private class WebViewOnTouchListener implements View.OnTouchListener {
    float currY = 0;
    float yVeloc = 0f;
    WebView localWebView = myWebView;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);
        switch(action){
            case MotionEvent.ACTION_DOWN:
                if (mVelocityTracker == null) {

                    // Retrieve a new VelocityTracker object to watch the velocity
                    // of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                } else {

                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                mVelocityTracker.addMovement(event);
                currY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                mVelocityTracker.computeCurrentVelocity(1000);
                yVeloc = mVelocityTracker.getYVelocity(pointerId);
                float y = event.getY();
                localWebView.scrollBy(0, (int) (currY - y));
                currY = y;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                localWebView.flingScroll(0, -(int)yVeloc);
                break;

        }
        return true;
    }

}

did you declare that you want software rendering in your application's manifest (or by setting the WebView's layer type)? Maybe try hardware mode

webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM