2013-12-03 3 views
3

У меня есть список изображений (с описаниями), которые загружаются с URL-адреса. Все в порядке до тех пор, пока я не перейду в список вверх. Картинки берутся снова, потому что (я думаю) метод вызывается снова getView(). Как сделать изображение загружено только один раз? Ниже класс ArrayAdapterЗагрузите изображение только один раз в getView() метод ArrayAdapter

public class ImageWithTwoTextArrayAdapter extends 
ArrayAdapter<ImageWithTwoText> { 
    public final static String TITLE_KEY = "title"; 
    public final static String SUBTITLE_KEY = "subtitle"; 
    public final static String IMAGE_RESOURCE_KEY = "imageResources"; 

    private final List<ImageWithTwoText> imageWithTwoTextList; 
    private final Context context; 

    TextView titleView; 
    TextView subTitleView; 
    ImageView imageView; 

    /** 
    * Class constructor 
    * 
    * @param context 
    */ 
    public ImageWithTwoTextArrayAdapter(Context context, 
     List<ImageWithTwoText> imageWithTwoTextList) { 
    super(context, R.layout.row_view, imageWithTwoTextList); 
    this.context = context; 
    this.imageWithTwoTextList = imageWithTwoTextList; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View rowView = inflater.inflate(R.layout.row_view, parent, false); 

    ImageWithTwoText currentElement = imageWithTwoTextList.get(position); 

    titleView = (TextView) rowView.findViewById(R.id.title); 
    subTitleView = (TextView) rowView.findViewById(R.id.subtitle); 
    imageView = (ImageView) rowView.findViewById(R.id.icon); 

    titleView.setText(currentElement.getTitle()); 
    subTitleView.setText(currentElement.getSubTitle()); 
    if (currentElement.getImageResource() != null) { 
     imageView.setImageResource(currentElement.getImageResource()); 
    } else { 
     if (imageView.getDrawable() == null) { 
     new DownloadThumbnailTask(imageView).execute(currentElement 
      .getImageURI()); 
     } 
    } 

    return rowView; 
    } 
} 
+2

Проверьте эту библиотеку: http://square.github.io/picasso/ – midhunhk

+0

Вы можете кэшировать изображения при загрузке с использованием вышеуказанной библиотеки. – midhunhk

+1

Проверьте это https://github.com/nostra13/Android-Universal-Image-Loader – SathishKumar

ответ

0

Использование this учебник для кэширования изображения, называемого отложенной загрузкой, я использую его, и он работает как шарм и весь процесс два этапа.

Смежные вопросы