Android monitors the sliding direction of webview and whether it slides to the top or bottom

Judging the sliding direction of webview, a commonplace question, mentioned again.

To monitor whether the webview slides to the bottom or top, you can judge by resetting the onScrollChanged() or onTouchEvent() of the webview.

Among them, the sliding direction of the webview can be determined by rewriting onTouchEvent().

Judging whether to slide to the bottom is obtained by comparing the height of the webview with the height of the current webview.

Judging whether to slide to the top is obtained by judging getScrollY() == 0 of webview.

The first one is to override onScrollChanged(), which is passed to the external call through the callback.

public class McWebViewScroll extends WebView {

    public McWebViewScroll(Context context) {
        super(context);
    }

    public McWebViewScroll(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public McWebViewScroll(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        // webview的高度
        float webcontent = getContentHeight() * getScale();
        // 当前webview的高度
        float webnow = getHeight() + getScrollY();

        if (Math.abs(webcontent - webnow) < 1) {
            //处于底端
            Log.e("测试","处于底端");

            if(mOnScrollChangeListener !=null){
                mOnScrollChangeListener.onPageEnd(l, t, oldl, oldt);
            }
        } else if (getScrollY() == 0) {
            //处于顶端
            Log.e("测试","处于顶端");
            if(mOnScrollChangeListener !=null){
                mOnScrollChangeListener.onPageTop(l, t, oldl, oldt);
            }
        } else {
            if(mOnScrollChangeListener !=null){
                mOnScrollChangeListener.onScrollChanged(l, t, oldl, oldt);
            }
        }
    }

    private OnScrollChangeListener mOnScrollChangeListener;

    public void setOnScrollChangeListener(OnScrollChangeListener listener) {
        this.mOnScrollChangeListener = listener;
    }

    public interface OnScrollChangeListener {

        public void onPageEnd(int l, int t, int oldl, int oldt);

        public void onPageTop(int l, int t, int oldl, int oldt);

        public void onScrollChanged(int l, int t, int oldl, int oldt);

    }



}

transfer:
 

        webView.setOnScrollChangeListener(object :McWebViewScroll.OnScrollChangeListener{
            override fun onPageEnd(l: Int, t: Int, oldl: Int, oldt: Int) {
                
            }

            override fun onPageTop(l: Int, t: Int, oldl: Int, oldt: Int) {
            }

            override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
            }

        })

The second is to rewrite onTouchEvent() to determine the sliding direction and pass it to the external call through the callback.

public class McWebViewScroll extends WebView {

    public McWebViewScroll(Context context) {
        super(context);
    }

    public McWebViewScroll(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public McWebViewScroll(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }
    

    public interface McEventListener {
        void onEventUp();
        void onEventDown();
        void onEventPageTop();
        void onEventPageEnd();
    }

    private McEventListener mEventListener;

    public void setMcEventListener(McEventListener listener) {
        mEventListener = listener;
    }

    private int lastScrollY;
    private boolean scrollFx = true;//true 向下滑 false 向上滑

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN:
                lastScrollY = this.getScrollY();

            case MotionEvent.ACTION_MOVE:
                if (Math.abs(lastScrollY - this.getScrollY()) > 10) {
                    if (lastScrollY > this.getScrollY()) {//向下滑
                        scrollFx = true;
                        Log.e("测试", "向下滑");
                    } else {//向上滑
                        scrollFx = false;
                        Log.e("测试", "向上滑");
                    }

                    lastScrollY = this.getScrollY();

                    if (scrollFx) {
                        Log.e("测试", "下滑 ");
                        if (mEventListener != null) {
                            mEventListener.onEventDown();
                        }
                        if (getScrollY() == 0){
                            Log.e("测试", "下滑 到头了");
                            if (mEventListener != null) {
                                mEventListener.onEventPageTop();
                            }
                        }
                    } else {
                        Log.e("测试", "上滑 ");
                        if (mEventListener != null) {
                            mEventListener.onEventUp();
                        }
//                    // webview的高度
//                    float webcontent = getContentHeight() * getScale();
//                    // 当前webview的高度
//                    float webnow = getHeight() + getScrollY();
                        if((this.getHeight() + this.getScrollY())>=(this.getContentHeight() * this.getScale())){
                            Log.e("测试", "上滑 到底了");
                            if (mEventListener != null) {
                                mEventListener.onEventPageEnd();
                            }
                        }
                    }
                }




            case MotionEvent.ACTION_UP:
                if (getScrollY() == 0){
                    Log.e("测试", "下滑 到头了");
                    if (mEventListener != null) {
                        mEventListener.onEventPageTop();
                    }
                }
                
                if((this.getHeight() + this.getScrollY())>=(this.getContentHeight() * this.getScale())){
                    Log.e("测试", "上滑 到底了");
                    if (mEventListener != null) {
                        mEventListener.onEventPageEnd();
                    }
                }
                break;
        }

        return super.onTouchEvent(ev);
    }


}

Among them, rewriting onTouchEvent() can judge the sliding direction by the way, scrollFx, true slide down, false slide up.

In order to ensure accuracy when judging whether to slide to the bottom or top, it is judged again when MotionEvent.ACTION_UP lifts the finger~~

            case MotionEvent.ACTION_UP:
                if (getScrollY() == 0){
                    Log.e("测试", "下滑 到头了");
                    if (mEventListener != null) {
                        mEventListener.onEventPageTop();
                    }
                }

                if((this.getHeight() + this.getScrollY())>=(this.getContentHeight() * this.getScale())){
                    Log.e("测试", "上滑 到底了");
                    if (mEventListener != null) {
                        mEventListener.onEventPageEnd();
                    }
                }

transfer:

        webView.setMcEventListener(object :McWebViewScroll.McEventListener{
            override fun onEventUp() {
            }

            override fun onEventDown() {
            }

            override fun onEventPageTop() {
            }

            override fun onEventPageEnd() {
            }

        })

end--------------------------------------------


In the nested webview of SwipeRefreshLayout, sometimes when the top of the webview is not fully displayed, sliding down will evoke the pull-down refresh of SwipeRefreshLayout.
Solution:

It can be used to determine whether to enable the refresh by judging whether to slide to the top of the webview~

public class McWebViewScroll extends WebView {

    public McWebViewScroll(Context context) {
        super(context);
    }

    public McWebViewScroll(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public McWebViewScroll(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        //四个参数分别对应,当前水平滚动的距离,当前垂直滚动的距离,上一次水平滚动的距离,上一次垂直滚动的距离
        super.onScrollChanged(l, t, oldl, oldt);
        if (mScrollListener != null) {
            mScrollListener.onScrollChanged(t);
        }
    }

    public interface IScrollListener {
        void onScrollChanged(int scrollY);
    }

    private IScrollListener mScrollListener;

    public void setOnScrollListener(IScrollListener listener) {
        mScrollListener = listener;
    }
}

use:

        mBinding.webWv.setOnScrollListener(object : McWebViewScroll.IScrollListener {
            override fun onScrollChanged(scrollY: Int) {
                if (scrollY == 0) {
                    //启用下拉刷新

                } else {
                    //禁止下拉刷新

                }
            }

        })

Guess you like

Origin blog.csdn.net/NewActivity/article/details/129555644