android listview 判断最后一行,listView优化,是否滑动,第一行,最后一行判断,判断何时要加载图片...

listView.setOnScrollListener(new OnScrollListener() {

private boolean isFling;

private boolean isLastRow;

private boolean isTop;

@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {

isFling=(scrollState == OnScrollListener.SCROLL_STATE_FLING);

if (!isLastRow&&!isTop && isFling) {

adapter.setLoading(false);//不加载图片

}else

adapter.setLoading(true);//加载图片

}

@Override

public void onScroll(AbsListView view, int firstVisibleItem,

int visibleItemCount, int totalItemCount) {

//判断是否滚到第一行

if (totalItemCount>0) {

if (firstVisibleItem == 0) {

isTop = true;

}else {

isTop = false;

}

}

//判断是否滚到最后一行

if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {

isLastRow = true;

}else {

isLastRow = false;

}

}

});

//adapter部分代码

private boolean isLoading=true;

public void setLoading(boolean isLoading) {

this.isLoading = isLoading;

this.notifyDataSetChanged();

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ListViewHolder viewHolder=null;

if (convertView==null)

{

viewHolder=new ListViewHolder();

convertView=View.inflate(context, R.layout.base_list_item, null);

viewHolder.tvTitle=(TextView) convertView.findViewById(R.id.tv_base_list_title);

viewHolder.tvTime=(TextView) convertView.findViewById(R.id.tv_base_list_time);

viewHolder.ivMsg=(ImageView) convertView.findViewById(R.id.iv_base_list_item);

convertView.setTag(viewHolder);

}else

{

viewHolder=(ListViewHolder) convertView.getTag();

}

Log.i("ListItemAdapter", "type:"+type);

InfoNews infoNews = ((InfoNews)list.get(position));

viewHolder.tvTitle.setText(infoNews.getTit());

String src = infoNews.getSrc();

if (src==null) {

src="";

}

viewHolder.tvTime.setText(infoNews.getAddDate()+"    "+src);

String path =infoNews.getImageUrl();

if(isLoading){

viewHolder.ivMsg.setTag(path);

Bitmap bm = loader.loadImage(path, 1);//异步加载图片

if (bm != null) {

viewHolder.ivMsg.setImageBitmap(bm);

} else {

viewHolder.ivMsg

.setImageResource(R.drawable.full_opacity);

}

} else{

viewHolder.ivMsg.setTag("abc");

viewHolder.ivMsg.setImageResource(R.drawable.full_opacity);

}

return convertView;

}

class ListViewHolder

{

TextView tvTitle;

TextView tvTime;

ImageView ivMsg;

}

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import android.graphics.Bitmap;

import android.graphics.Bitmap.CompressFormat;

import android.graphics.BitmapFactory;

import android.graphics.BitmapFactory.Options;

import android.util.Log;

public class BitmapUtils {

/* 下载图片的方法 */

public static Bitmap Downloadpic(String Url,int type) throws Exception{

try {

URL url = new URL(Url);

//URL url = new URL("http://e.hiphotos.baidu.com/p_w_picpath/w%3D2048/sign=f4ac7fda33adcbef0134790698972edd/3b292df5e0fe9925983f2f4e36a85edf8db171f8.jpg");

HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

httpConn.setReadTimeout(20000);

httpConn.setConnectTimeout(20000);

InputStream input = httpConn.getInputStream();

Bitmap bit = BitmapFactory.decodeStream(input);

input.close();

FileOutputStream b = null;

//

String name = Url.substring(Url.lastIndexOf("/"));

String fileName = "/sdcard/dxt/p_w_picpath/"+name ;

File file = new File(fileName);

save(bit, file,type);

return bit;

} catch (IOException e) {

// TODO Auto-generated catch block

throw new Exception(e.getMessage());

}

}

/**

*

* @param data

* @param width

* @param height

* @return

*/

public static Bitmap loadBitmap(byte[] data, int width, int height) {

Options opts = new Options();

opts.inJustDecodeBounds = true;

BitmapFactory.decodeByteArray(data, 0, data.length, opts);

int xScale = opts.outWidth / width;

int yScale = opts.outHeight / height;

opts.inSampleSize = xScale > yScale ? xScale : yScale;

opts.inJustDecodeBounds = false;

Bitmap bit = BitmapFactory.decodeByteArray(data, 0, data.length, opts);

return bit;

}

/**

*

* @param path

* @return

*/

public static Bitmap loadBitmap(String path) {

return BitmapFactory.decodeFile(path);

}

/**

*

* @param bm

* @param file

*/

public static void save(Bitmap bm, File file,int type) throws IOException {

Log.i("dxt", "save bm:"+file.getName());

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

if (!file.exists()) {

file.createNewFile();

}

FileOutputStream out = new FileOutputStream(file);

if (type ==1 ) {

bm.compress(CompressFormat.JPEG, 10, out);

}else {

bm.compress(CompressFormat.JPEG, 100, out);

}

out.close();

}

}

import java.io.File;

import java.io.IOException;

import java.lang.ref.SoftReference;

import java.util.ArrayList;

import java.util.HashMap;

import org.apache.http.HttpEntity;

import android.content.Context;

import android.graphics.Bitmap;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import com.ywtx.dxt.util.BitmapUtils;

import com.ywtx.dxt.util.HttpService;

public class AsyncImageLoader {

private Context context;

private boolean isLoop;

private ArrayList tasks;

private Thread workThread;

private Handler handler;

private HashMap> caches;

public AsyncImageLoader(final Context context, final Callback callback) {

this.context = context;

this.isLoop = true;

this.tasks = new ArrayList();

this.caches = new HashMap>();

this.handler = new Handler() {

public void handleMessage(android.os.Message msg) {

ImageLoadTask task = (ImageLoadTask) msg.obj;

callback.p_w_picpathLoaded(task.path, task.bitmap,task.type);

};

};

this.workThread = new Thread() {

@Override

public void run() {

while (isLoop) {

while (isLoop && !tasks.isEmpty()) {

try {

ImageLoadTask task = tasks.remove(0);

try {

Log.i("dxt", "runtask.path = "+task.path);

task.bitmap = BitmapUtils.Downloadpic(task.path,task.type);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Message msg = Message.obtain(handler, 0, task);

msg.sendToTarget();

caches.put(task.path, new SoftReference(task.bitmap));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (!isLoop)

break;

synchronized (this) {

try {

this.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

};

this.workThread.start();

}

public Bitmap loadImage(String path,int type) {

if (caches.containsKey(path)) {

Log.i("dxt", path+"缓存");

Bitmap bm = caches.get(path).get();

if (bm != null) {

return bm;

}

}

File dir = context.getCacheDir();

File file = new File(dir, path);

if (file.exists()) {

Log.i("dxt", path+"SD卡");

Bitmap bm = BitmapUtils.loadBitmap(file.getAbsolutePath());

if (bm != null) {

return bm;

}

}

ImageLoadTask task = new ImageLoadTask();

task.path = path;

task.type = type;

if (!tasks.contains(task)) {

Log.i("dxt", path+"download");

tasks.add(task);

synchronized (workThread) {

try {

workThread.notify();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return null;

}

public void quit() {

isLoop = false;

synchronized (workThread) {

workThread.notify();

}

}

private class ImageLoadTask {

private String path;

private Bitmap bitmap;

private int type;

@Override

public boolean equals(Object o) {

ImageLoadTask task = (ImageLoadTask) o;

if (path==null||task==null||task.path==null) {

return false;

}

return path.equals(task.path);

}

}

public interface Callback {

void p_w_picpathLoaded(String path, Bitmap bitmap, int type);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值