Adapter
/**
* Created by jack on 2016/7/3.
*/
public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener {
private List<NewsBean> newsBeanList = new ArrayList<>();
private LayoutInflater mInflater;
private ImageLoader mImageLoader;
private int mStart, mEnd;
public static String[] URLS;
private boolean mFirstIn;
public NewsAdapter(Context context, List<NewsBean> data, ListView listView) {
newsBeanList = data;
mInflater = LayoutInflater.from(context);
mImageLoader = new ImageLoader(listView);
//将图片的url存储在数组中
URLS = new String[data.size()];
for (int i = 0; i < data.size(); i++) {
URLS[i] = data.get(i).newsIconUrl;
}
listView.setOnScrollListener(this);
mFirstIn = true;
}
@Override
public int getCount() {
return newsBeanList.size();
}
@Override
public Object getItem(int position) {
return newsBeanList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
//判断是否有缓存
if (convertView == null) {
//通过LayoutInflate实例化布局
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_layout, parent, false);
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
convertView.setTag(viewHolder);
} else {
//通过tag找到缓存的布局
viewHolder = (ViewHolder) convertView.getTag();
}
NewsBean newsBean = newsBeanList.get(position);
String urlString = newsBean.newsIconUrl;
viewHolder.ivIcon.setTag(urlString); // 将ImageView与url绑定
//普通异步加载
// mImageLoader.showImageByThread(viewHolder.ivIcon,urlString);
mImageLoader.showImageByAsyncTask(viewHolder.ivIcon,urlString);
viewHolder.tvTitle.setText(newsBean.newsTitle);
viewHolder.tvContent.setText(newsBean.newsContent);
return convertView;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState){
case SCROLL_STATE_IDLE: //滑动停止时。
mImageLoader.loadImages(mStart, mEnd);
break;
case SCROLL_STATE_TOUCH_SCROLL: //正在滑动时
mImageLoader.cancelAllTasks();
break;
case SCROLL_STATE_FLING: //手指抛动时,即手指用力滑动在离开后ListView由于惯性而继续滑动
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mStart = firstVisibleItem;
mEnd = firstVisibleItem + visibleItemCount;
//第一次的时候预加载
if (mFirstIn && visibleItemCount > 0){
mImageLoader.loadImages(mStart, mEnd);
mFirstIn = false;
}
}
//使用ViewHolder
private static class ViewHolder {
private TextView tvTitle, tvContent;
private ImageView ivIcon;
}
}
ImageLoader
package com.jack.jack_listview_optimize_demo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;
import com.jack.jack_listview_optimize_demo.adapter.NewsAdapter;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
/**
* Created by jack on 2016/7/3.
*/
public class ImageLoader {
private ImageView mImageView;
private String mUrl;
private LruCache<String, Bitmap> mCaches;
private ListView mListView;
private Set<NewsAsyncTask> mAsyncTask;
public ImageLoader(ListView listView){
mListView = listView;
mAsyncTask = new HashSet<>();
//下面是建立缓存
int maxMemory = (int) Runtime.getRuntime().maxMemory(); //运行时最大内存
int cacheSize = maxMemory/4;
mCaches = new LruCache<String, Bitmap>(cacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
//将bitmap添加到缓存
public void addBitmapToCache(String url,Bitmap bitmap){
if (getBitmapFormCache(url) == null){
mCaches.put(url, bitmap);
}
}
//从缓存中获取数据
public Bitmap getBitmapFormCache(String url){
return mCaches.get(url);
}
//===================================下面为普通异步加载===========================================
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mImageView.getTag().equals(mUrl)) { //当url标记和原先设置的一样时,才设置ImageView
mImageView.setImageBitmap((Bitmap) msg.obj);
}
}
};
public void showImageByThread(ImageView imageView, final String url) {
this.mImageView = imageView;
this.mUrl = url;
new Thread() {
@Override
public void run() {
super.run();
Bitmap bitmap = getBitmapFormURL(url);
Message message = Message.obtain();
message.obj = bitmap;
handler.sendMessage(message);
}
}.start();
}
//====================上面是使用普通的异步加载,下面是使用AsyncTask进行的异步加载==================
public Bitmap getBitmapFormURL(String urlString) {
Bitmap bitmap;
InputStream inputStream = null;
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
inputStream = new BufferedInputStream(conn.getInputStream()); //得到图片的数据流
bitmap = BitmapFactory.decodeStream(inputStream); //根据数据流来解析出图片的bitmap
conn.disconnect();
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//加载图片
public void showImageByCache(ImageView ImageView, String url) {
Bitmap bitmap = getBitmapFormCache(url);
if (bitmap == null){
ImageView.setImageResource(R.mipmap.ic_launcher);
}else{
ImageView.setImageBitmap(bitmap);
}
}
public void cancelAllTasks(){
if (mAsyncTask != null){
for (NewsAsyncTask task : mAsyncTask){
task.cancel(false);
}
}
}
public void loadImages(int start, int end){
for (int i = start; i < end; i++){
String url = NewsAdapter.URLS[i];
//由缓存中得到bitmap
Bitmap bitmap = getBitmapFormCache(url);
if (bitmap == null){
//当bitmap为空时,由AsyncTask进行加载,并在onPostExecute()方法中setImageBitmap
NewsAsyncTask task = new NewsAsyncTask(url);
task.execute(url);
mAsyncTask.add(task);
} else {
//当bitmap不为空时,直接进行setImageBitmap
ImageView imageView = (ImageView) mListView.findViewWithTag(url);
imageView.setImageBitmap(bitmap);
}
}
}
//参数1:启动任务输入的参数,参数2:后台任务执行的百分比,参数3,后台执行任务的返回方法
private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> {
private String mUrl;
public NewsAsyncTask(String stringUrl) {
mUrl = stringUrl;
}
//doInBackground方法的参数是上面输入的第一个参数,返回的对象会传递给onPostExecute方法
@Override
protected Bitmap doInBackground(String... params) {
String url = params[0];
Bitmap bitmap = getBitmapFormURL(url);
if (bitmap != null){
addBitmapToCache(url,bitmap); //将bitmap添加到缓存
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//根据url从listView中找到对应的ImageView
ImageView imageView = (ImageView) mListView.findViewWithTag(mUrl);
if (imageView != null && bitmap != null){
imageView.setImageBitmap(bitmap);
}
mAsyncTask.remove(this);
}
}
}
源码请戳我的github:
Jack_listview_optimize_demo