开发者

Android: Get the scroll position of a WebView

I'm new to Android.

I would like to simply know where on a page the user has scrolled. When a certain point on the web page appears at the bottom of the screen, I want to trigger an event. But this code causes an exception. I know that WebView inherits getScrollY() from View. Am I not implementing it correctly?

Thanks in advance.

public class Scroll extends Activity {

    public WebView webview;
    public float yPos;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)开发者_如何转开发;

        WebView webview = new WebView(this);
        setContentView(webview);
        webview.loadUrl("file:///android_asset/Scroll.html");
    }


    public boolean onTouchEvent(final MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            yPos = webview.getScrollY();
            Log.v("Scroll", "yPos = " + yPos); 
        }
        return false;
    }
}


And the exception is?
Here is a note on WebView screen heights from one of my apps:

// current position (top) = getScrollY();  
// max position = getContentHeight();  
// screen height = getHeight();


The WebView has a method to get its content height, which I used in my solution:

webView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
    @Override
    public void onScrollChange(View view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        WebView webView = (WebView) view;
        float contentHeight = webView.getContentHeight() * webView.getScaleY();
        float total = contentHeight * getResources().getDisplayMetrics().density - view.getHeight();
        // on some devices just 1dp was missing to the bottom when scroll stopped, so we subtract it to reach 1
        float percent = Math.min(scrollY / (total - getResources().getDisplayMetrics().density), 1);
        Log.d("SCROLL", "Percentage: " + percent);
        if (scrollY >= total - 1) { 
                        Log.d("SCROLL", "Reached bottom");
        }
    }
}

Look out for:

  • WebView scale has to be taken into account to get the total scroll height
  • Display density has to be multiplied into content height to compare it with scrollY
  • On my Nexus 5X on Android 8 the scrollY never reached the height, but stopped 1dp before the end (workaround is included in code)


You are writing WebView webview; at two places inside the OnCreate() method.

Simply write webview = new WebView(this); instead of WebView webview = new WebView(this);

This way you can solved the null pointer issue.


This worked for me, I am calculating the basic scrolling position.

mainWebView.setOnScrollChangeListener { view, scrollX, scrollY, oldScrollX, oldScrollY ->

        if (scrollY > 800) {
            supportActionBar!!.setDisplayHomeAsUpEnabled(true)
            toolbar.logo = resources.getDrawable(R.mipmap.ic_launcher)
        } else {
            supportActionBar!!.setDisplayHomeAsUpEnabled(false)
            toolbar.logo = null
        }
        Log.e(TAG, "initializeWeb: $scrollY")
    }


For those who are using Ionic framework.

Getting the scrollTop from the Webview < ion-content id="_ionic_content" > does not work, because ion_content wraps it's own scroller.

To get the scrollTop of the webView use :

javascript ->

 var scroller = $('#_ionic_content')[0].firstChild.offsetParent;
 var  scrollTop_pos = scroller.scrollTop;

This has worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜