检测WebView是否已滚动到底部的新方法 - 不推荐使用setScale

时间:2015-03-24 16:22:03

标签: android webview

在对stackoverflow进行大量研究并寻找答案后,我发现我需要创建WebView的子类,然后对OnScrollChanged等进行覆盖。我有以下代码......

SearchResultsWebView.setOnScrollChangedCallback(
    new Sub_WebView_Results.OnScrollChangedCallback() {
            @Override
            public void onScroll(int l, int t) {
                int tek = (int) Math.floor(SearchResultsWebView.getContentHeight() * SearchResultsWebView.getScale());
                if (tek - SearchResultsWebView.getScrollY() == SearchResultsWebView.getHeight())
                    Toast.makeText(getActivity(), "End", Toast.LENGTH_SHORT).show();


            }
        });

但问题是.getScale已被折旧。我还没有找到另一种有效的方式。

我尝试过使用..

          SearchResultsWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
            currentScale = newScale;
        }
    });

然后只是传递[currentScale]但似乎永远不会被调用,所以我对如何做到这一点感到茫然。

4 个答案:

答案 0 :(得分:1)

网页视图的内容高度返回dp,因此我们需要乘以设备密度乘数来获取内容的实际高度 使用实际高度我扣除webview的高度来计算webview何时可见,并在每次用户拿起他们的手指时与滚动y进行比较。 这就是我所做的,对我来说很完美。

webView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            if (Math.floor((webView.getContentHeight() * getResources().getDisplayMetrics().density) - webView.getHeight()) == webView.getScrollY()) {

                // Bottom Reached , it is necessary to calculate content height because 
                // it changes showAgreeButton();

                return true;
            }
            return false;
        }
    });
}

答案 1 :(得分:1)

为了判断用户是否已滚动到Web视图的底部,我扩展了Web视图,并在用户到达视图底部的SBrollChanged时进行了界面回调。这是代码:

import android.content.Context;

import android.util.AttributeSet; import android.util.Log; import android.webkit.WebView;

公共类EULAWebView扩展了WebView {

//declare needed constants
private final String TAG = getClass().getSimpleName();

//declare needed variables
private EULAWebInterface eulaWebInteface;
private int paddingOffset = 200;
private boolean bottomReached;

public EULAWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

public EULAWebView(Context context) {
    super(context);
}

public void setEULAScrollListener(Context context) {
    try {
        eulaWebInteface = (EULAWebInterface)context;
    } catch (ClassCastException ex) {
        Log.e(TAG, "UNABLE TO CAST CONTEXT TO EULAWebInterface");
        ex.printStackTrace();
        throw new ClassCastException();
    }
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    if(this.computeVerticalScrollRange() <= (this.computeVerticalScrollOffset() +
            this.computeVerticalScrollExtent() + this.paddingOffset)) {
        if(!bottomReached) {
            bottomReached = true;
            if(eulaWebInteface != null)
                eulaWebInteface.atBottomOfScrollView(true);
        }
    } else {
        if(bottomReached) {
            bottomReached = false;
            if(eulaWebInteface != null)
                eulaWebInteface.atBottomOfScrollView(false);
        }
    }
    super.onScrollChanged(l, t, oldl, oldt);
}

}

以下是用于让活动知道滚动视图底部已更改的接口:

public interface EULAWebInterface {

void atBottomOfScrollView(boolean atBottom);

}

这是活动中的接口实现:

@Override
public void atBottomOfScrollView(boolean atBottom) {
    findViewById(R.id.eula_action_layout).setVisibility(atBottom ? View.VISIBLE : View.GONE);
    findViewById(R.id.eula_instruction_textview).setVisibility(atBottom ? View.GONE : View.VISIBLE);
} 

答案 2 :(得分:0)

显然我找到了答案:How can i get the current scale of a webView(android)

而不是WebView.getScale()

您可以使用:getResources()。getDisplayMetrics()。density

答案 3 :(得分:0)

试试这个:

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        View view = (View) getChildAt(getChildCount()-1);
        int diff = (view.getBottom()-(getHeight()+getScrollY()));// Calculate the difference in scrolling
        if( diff == 0 ){  // The bottom has been reached if the difference is 0
            Log.d(ScrollTest.LOG_TAG, "WebView: Bottom has been reached" );
        // DO SOMETHING HERE WHEN THE WEBVIEW HAS REACHED THE BOTTOM!
    }
    super.onScrollChanged(x, y, oldx, oldy);
}

顺便说一句,为什么在上述方法可能更好的时候使用缩放方法(我觉得它更容易实现)