加载图像时,TableRow与其他TableRow重叠-Android

加载图像时,TableRow与其他TableRow重叠-Android,android,image,tablelayout,tablerow,Android,Image,Tablelayout,Tablerow,我正在android应用程序上将大量书面内容转换为易于阅读的页面。该内容主要包括文本和图像,但也包括表格 内容是以html的形式下载的,因此我使用html.fromHtml()方法和URLImageParser类来显示内容 我正在我的应用程序中使用表格布局。我的代码是这样工作的: 如果内容中没有表格,只需将其全部放在一行中,并将该行添加到tablelayout中 如果有一个表,则获取该表之前的所有内容,将其放入一行,并将该行添加到tablelayout。现在,对于表中的每一行,获取其内容并将其

我正在android应用程序上将大量书面内容转换为易于阅读的页面。该内容主要包括文本和图像,但也包括表格

内容是以html的形式下载的,因此我使用html.fromHtml()方法和URLImageParser类来显示内容

我正在我的应用程序中使用表格布局。我的代码是这样工作的:

  • 如果内容中没有表格,只需将其全部放在一行中,并将该行添加到tablelayout中
  • 如果有一个表,则获取该表之前的所有内容,将其放入一行,并将该行添加到tablelayout。现在,对于表中的每一行,获取其内容并将其作为一行添加到tablelayout中。然后将表格后面的所有内容添加到一行中,并将其添加到tablelayout中
  • (对于多个表,依此类推)
这段代码运行得很好,我遇到的问题是表前的内容是否包含图像。在这种情况下,在图像完成加载之前,初始内容将作为一行放置在TableLayout中。当图像完成加载后,将调整此tablerow的大小以包括图像。但是,tablelayout中的下一行不会相应地下移。这意味着下面的行与第一行重叠

理想情况下,我希望下面的行在上面的行改变大小时自行调整,这可能吗?我一直在网上寻找,但找不到类似的东西

我一直在尝试一种不同的方法-我想在我将行添加到tablelayout之前检测URLImageParser何时完成了图像加载(我接受这将意味着在打开页面时应用程序将暂停一段时间),但我也无法让它工作。由于某些原因,从未调用URLImageParser AsyncTask的onPostExecute()方法

有人知道如果上面的行改变了大小,是否有可能/如何让tablerows重新调整自己吗? 或者,任何人都可以看到我在尝试检测加载完成时出错的地方。我只是使用布尔标志finishedExecuting,但它从未设置为true。我已经包括了下面的课程

谢谢您的时间,任何回复都将不胜感激

URLImageParser.java

public class URLImageParser implements ImageGetter {
Context c;
TextView container;
Activity a;
int intrinsicHeight;
int intrinsicWidth;
boolean finishedExecuting;

public URLImageParser(TextView t, Context c, Activity a) {
    this.c = c;
    this.container = t;
    this.a = a;
    intrinsicHeight =0;
    intrinsicWidth = 0;
    finishedExecuting = false;
}

public Drawable getDrawable(String source) {
    System.out.println("getDrawable() was called");
    URLDrawable urlDrawable = new URLDrawable();

    ImageGetterAsyncTask asyncTask = 
        new ImageGetterAsyncTask( urlDrawable);

    asyncTask.execute(source);

    return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  {
    URLDrawable urlDrawable;
    boolean usesImageNotFoundDrawable = false;

    public ImageGetterAsyncTask(URLDrawable d) {
        this.urlDrawable = d;
    }

    @Override
    protected Drawable doInBackground(String... params) {
        String source = params[0];
        return fetchDrawable(source);
    }

    @Override
    protected void onPostExecute(Drawable result) {
        System.out.println("ENTERED ON POST EXECUTE");
        //check to see if an image was found (if not could
        //be due to no internet)
        if(result ==null){
            usesImageNotFoundDrawable = true;
            //the drawable wasn't found so use the image not found
            //png
            Drawable imageNotFound = a.getResources().getDrawable(R.drawable.image_not_found);
            result = imageNotFound;
        } else {
            usesImageNotFoundDrawable = false;
        }

        intrinsicHeight = result.getIntrinsicHeight();
        intrinsicWidth = result.getIntrinsicWidth();

        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels -50;
        int height = width * intrinsicHeight / intrinsicWidth;

        result.setBounds(0, 0, 0 + width, 0 
                + height);

        urlDrawable.setBounds(0, 0, 0+width, 0+height);  

        urlDrawable.drawable = result; 

        URLImageParser.this.container.invalidate();


        if(usesImageNotFoundDrawable == true){
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                    + height*4));
        } else {
            URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                    + height));
        }

        // Pre ICS
        URLImageParser.this.container.setEllipsize(null);

        setFinishedExecuting(true);

       System.out.println("END OF ON POST EXECUTE");

    }

    public Drawable fetchDrawable(String urlString) {
        try {
            InputStream is = fetch(urlString);
            Drawable drawable = Drawable.createFromStream(is, "src");             
            return drawable;
        } catch (Exception e) {
            return null;
        } 
    }

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);
        return response.getEntity().getContent();
    }
}

public boolean getFinishedExecuting(){
    return finishedExecuting;
}

public void setFinishedExecuting(boolean bool){
    finishedExecuting = bool;
}
公共类URLImageParser实现ImageGetter{
上下文c;
文本视图容器;
活动a;
内质内质;
内部宽度;
布尔精加工;
公共URLImageParser(TextView t、上下文c、活动a){
这个.c=c;
这个容器=t;
这个a=a;
内在八度=0;
内部宽度=0;
finishedExecuting=假;
}
公共可绘制getDrawable(字符串源){
println(“调用了getDrawable());
URLDAWABLE URLDAWABLE=新的URLDAWABLE();
ImageGetterAsyncTask异步任务=
新的ImageGetterAsyncTask(URLDawable);
asyncTask.execute(源代码);
返回可提取;
}
公共类ImageGetterAsyncTask扩展异步任务{
URLDrawable URLDrawable;
布尔值usesImageNotFoundDrawable=false;
公共ImageGetterAsyncTask(URLDawable d){
this.urlDrawable=d;
}
@凌驾
受保护的可抽出式doInBackground(字符串…参数){
字符串源=参数[0];
返回可提取的(源);
}
@凌驾
受保护的void onPostExecute(可提取结果){
System.out.println(“在执行后输入”);
//检查是否找到图像(如果未找到,则无法找到)
//(由于没有互联网)
如果(结果==null){
usesImageNotFoundDrawable=true;
//找不到绘图表,因此请使用未找到的图像
//巴布亚新几内亚
Drawable imageNotFound=a.getResources().getDrawable(R.Drawable.image\u未找到);
结果=imageNotFound;
}否则{
usesImageNotFoundDrawable=false;
}
intrinsicHeight=result.getIntrinsicHeight();
intrinsicWidth=result.getIntrinsicWidth();
DisplayMetrics dm=新的DisplayMetrics();
((WindowManager)c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels-50;
内部高度=宽度*内部高度/内部宽度;
结果.设置边界(0,0,0+宽度,0
+高度);
urlDrawable.setBounds(0,0,0+宽度,0+高度);
urlDrawable.drawable=结果;
URLImageParser.this.container.invalidate();
如果(usesImageNotFoundDrawable==true){
urlmageparser.this.container.setHeight((urlmageparser.this.container.getHeight)()
+高度*4);
}否则{
urlmageparser.this.container.setHeight((urlmageparser.this.container.getHeight)()
+身高),;
}
//预集成电路
URLImageParser.this.container.setEllipsize(null);
setFinishedExecuting(真);
System.out.println(“后期执行结束”);
}
公共可提取可提取(字符串urlString){
试一试{
InputStream is=fetch(urlString);
Drawable Drawable=Drawable.createFromStream(是“src”);
回拉;
}捕获(例外e){
返回null;
} 
}
私有InputStream获取(字符串urlString)引发错误的InputStream异常,IOException{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet请求=新的HttpGet(urlString);
HttpResponse response=httpClient.execute(请求);
返回response.getEntity().getContent();
}
}
公共布尔getFinishedExecuting(){
返回完成数据采集;
}
public void setFinishedExecuting(布尔布尔布尔值){
完成Dexecuting=bool;
}

}

现在就知道了。这不是TableRow的问题。这是我的URLImageParser类的一个问题

我搬走了

if(usesImageNotFoundDrawable == true){
                  URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                + height*4));
    } else {
        URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() 
                + height));
    }
并将其替换为

container.setText(container.getText());
现在高度