Android实现真正的禁止WebView滚动

转载地址 http://blog.csdn.net/bdemq/article/details/46775771


这个博客的这个写的不错,借鉴下。

在选择Web的输入框弹出键盘,这时WebView的高度改变重新渲染,如果html调整层级的位置使内容除输入框外不变,先将层级上移再移回原来位置时就会出现闪屏。经测试,有些手机会出现闪屏,有些不会。想到了禁止WebView滚动应该可以解决问题。

     但是如何禁止WebView不可滚动呢?WebView有几个与滚动有关的方法,但是都无效。如:    

    WebView.setScrollContainer(false);
    WebView.setVerticalScrollBarEnabled(false);
    WebView.setHorizontalScrollBarEnabled(false);

   后来看到WebView有个scrollTo(int x, int y)方法,于是重写该方法使其x,y都为0,结果头痛了几天的问题解决了,在此记录下来

[java]  view plain  copy
  1.     public class WebViewMod extends WebView {  
  2.     public EditText mFocusDistraction;  
  3.     public Context mContext;  
  4.     public WebViewMod(Context context) {  
  5.             super(context);  
  6.             init(context);  
  7.         }      
  8.   
  9.         public WebViewMod(Context context, AttributeSet attrs) {  
  10.             super(context, attrs);  
  11.             init(context);  
  12.         }      
  13.   
  14.         public WebViewMod(Context context, AttributeSet attrs, int defStyle) {  
  15.             super(context, attrs, defStyle);  
  16.             init(context);  
  17.         }  
  18.   
  19.         @SuppressLint("NewApi")   
  20.         public WebViewMod(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {  
  21.             super(context, attrs, defStyle, privateBrowsing);  
  22.             init(context);  
  23.         }  
  24.   
  25.         public void init(Context context) {  
  26.             // This lets the layout editor display the view.  
  27.             if (isInEditMode()) return;  
  28.   
  29.             mContext = context;  
  30.   
  31.             mFocusDistraction = new EditText(context);  
  32.             mFocusDistraction.setBackgroundResource(android.R.color.transparent);  
  33.             this.addView(mFocusDistraction);  
  34.             mFocusDistraction.getLayoutParams().width = 1;  
  35.             mFocusDistraction.getLayoutParams().height = 1;  
  36.         }  
  37.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  38.             invalidate();  
  39.                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  40.         }  
  41.   
  42.         @Override  
  43.         public boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY,   
  44.                                     int scrollRangeX, int scrollRangeY, int maxOverScrollX,   
  45.                                     int maxOverScrollY, boolean isTouchEvent) {  
  46.             return false;  
  47.         }  
  48.         /** 
  49.          * 使WebView不可滚动 
  50.          * */  
  51.         @Override  
  52.         public void scrollTo(int x, int y){  
  53.             super.scrollTo(0,0);  
  54.         }  
  55.     }  

猜你喜欢

转载自blog.csdn.net/qq_33756493/article/details/69948549
今日推荐