获取WebView内容高度,一旦其加载Android [英] Getting WebView Content height once its loaded Android

查看:1508
本文介绍了获取WebView内容高度,一旦其加载Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Webview上应用展开折叠功能基于其内容高度,但我总是得到错误的值
这是我的代码



<$ p $ b $ public $ {
super(context); $ public $ {
super(context); $ public $ {
super(context); $ p $ {code> public class MesuredHeightWebView extends webView {
public MesuredHeightWebView

$ b $ public MesuredHeightWebView(Context context,AttributeSet attrs){
super(context,attrs);

$ b $ public MesuredHeightWebView(Context context,AttributeSet attrs,int defStyleAttr){
super(context,attrs,defStyleAttr);




@Override
public void invalidate(){
super.invalidate();

if(getContentHeight()> 0){
// WebView显示了一些内容并且可滚动。
if(listener!= null)
listener.updateContentHeight(getContentHeight());
}
}



WebViewContentHeight侦听器;

public void setChangeContentListener(WebViewContentHeight listener){
this.listener = listener;


然后在片段中我尝试获取内容身高

  webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view ,int newProgress){

super.onProgressChanged(view,newProgress);
if(newProgress == 100)
{
new Handler()。postDelayed(new Runnable(){
@Override
public void run(){
webView.setChangeContentListener(new WebViewContentHeight(){
@Override
public void updateContentHeight(int height) {

if(height> 0&& getActivity()!= null){

System.out.println(height2 is+ height + + Utils.convertPixelsToDp(height,getAct ivity()));
final int text_height = height;

但是我的问题是我总是收到错误的结果
谢谢

解决方案

使用下列方法代替 getContentHeight()方法:

  computeHorizo​​ntalScrollRange(); - >宽度
computeVerticalScrollRange(); - >对于高度

这两个方法将返回整个可滚动的宽度/高度,而不是实际的widht / height屏幕上的网络视图


I'm trying to apply expand collapse feature on Webview Based on its Content height but i'm always getting wrong value Here is my code

public class MesuredHeightWebView extends WebView {
    public MesuredHeightWebView(Context context) {
        super(context);
    }

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

    public MesuredHeightWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    @Override
    public void invalidate() {
        super.invalidate();

        if (getContentHeight() > 0) {
            // WebView has displayed some content and is scrollable.
            if(listener!=null)
                listener.updateContentHeight(getContentHeight());
        }
    }



    WebViewContentHeight listener;

    public void setChangeContentListener(WebViewContentHeight listener) {
        this.listener = listener;
    }
}

and then in the fragment i tried to get the content height

 webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

            super.onProgressChanged(view, newProgress);
            if(newProgress==100)
            {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        webView.setChangeContentListener(new WebViewContentHeight() {
                            @Override
                            public void updateContentHeight(int height) {

                                if(height>0 && getActivity()!=null) {

                                    System.out.println("the height2 is" + height + " " + Utils.convertPixelsToDp(height, getActivity()));
                                    final int text_height = height;

but my problem is that i'm always getting wrong result Thanks

解决方案

Use the following methods instead of getContentHeight() method:

computeHorizontalScrollRange(); -> for width 
computeVerticalScrollRange(); -> for height

these two methods will return the entire scrollable width/height rather than the actual widht/height of the webview on screen

这篇关于获取WebView内容高度,一旦其加载Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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