Android:滚动完成webview后,可以看到一个按钮

时间:2012-06-08 21:51:20

标签: android delegates scroll android-webview

我有一个显示html文件的webview。当用户在webview中滚动到此文件的底部时,我想要一个先前隐藏的按钮显示,然后用户可以按此按钮进行一些活动

我在iOS中做了类似的事情,我只是将委托设置为ViewController,只是将按钮设置为可见。我如何在Android上做类似的事情?我注意到iOS中没有回调方法。

编辑:现在,我有一个包含2个对象的活动:包含我的文本的webview和一个当前不可见的按钮。我希望我的活动在webview文本滚动到底部时收到消息,并使按钮可见

5 个答案:

答案 0 :(得分:4)

我必须自己这样做,以便在用户滚动到EULA底部后显示“我同意”按钮。律师,嗯?

实际上当你覆盖WebView(而不是像@JackTurky的答案中的ScrollView)时,你可以调用getContentHeight()来获取内容的高度,而不是getBottom()返回可见的底部而不是有用。

这是我全面的解决方案。据我所知,这是所有API级别1的东西,所以它应该可以在任何地方使用。

public class EulaWebView extends WebView {
    public EulaWebView(Context context) 
    {
        this(context, null);
    }

    public EulaWebView(Context context, AttributeSet attrs) 
    {
        this(context, attrs, 0);
    }

    public EulaWebView(Context context, AttributeSet attrs, int defStyle) 
    {
        super(context, attrs, defStyle);
    }

    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())) <= mMinDistance )
                mOnBottomReachedListener.onBottomReached(this);
        }
        super.onScrollChanged(left, top, oldLeft, oldTop);
    }

}

一旦用户滚动到WebView的底部,我用它来显示“我同意”按钮,我在这里调用它(在“实现OnBottomReachedListener”的类中:

EulaWebView mEulaContent;
Button mEulaAgreed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.eula);
    mEulaContent = (EulaWebView) findViewById(R.id.eula_content);
    StaticHelpers.loadWebView(this, mEulaContent, R.raw.stylesheet, StaticHelpers.readRawTextFile(this, R.raw.eula), null);
    mEulaContent.setVerticalScrollBarEnabled(true);
    mEulaContent.setOnBottomReachedListener(this, 50);

    mEulaAgreed = (Button) findViewById(R.id.eula_agreed);
    mEulaAgreed.setOnClickListener(this);
    mEulaAgreed.setVisibility(View.GONE);
}

@Override
public void onBottomReached(View v) {
    mEulaAgreed.setVisibility(View.VISIBLE);
}

因此,当达到底部时(或者在这种情况下,当它们在50像素范围内时),会出现“我同意”按钮。

答案 1 :(得分:2)

[我无法对答案发表评论,因此请将此评论作为新答案留待]

karora的回答(第一个)非常有效,除了在

protected void onScrollChanged(int left, int top, int oldLeft, int oldTop)

方法,调用

getContentHeight()

对我来说非常不准确。它报告的值太小了,所以当用户只滚动了WebView的三分之一时,我的监听器就会被调用。我用了

computeVerticalScrollRange()
相反,这是完美的。感谢this post提供了有用的提示。

答案 2 :(得分:0)

试试这个:

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        View view = (View) getChildAt(getChildCount()-1);
        int diff = (view.getBottom()-(getHeight()+getScrollY()));// Calculate the scrolldiff
        if( diff == 0 ){  // if diff is zero, then the bottom has been reached
            Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
            yourButton.setVisible(true);
        }
        super.onScrollChanged(l, t, oldl, oldt);
}

要实现此功能,请扩展ScrollView,然后覆盖onScrollChanged方法(从View继承)。

答案 3 :(得分:0)

仅在webview到达/滚动到底部时加载/可见按钮。

创建JavaScript类:

public class JavaScriptInterface {

  @android.webkit.JavascriptInterface
  public void didScrollToBottom() {
    Log.d(TAG, "Scroll to Bottom");
    myHandler.post(new Runnable() {
      @Override
      public void run() {
         btnAccept.setVisibility(View.VISIBLE);

          }
       });
      }
    }

在onCreate()中:

final JavaScriptInterface jsInterface = new JavaScriptInterface();
myWebView.addJavascriptInterface(jsInterface, "AndroidFunction");

答案 4 :(得分:-1)

上面的解决方案对我来说并不完全适用于类似的问题(滚动webView时隐藏按钮,滚动结束后显示)。我想让它在滚动时隐藏的原因是因为我想要隐藏的按钮是跳到webview的最底层,当它只对我有用时webview是静态的,但是当视图仍然存在时没有跳到底部滚动。 所以我做了以下事情:

添加了一个onScrollChanged回调来覆盖webView,就像附近建议的那样:

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

并在我的activity类中实现了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;
}