【问题标题】:TableRow Overlaps other TableRows when Images are Loaded - Android加载图像时 TableRow 与其他 TableRows 重叠 - Android
【发布时间】:2015-01-26 11:45:35
【问题描述】:

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

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

我在我的应用程序中使用 TableLayout。我的代码是这样工作的:

  • 如果内容中没有表格,只需将其全部放在一行中并将这一行添加到表格布局中
  • 如果有一个表格,则获取表格之前的所有内容,将其放在一行中,并将该行添加到表格布局中。现在对于表格中的每一行,获取它的内容并将其添加为表格布局中的一行。然后将表格后面的所有内容添加到一行中,并添加到表格布局中。
  • (对于多个表以此类推)

这段代码运行良好,我遇到的问题是表格之前的内容是否包含图像。在这种情况下,初始内容在图像完成加载之前作为一行放置在 TableLayout 中。当图像完成加载后,此 tablerow 将调整大小以包含图像。但是,tablelayout 中的下一行不会相应地向下移动。这意味着下面的行与第一个表行重叠。

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

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

如果上面的行改变了它的大小,有谁知道是否有可能/如何让表行重新调整自己? 或者,任何人都可以看到我在尝试检测加载完成时出错的地方。我只是使用一个布尔标志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;
}

}

【问题讨论】:

    标签: android image tablelayout tablerow


    【解决方案1】:

    现在想通了。这不是 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());
    

    现在可以正确检测图像的高度并正确显示所有内容。

    【讨论】:

      猜你喜欢
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2021-01-28
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多