Android:在 webview 完成滚动后使按钮可见 [英] Android: Making a button visible once webview is done scrolling

查看:20
本文介绍了Android:在 webview 完成滚动后使按钮可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 can then press to do some activity

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

I did something similar in iOS, where I just set the delegate to the ViewController and just set the button as visible. How do I do something similar on Android? I noticed there isn't a callback method like in iOS.

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

Right now, I have an activity with 2 objects: a webview containing my text, and a button which is currently invisible. I want my activity to receive a message when the webview text scrolls to the bottom, and make the button visible

推荐答案

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

I had to do this myself, in order to display an "I Agree" button once the user has scrolled to the bottom of a EULA. Lawyers, huh?

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

In fact when you override the WebView (rather than the ScrollView as in the answer from @JackTurky) you can call computeVerticalScrollRange() to get the height of the content, rather than getBottom() which returns the visible bottom and is not useful.

这是我的综合解决方案.据我所知,这都是 API 级别 1 的东西,所以它应该可以在任何地方使用.

This is my comprehensive solution. As far as I can see this is all API Level 1 stuff, so it should work anywhere.

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

}

一旦用户滚动到 WebView 的底部,我就用它来显示我同意"按钮,在那里我这样称呼它(在实现 OnBottomReachedListener"的类中:

I use this to display an "I Agree" button once the user has scrolled to the bottom of the WebView, where I call it like this (in a class which "implements 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 像素以内时)会出现我同意"按钮.

So when the bottom is reached (or in this case, when they get within 50 pixels of it) the "I Agree" button appears.

这篇关于Android:在 webview 完成滚动后使按钮可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆