簡體   English   中英

Android ListView緩慢加載圖像

[英]Android ListView loads images slowly

我在每個項目中都有一個帶有圖像的ListView 我從服務器下載圖像。 由於有兩個以上的圖像,因此加載速度真的很慢,向下滾動時,您可以從上方看到該圖像,並且必須等待很長時間才能更改為正確的圖像。 我已經嘗試使用這篇文章中的提示: http : //lucasr.org/2012/04/05/performance-tips-for-androids-listview/,但是它仍然很慢。 什么是改善此問題的最佳和最簡單的方法。 這是我的BaseAdapter類中的代碼:

public class GetAllEntrysListViewAdapter extends BaseAdapter {

private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;
public Cursor cursor;
private SQLiteDatabase dbase;
DbHelper dbh;
private Context context;
String pos;
Integer markerID;
public String objectID;



private static final String baseUrlForImage = "http://...";

public GetAllEntrysListViewAdapter(JSONArray jsonArray, Context context, Integer markerID) {
    this.dataArray = jsonArray;
    this.context= context;
    this.markerID = markerID;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);

        cell = new ListCell();

        cell.likes = (TextView) convertView.findViewById(R.id.listViewLikes);
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
        cell.likeImage = (ImageView) convertView.findViewById(R.id.heartImage);

        convertView.setTag(cell);

    }
    else {
        cell = (ListCell)convertView.getTag();
    }

    cell.position = position;

    try {
        JSONObject jsonObject = this.dataArray.getJSONObject(position);
        cell.likes.setText(jsonObject.getString("likes"));
        cell.note.setText(jsonObject.getString("note"));
        cell.entryID = jsonObject.getString("id");
        String img = jsonObject.getString("image");
        String urlForImageInServer = baseUrlForImage + img;

        new AsyncTask<String, Void, Bitmap>() {
            private int mPosition = position;
            private ListCell mCell = cell;
            @Override
            protected Bitmap doInBackground(String... params) {
                //download image
                String url = params[0];
                Bitmap icon = null;

                try {
                    InputStream in = new java.net.URL(url).openStream();
                    icon = BitmapFactory.decodeStream(in);
                } catch(MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return icon;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                if (cell.position == mPosition) {
                    cell.img.setImageBitmap(result);
                }
            }

        }.execute(urlForImageInServer);


    catch (JSONException e) {
        e.printStackTrace();
    }
    return convertView;

}

public static class ListCell {
    private TextView likes;
    private TextView note;
    private ImageView img;
    public ImageView likeImage;
    public int position;
    public String entryID;
}

}

不要重新發明輪子。 不要為ListView每個項目創建AsyncTask 使用一些庫,該庫將下載,緩存並幫助顯示諸如Picasso之類的圖像:

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);

另外,將JSONObject拆分為ListView每個項目的參數也是多余的。 創建POJO對象數組,而不僅僅是JSONObject數組。 或者,如果ListView僅顯示圖像,則可以僅將String數組傳遞給適配器。

PS並嘗試查找和閱讀新鮮文章。 您提到了2012年的文章。

到目前為止,最受歡迎的SO帖子是@ListView中的圖像的延遲加載 它提到了畢加索以及其他軟件包。 玩得開心...

您可以使用Volley Library,它會讓您放松並做所有事情

http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

     <!-- Thumbnail Image -->
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/thumbnail"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="8dp" />

    NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);

   // thumbnail image
        thumbNail.setImageUrl(Your url, imageLoader);

暫無
暫無

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

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