Skip to content

Instantly share code, notes, and snippets.

@canujohann
Created January 27, 2016 02:25
Show Gist options
  • Save canujohann/03e2cfafb4344ca6c6cc to your computer and use it in GitHub Desktop.
Save canujohann/03e2cfafb4344ca6c6cc to your computer and use it in GitHub Desktop.
observable-webview

observable Webview

from http://stackoverflow.com/questions/14752523/how-to-make-a-scroll-listener-for-webview-in-android

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;
    }

    /**
     * Impliment in the activity/fragment/view that you want to listen to the webview
     */
    public static interface OnScrollChangedCallback
    {
        public void onScroll(int l, int t);
    }
}
wv = (ObservableWebView) findViewById(R.id.scorllableWebview);
 wv.setOnScrollChangedCallback(new OnScrollChangedCallback(){
    public void onScroll(int l, int t){
      //Do stuff
      Log.d(TAG,"We Scrolled etc...");
    }
 });
@anass05
Copy link

anass05 commented Dec 28, 2019

works like a charm, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment