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

标签 android image tablelayout tablerow

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

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

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

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

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

理想情况下,当上面的行改变大小时,下面的行会自行调整,这可能吗?我一直在尝试在线查找,但找不到类似的内容。

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

有谁知道如果上面的行改变了它的大小是否可能/如何使表行重新调整自己? 或者,任何人都可以看到我尝试检测加载完成时出错的地方。我只是使用一个 bool 标志 finishedExecuting 但它从未设置为 true。我已经包含了下面的类(class)。

感谢您的宝贵时间,我们将不胜感激。

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;
}

最佳答案

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

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

关于android - 加载图像时 TableRow 与其他 TableRow 重叠 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28149485/

相关文章:

android - FragmentManager popBackStack 不删除 fragment

image - 使用高度 flexbox 调整图像大小

android - setShowDividers 的 NoSuchMethod 异常

css - tr :table using CSS 中的样式行分段和选择

java - 无法插入图像。没有权限。尽管提供许可

java - 如何使用 ExpandableListView 创建小部件?

java - 如何获取用户当前所在城市?

android - 如何使用 Android koush/ion 动态形成请求

php - 显示来自网络根目录外部的图像 ( public_html )

Matlab - 绘图窗口排列