Android判斷WebView是否滑動(dòng)到頂部和底部

直接上代碼:

自定義一個(gè)Webview

public class TermsWebView extends WebView {

    ScrollInterface mScrollInterface;
// 構(gòu)造函數(shù)
    public TermsWebView(Context context) {
        super(context);
    }
// 構(gòu)造函數(shù)
    public TermsWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
// 構(gòu)造函數(shù)
    public TermsWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
// 復(fù)寫(xiě) onScrollChanged 方法,用來(lái)判斷觸發(fā)判斷的時(shí)機(jī)
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mScrollInterface != null){
            mScrollInterface.onSChanged(l, t, oldl, oldt);
        }
    }
// 定義接口
    public void setOnCustomScrollChangeListener(ScrollInterface mInterface) {
        mScrollInterface = mInterface;
    }

    public interface ScrollInterface {
        void onSChanged(int scrollX, int scrollY, int oldScrollX, int oldScrollY);
    }
}

使用的地方:

mWebView.setOnCustomScrollChangeListener((scrollX, scrollY, oldScrollX, oldScrollY) -> {
           // isLoadError 判斷webview是否加載成功
             if (!mWebView.canScrollVertically(1) && !isLoadError) {  
                   mAgreeText.setEnabled(true); // button 可點(diǎn)擊
                 }
           });

這個(gè)地方使用自定義的接口方式是因?yàn)椋?/p>

setOnScrollChangeListener.png

其中用來(lái)判斷能否滑動(dòng)的關(guān)鍵方法是 :canScrollVertically()。這個(gè)方法是View的方法,直接上源碼:

 /**
     * Check if this view can be scrolled horizontally in a certain direction.
     *
     * @param direction Negative to check scrolling left, positive to check scrolling right.
     * @return true if this view can be scrolled in the specified direction, false otherwise.
     */
    public boolean canScrollHorizontally(int direction) {
        final int offset = computeHorizontalScrollOffset();
        final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
        if (range == 0) return false;
        if (direction < 0) {
            return offset > 0;
        } else {
            return offset < range - 1;
        }
    }

    /**
     * Check if this view can be scrolled vertically in a certain direction.
     *
     * @param direction Negative to check scrolling up, positive to check scrolling down.
     * @return true if this view can be scrolled in the specified direction, false otherwise.
     */
    public boolean canScrollVertically(int direction) {
        final int offset = computeVerticalScrollOffset();
        final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
        if (range == 0) return false;
        if (direction < 0) {
            return offset > 0;
        } else {
            return offset < range - 1;
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容