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();
}
}