1

listviewサーバーからの画像とテキストを含むカスタムがあります。サーバーが 500 以上のデータ セットを返す場合、すべてのデータをレンダリングできますが、listview読み込みに時間がかかります。

代わりに、リストが下にスクロールされたときにデータをレンダリングする必要があります。このために、画像をロードするためのコードをいくつか返しましたが、それでもこのコードには満足できませんでした。 (ImageLoaderThread)getView()を呼び出し て、メソッドから画像をロードしています。manager.fetchBitMapThread(data.getImgUrl(),iv)非常に多くのスレッドが実行中に作成されるためです。データ custom をロードすることをお勧めしますlistview

私は見OnScrollListenerましたが、これをカスタムに実装する方法を理解していませんlistview

manager.fetchBitMapThread(data.getImgUrl(),iv);


public class Manager {

ImageView imageView;
final int stub_id=R.drawable.stub;
private final Map<String, Bitmap> bitMap;

public Manager() {
    bitMap = new HashMap<String, Bitmap>();
}

public void fetchBitMapThread(final String urlString, final ImageView imageView) {
    this.imageView = imageView;
    if (bitMap.containsKey(urlString)) {
        imageView.setImageBitmap(bitMap.get(urlString));
    }
    imageView.setImageResource(stub_id);
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            if(!message.equals(null))
            imageView.setImageBitmap((Bitmap) message.obj);
            else
            imageView.setImageResource(stub_id);
        }
    };

    Thread thread = new Thread() {
        @Override
        public void run() {
            // set imageView to a "pending" image
            Bitmap bitM = fetchBitmap(urlString);
            Message message = handler.obtainMessage(1, bitM);
            handler.sendMessage(message);
        }
    };
    thread.start();
}


public Bitmap fetchBitmap(String urlString) {
    if (bitMap.containsKey(urlString)) {
        return bitMap.get(urlString);
    }

    Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
    try {
        InputStream is = fetch(urlString);


        Bitmap drawable = BitmapFactory.decodeStream(is);


        if (drawable != null) {
            bitMap.put(urlString, drawable);
            //Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", " + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", " + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
        } else {
            //wrong.setImageResource(stub_id);
            Log.w(this.getClass().getSimpleName(), "could not get thumbnail");

        }

        return drawable;
    } catch (MalformedURLException e) {
        Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
        //imageView.setImageResource(stub_id);
        return null;
    } catch (IOException e) {

        Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
    //  imageView.setImageResource(stub_id);
        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();
}

}

4

3 に答える 3

2

このリンクに従って、カスタム リスト ビューでサーバーから画像をロードします。 http://androidexample.com/Download_Images_From_Web_And_Lazy_Load_In_ListView_-_Android_Example/index.php?view=article_discription&aid=112&aaid=134

これをダウンロードして、アプリケーションに実装してみてください。

于 2014-02-15T07:21:21.897 に答える
1

このリンクに従ってください

http://android-er.blogspot.in/2010/07/load-listview-in-background-asynctask.html

Async Task を使用してサーバーから画像を読み込み、それらを ListView に表示する完全な例

于 2012-05-02T05:53:50.347 に答える