I have a webview which shows an html file. When the user scrolls to the bottom of this file in webview, I want a button that was previously hidden to show up, which the user
Solutions above didn't fully work for me for the similar issue (hide button while webView is being scrolled, show after scrolling is over). The reason I wanted it to hide while scrolling is because button I want to hide is for jumping to the very bottom of webview, and when it only worked for me when webview is static, but didn't jump to bottom while view is still being scrolled. So I did the following:
added a onScrollChanged callback to overridden webView, like suggested nearby:
private OnScrollChangedCallback mOnScrollChangedCallback;
public OnScrollChangedCallback getOnScrollChangedCallback() {
return mOnScrollChangedCallback;
}
public void setOnScrollChangedCallback(
final OnScrollChangedCallback onScrollChangedCallback) {
mOnScrollChangedCallback = onScrollChangedCallback;
}
@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.onScrollChanged(l, t);
}
}
/**
* Implement in the activity/fragment/view that you want to listen to the
* webview
*/
public static interface OnScrollChangedCallback {
public void onScrollChanged(int l, int t);
}
and in my activity class which implements OnScrollChangedCallback
UPDATED:
Timer timer2showJumpButton;
private long lastScrollEventTimestamp;
public final static int HIDING_JUMP_BUTTON_ON_SCROLL_DELAY = 500;
public void onScrollChanged(int l, int t) {
// showing button when scrolling starts
if (btnJumpToBottom != null) {
btnJumpToBottom.setVisibility(View.VISIBLE);
}
if (btnJumpToTop!= null) {
btnJumpToTop.setVisibility(View.VISIBLE);
}
if (timer2showJumpButton == null) {
final Runnable r2 = new Runnable() {
@Override
public void run() {
if (btnJumpToBottom != null) {
btnJumpToBottom.setVisibility(View.GONE);
}
if (btnJumpToTop!= null) {
btnJumpToTop.setVisibility(View.GONE);
}
}
};
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
if (btnJumpToTop.getVisibility() == View.VISIBLE || btnJumpToBottom.getVisibility() == View.VISIBLE){
long currentTimestamp = System.currentTimeMillis();
if (currentTimestamp - lastScrollEventTimestamp > HIDING_JUMP_BUTTON_ON_SCROLL_DELAY1 ){
webView.postDelayed(r2, HIDING_JUMP_BUTTON_ON_SCROLL_DELAY);
}else{
//too soon
}
}
}
};
try {
timer2showJumpButton = new Timer();
timer2showJumpButton.schedule(timerTask, 500, 500);
} catch (IllegalStateException e) {
logger.warn(TAG + "/onScrollChanged/" + e.getMessage());
}
}
// adding runnable which will hide button back
long currentTimestamp = System.currentTimeMillis();
lastScrollEventTimestamp = currentTimestamp;
}