webView监听是否滑动到底部

来源:互联网 发布:域名系统dns的作用是 编辑:程序博客网 时间:2024/05/17 23:46

在使用webView时,经常需要知道webView滑动了多少,是否滑动到底部,webView高度是多少等等?

其实webView 中有个方法onScrollChanged()函数可以计算滑动高度等

1.实现

1.1、自定义webView

public class MyWebView extends WebView {    public ScrollInterface mScrollInterface;    public MyWebView (Context context) {        super(context);    }    public MyWebView (Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public MyWebView (Context context, AttributeSet attrs) {        super(context, attrs);    }    //实时滑动监控    //参数l代表滑动后当前位置,old代表原来原值    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {        super.onScrollChanged(l, t, oldl, oldt);        mScrollInterface.onSChanged(l, t, oldl, oldt);    }    //供外部调用,监控滑动    public void setOnCustomScroolChangeListener(ScrollInterface scrollInterface) {        this.mScrollInterface = scrollInterface;    }    public interface ScrollInterface {        public void onSChanged(int l, int t, int oldl, int oldt);    }}

1.2、布局文件xml

 <cn.myPackage.MyWebView        android:id="@+id/webView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:text="@string/hello_world"        android:layout_centerInParent="true"    />

1.3、activity调用setOnCustomScrollChangeListemer()实现滑动判断

 private void webViewScroolChangeListener() {    mYWebView.setOnCustomScroolChangeListener(new ScrollInterface() {                   @Override        public void onSChanged(int l, int t, int oldl, int oldt) {                          //WebView的总高度        float webViewContentHeight=mTestWebView.getContentHeight() * mTestWebView.getScale();                       //WebView的现高度        float webViewCurrentHeight=(mTestWebView.getHeight() + mTestWebView.getScrollY());        if ((webViewContentHeight-webViewCurrentHeight) == 0) {            System.out.println("WebView滑动到了底端");            }        }    });    }
0 0
原创粉丝点击