Android实现真正的禁止WebView滚动

本文介绍了一种解决在移动端Web开发中,当输入框弹出键盘导致WebView高度变化而引发闪屏问题的方法。通过重写WebView的scrollTo方法,并始终将其参数设置为(0,0),成功避免了因键盘弹出引起的页面滚动,从而解决了闪屏问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载地址 http://blog.youkuaiyun.com/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.     }  

### 禁用 Android WebView 的滑动功能 为了在 Android 应用程序中禁用 `WebView` 组件的滑动行为,可以通过重写 `overScrollBy()` 方法来实现这一目标。具体来说,在自定义的 `WebView` 类中覆盖此方法并返回特定布尔值可以控制是否允许滑动[^3]。 ```java public class NonScrollableWebView extends WebView { private boolean disableScroll; public NonScrollableWebView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) { if (disableScroll) { return false; } return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent); } public void setDisableScroll(boolean disableScroll) { this.disableScroll = disableScroll; } } ``` 上述代码展示了创建一个新的名为 `NonScrollableWebView` 的类继承自原生 `WebView` 并修改其滚动逻辑。通过设置 `setDisableScroll(true)` 即可关闭该组件内的任何垂直方向上的移动[^4]。 另外一种方式是在布局文件里直接使用 XML 属性配置: ```xml <com.example.NonScrollableWebView android:id="@+id/non_scroll_web_view" android:layout_width="match_parent" android:layout_height="wrap_content"/> ``` 之后可以在 Activity 或 Fragment 中初始化这个控件,并调用相应的方法启用或禁用它的滚动特性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值