我正在将大量的书面内容转换为Android应用程序上易于阅读的页面。此内容主要由文本和图像组成,但也包括表格。
内容以html格式下载,因此我使用Html.fromHtml()方法和URLImageParser类来显示内容。
我在我的应用中使用TableLayout。我的代码就像这样:
此代码工作正常,我遇到的问题是表格前的内容是否包含图片。在这种情况下,初始内容在图像加载完成之前作为一行放在TableLayout中。当图像加载完毕后,此表格的大小将调整为包含图像。但是,tablelayout中的下一行不会相应地向下移动。这意味着下面的行与第一个tablerow重叠。
理想情况下,当上面的行改变大小时,我希望下面的行自行调整,这可能吗?我一直在尝试在网上找不到类似的东西。
我一直在尝试一种不同的方法 - 我想在我将行添加到tablelayout之前检测URLImageParser何时完成加载图像(我接受这意味着应用程序会在打开页面时暂停一段时间)但是我也无法让这个工作。出于某种原因,我的URLImageParser AsyncTask的onPostExecute()方法永远不会被调用。
如果上面的行改变了它的大小,有没有人知道是否可以/如何让桌子重新调整? 或者,任何人都可以看到我在尝试检测负载完成时出错的地方。我只是使用boolean flag 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;
}
}
答案 0 :(得分: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());
现在可以正确检测图像的高度,并正确显示所有内容。