安卓:制作一个按钮可见一次的WebView做滚动(Android: Making a button

2019-06-25 09:00发布

我有一个网页视图,显示一个HTML文件。 当用户滚动到该文件中的WebView的底部,我想这是以前隐藏的展现出来一个按钮,用户可以再按做一些活动

我没有在iOS上,在那里我只是设置委托到ViewController,只是设置按钮为可见类似的东西。 我该怎么做Android上的类似的东西? 我注意到没有像iOS中的回调方法。

编辑:现在,我有2个对象的活动:包括我的文字网页视图,和一个按钮,这是当前不可见的。 我想我的活动接收消息时的WebView文本滚动至底部,使按钮可见

Answer 1:

我不得不这样做我自己,为了显示“我同意”按钮,一旦用户已滚动到EULA的底部。 律师,是吧?

事实上,当你覆盖的WebView(而非滚动型,如从@JackTurky答案),你可以调用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);
    }

}

我使用它来显示“我同意”按钮一旦用户已经滚动到web视图,其中I这样称呼它(在这种“实现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个它拿到像素)“我同意”按钮会出现。



Answer 2:

[我不能回答这里作为一个新的答案发表评论,所以离开我的评论]

karora的回答(第一批)工作得非常好,只是在

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

方法,调用

getContentHeight()

对我来说是极不准确。 据报道值过于小,因此当用户原本只滚动也许的话了的WebView第三我监听器被调用。 我用了

computeVerticalScrollRange()

相反,那就是完美的。 由于这个职位对于有用的提示。



Answer 3:

试试这个:

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

为了实现这一点,延伸滚动型,然后重写onScrollChanged方法(从视图继承)。



Answer 4:

加载/可见按钮仅在网页流量达到/滚动至底部。

创建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");


Answer 5:

上述解决方案没有完全为我工作了类似的问题(隐藏按钮,web视图被滚动,表演结束后滚动结束)。 究其原因,我想它隐藏在滚动是因为按钮,我想隐藏是跳转到网页视图的最底部,而当它只是为我工作时的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;
}


文章来源: Android: Making a button visible once webview is done scrolling