简体   繁体   English

将一组图像加载到滚动视图中

[英]Load a set of images into a scrollview

Okay, so I have a JSON array of image URLs. 好的,所以我有一个图像URL的JSON数组。 I want to load them into a horizontal scrolling view. 我想将它们加载到水平滚动视图中。 This is the way I've gone about it, but nothing gets displayed where the ScrollView should go: 这是我处理过的方法,但是在ScrollView应该去的地方没有显示任何内容:

public void run() {
    JSONArray photosArray;
    caption = new WebView(thisContext);
    imageScroller = new HorizontalScrollView(thisContext);
    imagesHolder = new LinearLayout(thisContext);
    imagesHolder.setOrientation(LinearLayout.HORIZONTAL);
    try {
        photosArray = new JSONArray(postData.getString("photos"));
        for(int i = 0; i < photosArray.length(); i++) {
            WebView iV = new WebView(thisContext);
            JSONObject thisPhoto = photosArray.getJSONObject(i);
            JSONArray sizesArray = new JSONArray(thisPhoto.getString("alt_sizes"));
            JSONObject largest = sizesArray.getJSONObject(0);
            iV.loadData("<img src=\""+largest.getString("url")+"\" />", "text/html", null);
            imagesHolder.addView(iV);
        }
        imageScroller.addView(imagesHolder);
        myPostHolder.addView(imageScroller);
        caption.loadData(postData.getString("caption"),"text/html",null);
        myPostHolder.addView(caption);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Note that this is done in a runnable class. 请注意,这是在可运行类中完成的。 Thanks a lot 非常感谢

There are two rules in the Android single thread model. Android单线程模型中有两个规则。

  1. Do not block the UI thread 不要阻塞UI线程
  2. Do not access the Android UI toolkit from outside the UI thread 不要从UI线程外部访问Android UI工具包

So I don't think you can add views to a viewgroup outside of the main thread. 因此,我认为您无法将视图添加到主线程之外的视图组中。 Please see: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html for a fuller explanation. 请参阅: http : //developer.android.com/guide/topics/fundamentals/processes-and-threads.html以获得更完整的说明。

Also, you need to make sure you are actually setting the contentview and you may need to call invalidate() on the viewgroup to cause a redraw. 另外,你需要确保你实际上是在设置contentview ,你可能需要调用invalidate()viewgroup引起重绘。

And, you probably should be using a GridView ..can't see why you'd use a webview for this. 并且,您可能应该使用GridView ..无法了解为什么为此使用Webview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM