如何检测webivew的滚动结束

时间:2016-11-23 13:40:02

标签: android scroll webview

我有一个自定义WebView,其中包含以下代码:

public class ObservableWebView extends WebView {

    private OnScrollChangedCallback mOnScrollChangedCallback;

    public ObservableWebView(final Context context) {
        super(context);
    }

    public ObservableWebView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
    }

    public OnScrollChangedCallback getOnScrollChangedCallback() {
        return mOnScrollChangedCallback;
    }

    public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback) {
        mOnScrollChangedCallback = onScrollChangedCallback;
    }

    /**
     * Implement in the activity/fragment/view that you want to listen to the webview
     */
    public static interface OnScrollChangedCallback {
        public void onScroll(int l, int t);
    }
} 

我使用以下方法来检测滚动是否已到达结尾:

@Override
    public void onScroll(int l, int t) {
        int tek = (int) Math.floor(webView.getContentHeight() * webView.getScale());
        if(tek - webView.getScrollY() == webView.getHeight())
            Toast.makeText(getActivity(), "End", Toast.LENGTH_SHORT).show();
    }

问题是它总是不能工作并且有时会失败。是否有更可靠的方法来检测webview的滚动结束,该结果向后兼容到api 16级?

0 个答案:

没有答案