动态加载图片的Adapter


package com.example.cc.ecustapp.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.LayoutInflater;


import com.example.cc.ecustapp.R;
import com.nostra13.universalimageloader.core.ImageLoader;


import java.util.ArrayList;


public class ImageAdapter1 extends BaseAdapter implements OnScrollListener{
/**
* 上下文对象的引用
*/
private Context context;

/**
* Image Url的数组
*/
private String [] imageThumbUrls;

/**
* GridView对象的应用
*/
private NoScrollGridView mGridView;

/**
* 记录是否刚打开程序,用于解决进入程序不滚动屏幕,不会下载图片的问题。
* 参考http://blog.csdn.net/guolin_blog/article/details/9526203#comments
*/
private boolean isFirstEnter = true;

/**
* 一屏中第一个item的位置
*/
private int mFirstVisibleItem;

/**
* 一屏中所有item的个数
*/
private int mVisibleItemCount;

private LayoutInflater mLayoutInflater = null;

//private ArrayList<ProductEntity> mList=new ArrayList<>();

private DisplayMetrics displaysMetrics;

public void setUrls(String[] imageThumbUrls){this.imageThumbUrls=imageThumbUrls;}

//public void setlist(ArrayList<ProductEntity> list){this.mList=list;}

public ImageAdapter1(Context context, NoScrollGridView mGridView, String[] imageThumbUrls,DisplayMetrics displayMetrics){
this.context = context;
this.mGridView = mGridView;
this.imageThumbUrls = imageThumbUrls;
// this.mList=list;
isFirstEnter=true;
mLayoutInflater=LayoutInflater.from(context);
this.displaysMetrics = displayMetrics;//获取屏幕分辨率
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//仅当GridView静止时才去下载图片,GridView滑动时取消所有正在下载的任务
if(scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
showImage(mFirstVisibleItem, mVisibleItemCount);
}
}


/**
* GridView滚动的时候调用的方法,刚开始显示GridView也会调用此方法
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
mFirstVisibleItem = firstVisibleItem;
mVisibleItemCount = visibleItemCount;
// 因此在这里为首次进入程序开启下载任务。
if(isFirstEnter && visibleItemCount > 0){
showImage(mFirstVisibleItem,mVisibleItemCount);
isFirstEnter = false;
}
}


@Override
public int getCount() {
return imageThumbUrls.length;
}

@Override
public Object getItem(int position) {
return imageThumbUrls[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, final View convertView, ViewGroup parent) {
ImageView mImageView;
TextView tvView;
final String mImageUrl = imageThumbUrls[position];
// final ProductEntity pro;
// if(!mList.isEmpty()&&mList.get(position)!=null)
// pro=mList.get(position);
// else
// pro=null;
View view;
if (convertView == null) {
view = mLayoutInflater.inflate(R.layout.gridview_item,parent,false);
} else {
view = convertView;
}
mImageView=(ImageView)view.findViewById(R.id.ItemImage);
//tvView=(TextView)view.findViewById(R.id.tvTitle);

//tvView.setText(pro.getProName());
// mImageView.setLayoutParams(new RelativeLayout.LayoutParams(displaysMetrics.widthPixels/3,displaysMetrics.heightPixels/5));;
//给ImageView设置Tag,这里已经是司空见惯了
mImageView.setTag(mImageUrl);
ImageLoader.getInstance().displayImage(mImageUrl, mImageView);
// mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
// mImageView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent intent=new Intent();
// intent.putExtra("ProductName",pro.getProName());
// intent.setClass(context, ProductdetailActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(intent);
// }
// });

return view;
}

/**
* 显示当前屏幕的图片,先会去查找LruCache,LruCache没有就去sd卡或者手机目录查找,在没有就开启线程去下载
* @param firstVisibleItem
* @param visibleItemCount
*/
public void showImage(int firstVisibleItem, int visibleItemCount){
for(int i=firstVisibleItem; i<firstVisibleItem + visibleItemCount; i++) {
final String mImageUrl = imageThumbUrls[i];
final ImageView mImageView = (ImageView) mGridView.findViewWithTag(mImageUrl);
ImageLoader.getInstance().displayImage(mImageUrl, mImageView);
}
}

}
posted @ 2016-03-06 19:44  Wei_java  阅读(687)  评论(0编辑  收藏  举报