开发学院手机开发Android 开发 Android实现ListView异步加载图片 阅读

Android实现ListView异步加载图片

 2010-06-19 18:23:00 来源:WEB开发网   
核心提示:import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.ListView;import

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;

public class ImageAndTextListAdapter extends ArrayAdapter {

private ListView listView;

private AsyncImageLoader asyncImageLoader;

public ImageAndTextListAdapter(Activity activity, List imageAndTexts, ListView listView) {

super(activity, 0, imageAndTexts);

this.listView = listView;

asyncImageLoader = new AsyncImageLoader();

}

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

Activity activity = (Activity) getContext();

// Inflate the views from XML

View rowView = convertView;

ViewCache viewCache;

if (rowView == null) {

LayoutInflater inflater = activity.getLayoutInflater();

rowView = inflater.inflate(R.layout.image_and_text_row, null);

viewCache = new ViewCache(rowView);

rowView.setTag(viewCache);

} else {

viewCache = (ViewCache) rowView.getTag();

}

ImageAndText imageAndText = getItem(position);

// Load the image and set it on the ImageView

String imageUrl = imageAndText.getImageUrl();

ImageView imageView = viewCache.getImageView();

imageView.setTag(imageUrl);

Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {

public void imageLoaded(Drawable imageDrawable, String imageUrl) {

ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);

if (imageViewByTag != null) {

imageViewByTag.setImageDrawable(imageDrawable);

上一页  1 2 3 4 5 6 7  下一页

Tags:Android 实现 ListView

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接