我有很多webviews要在viewpager中显示。每当我拖动webview时,都会执行invidate()来重绘。如果webview很复杂,重绘需要很长时间,所以滚动不流畅。我试过了在 webview 中使用 setDrawingCacheEnabled(true),但它没有效果。有人有想法吗?非常感谢!
问问题
614 次
1 回答
1
一种想法是研究使用 PageChangeListener 来检测聚焦的视图,以及用户是否正在拖动视图。
PageChangeListener 中有一个方法覆盖,如下所示。您可以打开滚动状态来设置一个属性,让您知道什么时候可以重绘/实例化视图,什么时候不可以。
/**
* 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;
}
}
将 pagechangelistener 添加到 viewpager 很容易。
pageChangeListener = new PageChangeListener(viewIndicator, viewAdapter);
viewPager.setOnPageChangeListener(pageChangeListener);
接下来,我将研究如何使用 webview 来防止它在用户主动滚动时重绘。想到的一种想法是在 Web 视图中覆盖无效,并且仅在用户不滚动且视图空闲时才使其无效。
于 2012-09-05T23:21:21.617 回答