Skip to content

Instantly share code, notes, and snippets.

@alorma
Created February 6, 2015 13:02
Show Gist options
  • Save alorma/7a9fe52082bbce7b2be6 to your computer and use it in GitHub Desktop.
Save alorma/7a9fe52082bbce7b2be6 to your computer and use it in GitHub Desktop.
public class ObservableWebView extends WebView {
public ObservableWebView(Context context) {
this(context, null);
}
public ObservableWebView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ObservableWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
isInEditMode();
}
public OnBottomReachedListener mOnBottomReachedListener = null;
private int mMinDistance = 0;
/**
* Set the listener which will be called when the WebView is scrolled to within some
* margin of the bottom.
*
* @param bottomReachedListener
* @param allowedDifference
*/
public void setOnBottomReachedListener(OnBottomReachedListener bottomReachedListener, int allowedDifference) {
mOnBottomReachedListener = bottomReachedListener;
mMinDistance = allowedDifference;
}
/**
* Implement this interface if you want to be notified when the WebView has scrolled to the bottom.
*/
public interface OnBottomReachedListener {
void onBottomReached(View v);
}
@Override
protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) {
if (mOnBottomReachedListener != null) {
if ((getContentHeight() - (top + getHeight())) <= (getContentHeight() / 2)) {
mOnBottomReachedListener.onBottomReached(this);
}
}
super.onScrollChanged(left, top, oldLeft, oldTop);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment