/*
* Copyright (c) 2012 Daniel Huckaby
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.handlerexploit.prime.example.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.handlerexploit.prime.example.R;
import com.handlerexploit.prime.example.utils.Utilities.Image;
import com.handlerexploit.prime.widgets.RemoteImageView;
/**
* This class uses the basic ViewHolder optimization pattern and is displaying
* the expected implementation of the {@link RemoteImageView}. If you follow the
* correct pattern the user experience should be clean and painless.
*/
public class LazyImageAdapter extends ArrayAdapter<Image> {
private int resId;
public LazyImageAdapter(Context context, int resId) {
super(context, 0);
this.resId = resId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(resId, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (RemoteImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Image image = getItem(position);
holder.text.setText(image.title);
/*
* Set the default image, this is required. If you do not set a default
* image the recycled views will display the incorrect images if/when
* they are finally loaded. If the image is available in memory this
* default may not ever be displayed to the user.
*/
holder.icon.setImageBitmap(null);
/*
* Make sure to always set something to this attribute if you are using
* it in an adapter. This is the expected adapter behavior and our
* recycling optimizations will not work correctly if we do not set this
* correctly.
*/
holder.icon.setImageURL(image.imageURL);
return convertView;
}
private static class ViewHolder {
TextView text;
RemoteImageView icon;
}
}