簡體   English   中英

如何將圖像從服務器動態添加到線性布局

[英]How to dynamically add images from server to a linear layout

嗨,我正在嘗試從服務器加載圖像並將其動態添加到滾動視圖中的linearlayout中

protected void onPostExecute(ArrayList<RssItem> result) {
    Log.i("Async-Example", "onPostExecute Called");
    horview = (HorizontalScrollView) aview.findViewById(R.id.homesection);
    LinearLayout ll = (LinearLayout) aview.findViewById(R.id.sectionid);
    for(int i = 0; i < rssItems.size(); i++){
        try{
        image = new ImageView(getActivity());
        image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //text.setText(data.get(position).getTitle());
        aview.setTag(rssItems.get(i).getLink());
        image.setFocusable(true);
        imageLoader.DisplayImage(rssItems.get(i).getLink(), image);
        ll.addView(image);  

    }catch(Exception e) {

    }
}

我面臨的問題是:如果我在xml的LinearLayout中聲明ImageView,則僅添加來自服務器的最后一個圖像。 但是,如果我沒有在線性布局中聲明ImageView而是實例化並添加到其中,則服務器中的所有圖像都不會替換我的實際默認圖標

這是在gridview,listview和pager中顯示圖像的絕佳示例。 看一下這個

https://github.com/nostra13/Android-Universal-Image-Loader

根據您的需求進行更改

您不應在LinearLayout內添加帶有XML的ImageView,因為這只會導致單個視圖膨脹。 在旅途中啟動ImageViews應該可以工作,但是對我來說,這聽起來像是您嘗試加載從網站獲取的圖像列表,在這種情況下,通常將ListView與自定義適配器一起使用。 在線上有很多教程: http : //www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

我認為您代碼中的問題與imageLoader有關。 如果您遇到問題,可以隨時嘗試使用http://square.github.io/picasso/之類的其他方法並致電

Picasso.with(getActivity()).load(rssItems.get(i).getLink()).into(image);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM