wen dao
wen dao

Reputation: 21

How to flip smooth with webviews in viewpager

I have many webviews to show in a viewpager.Whenever I drag the webview,invidate() is executed to redraw.If the webview is much complex,it take a long time to redraw,so the scroll is not smooth.I have tried to use setDrawingCacheEnabled(true) with the webview,but it is not effective.Any one have ideas? Thanks very much!

Upvotes: 2

Views: 632

Answers (1)

josh527
josh527

Reputation: 6999

One thought, is to look into using a PageChangeListener to detect what view is focused, and whether the user is dragging the views.

There is a method override in PageChangeListener that looks like this. You could switch on the scroll state to set a property that would let you know when you are okay to redraw/instantiate views, and when you aren't.

/**
 * Called when the scroll state changes. Useful for discovering when the user
 * begins dragging, when the pager is automatically settling to the current page,
 * or when it is fully stopped/idle.
 *
 * @param state The new scroll state.
 * @see ViewPager#SCROLL_STATE_IDLE
 * @see ViewPager#SCROLL_STATE_DRAGGING
 * @see ViewPager#SCROLL_STATE_SETTLING
 */
@Override
public void onPageScrollStateChanged(int state) {
    switch (state) {
    case ViewPager.SCROLL_STATE_IDLE:
        // Allow updating of views by view adapter
        this.isAnimating = false;
        break;
        /**
         * Indicates that the pager is currently being dragged by the user
         */
    case ViewPager.SCROLL_STATE_DRAGGING:
        // do not allow updating of views
        this.isAnimating = true;
        break;
        /**
         * Indicates that the pager is in the process of settling to a final position.
         */
    case ViewPager.SCROLL_STATE_SETTLING:
        this.isAnimating = true;
        break;
    }
}

Adding a pagechangelistener to the viewpager is easy.

pageChangeListener = new PageChangeListener(viewIndicator, viewAdapter);
viewPager.setOnPageChangeListener(pageChangeListener);

Next I would look into what you could do with the webview to keep it from redrawing when the user is actively scrolling. One thought that comes to mind would be to override invalidate in the web view, and have it only invalidate when the user is not scrolling, and the view is idle.

Upvotes: 1

Related Questions