解決方案並沒有完全爲我工作了類似的問題(隱藏按鈕,而webView正在滾動,滾動結束後顯示)。我想要的原因它在滾動時隱藏是因爲我想隱藏的按鈕是跳到webview的最底層,當它只在webview是靜態時才起作用,但在視圖仍在滾動時沒有跳到底部。 所以我做了以下內容:
增加了onScrollChanged回調覆蓋web視圖,如建議附近:
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);
}
,並在我的活動類,它實現OnScrollChangedCallback
更新:
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;
}
雖然webview沒有引用按鈕。 – Daniel