and roid how to deal with data when listview refresh
23399 ワード
どのようにlistviewデータの更新を解決して、下に引いて更新して、上に引き延ばしてもっと多い時ロードして、ピクチャーはフラッシュしません。
ActivityのonResume()方法でadaperとlistViewを再結合します。
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=541113&extra=&ordertype=1
ActivityのonResume()方法でadaperとlistViewを再結合します。
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=541113&extra=&ordertype=1
public class HomeWaterfallXlListAdapter extends BaseAdapter {private Bitmap defaultBm;
private static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
public HomeWaterfallXlListAdapter() {
defaultBm = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.loading);
}
/** **/
public void setList(List<HomeAnswersJSONResult.Answer> list) {
this.answers = list;
notifyDataSetInvalidated();
}
/** **/
public void addList(List<HomeAnswersJSONResult.Answer> list) {
if (this.answers != null) {
this.answers.addAll(list);
notifyDataSetChanged();
} else {
setList(list);
}
}
@Override
public int getCount() {
return answers == null ? 0 : answers.size();
}
@Override
public Object getItem(int arg0) {
return answers.get(arg0);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Answer answer = answers.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_home_infos_list, null);
holder = new ViewHolder();
holder.imageView = (ScaleImageView) convertView.findViewById(R.id.starImg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
int w = answer.getImg().getWidth();
int h = answer.getImg().getHeight();
holder.imageView.setImageWidth(w);
holder.imageView.setImageHeight(h);
String imageUrl = answer.getImg().getUrl().toString() + "?w=" + AppActivity.screenWidth / 2;
holder.imageView.setTag(imageUrl);
imageLoader.loadImage(imageUrl, new ImageSize(w, h), options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.imageView.setImageBitmap(defaultBm);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null && imageUri.equals(holder.imageView.getTag())) {
holder.imageView.setImageBitmap(loadedImage);
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
//
AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
fadeImage.setDuration(500);
fadeImage.setInterpolator(new DecelerateInterpolator());
holder.imageView.startAnimation(fadeImage);
displayedImages.add(imageUri);
}
}
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
convertView.setOnClickListener(new onItemClick(id));
return convertView;
}
static final class ViewHolder {
ScaleImageView imageView;
}
private class onItemClick implements View.OnClickListener {
private int id;
public onItemClick(int id) {
this.id = id;
}
@Override
public void onClick(View arg0) {
Intent intent = new Intent(mActivity, A.class);
intent.putExtra("answerId", id);
mActivity.startActivity(intent);
}
}
/**
*
* @author Administrator
*
*/
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
//
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
//
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
}
ScreImageView/**
*
* This view will auto determine the width or height by determining if the
* height or width is set and scale the other dimension depending on the images
* dimension
*
* This view also contains an ImageChangeListener which calls changed(boolean
* isEmpty) once a change has been made to the ImageView
*
* @author Maurycy Wojtowicz
*
*/
public class ScaleImageView extends ImageView {
private Bitmap currentBitmap;
private ImageChangeListener imageChangeListener;
private boolean scaleToWidth = false; // this flag determines if should
// measure height manually dependent
// of width
public ScaleImageView(Context context) {
super(context);
init();
}
public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ScaleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
this.setScaleType(ScaleType.CENTER_INSIDE);
}
public void recycle() {
setImageBitmap(null);
if ((this.currentBitmap == null) || (this.currentBitmap.isRecycled()))
return;
this.currentBitmap.recycle();
this.currentBitmap = null;
}
@Override
public void setImageBitmap(Bitmap bm) {
currentBitmap = bm;
super.setImageBitmap(currentBitmap);
if (imageChangeListener != null)
imageChangeListener.changed((currentBitmap == null));
}
@Override
public void setImageDrawable(Drawable d) {
super.setImageDrawable(d);
if (imageChangeListener != null)
imageChangeListener.changed((d == null));
}
@Override
public void setImageResource(int id) {
super.setImageResource(id);
}
public interface ImageChangeListener {
// a callback for when a change has been made to this imageView
void changed(boolean isEmpty);
}
public ImageChangeListener getImageChangeListener() {
return imageChangeListener;
}
public void setImageChangeListener(ImageChangeListener imageChangeListener) {
this.imageChangeListener = imageChangeListener;
}
private int imageWidth;
private int imageHeight;
public void setImageWidth(int w) {
imageWidth = w;
}
public void setImageHeight(int h) {
imageHeight = h;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
/**
* if both width and height are set scale width first. modify in future
* if necessary
*/
if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) {
scaleToWidth = true;
} else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) {
scaleToWidth = false;
} else
throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension");
if (imageWidth == 0) {
// nothing to measure
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
} else {
if (scaleToWidth) {
int iw = imageWidth;
int ih = imageHeight;
int heightC = width * ih / iw;
if (height > 0)
if (heightC > height) {
// dont let hegiht be greater then set max
heightC = height;
width = heightC * iw / ih;
}
this.setScaleType(ScaleType.CENTER_CROP);
setMeasuredDimension(width, heightC);
} else {
// need to scale to height instead
int marg = 0;
if (getParent() != null) {
if (getParent().getParent() != null) {
marg += ((RelativeLayout) getParent().getParent()).getPaddingTop();
marg += ((RelativeLayout) getParent().getParent()).getPaddingBottom();
}
}
int iw = imageWidth;
int ih = imageHeight;
width = height * iw / ih;
height -= marg;
setMeasuredDimension(width, height);
}
}
}
}