Android禁用webview滚动但不禁用内部scrollview

时间:2015-05-21 10:35:35

标签: android scroll webview

在我的Android应用程序中,我有一个webview,在加载网站时,我想禁用webview的滚动,但不禁用网站中的内部滚动条。

例如, https://stackoverflow.com/questions/ask 我想禁用最多的外部滚动条,因为我的浏览器很小,但我想让问题列表中的滚动条仍然有效。

1 个答案:

答案 0 :(得分:1)

所以,这是我的答案,我觉得拥有这个解决方案是愚蠢的...但客户的要求,我希望没有人会永远使用它......

public class NoScrollWebView extends WebView implements GestureDetector.OnGestureListener  {

private GestureDetector gestureDetector;
private WebViewActionListener mWebViewActionListener;


public NoScrollWebView(Context context) {
    super(context);
    initUI();
}

public NoScrollWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initUI();
}

public NoScrollWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initUI();
}


private void initUI(){
    gestureDetector = new GestureDetector(getContext(), this);
    setOnTouchListener(webviewOnTouchLIstener);
    initialWebViewSettings();
}

private void initialWebViewSettings() {

    WebSettings webSettings = getSettings();
    webSettings.setSupportZoom(false);
    webSettings.setBuiltInZoomControls(false);
    webSettings.setDisplayZoomControls(false);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setDomStorageEnabled(true);

    setHorizontalScrollBarEnabled(false);
    setVerticalScrollbarOverlay(false);
}

@Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt)
{
    super.onScrollChanged(0, 0, 0, 0);
    this.scrollTo(0,0);
}


private View.OnTouchListener webviewOnTouchLIstener = new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        // Avoid multipe touch point
        if(event.getPointerCount() > 1){
            return true;
        }

        if (gestureDetector.onTouchEvent(event)) {
            // single tap
            return true;
        }else {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    requestDisallowInterceptTouchEvent(true);
                    return false;
                }
                case MotionEvent.ACTION_MOVE: {
                    return false;
                }
                case MotionEvent.ACTION_UP: {
                    requestDisallowInterceptTouchEvent(false);
                    return false;
                }
                default:
                    return gestureDetector.onTouchEvent(event);

            }
        }
    }
};


@Override
public boolean onDown(MotionEvent e) {
    Log.e("SFD", "m_downX onDown");
    scrollTo(0, 0);
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    Log.e("SFD", "m_downX onSingleTapUp");
    scrollTo(0, 0);
    if(mWebViewActionListener != null) {
        mWebViewActionListener.onTapped();
    }
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    Log.e("SFD", "m_downX onScroll");
    scrollTo(0, 0);
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
    Log.e("SFD", "m_downX onLongPress");

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    Log.e("SFD", "m_downX onFling");
    scrollTo(0, 0);
    return true;
}


public interface WebViewActionListener {
    void onTapped();
}

public void setWebViewActionListener(WebViewActionListener listener){
    this.mWebViewActionListener = listener;
}