• ListView设置快速滑动时不加载图片


    设置ListView 快速滑动不加载要设置滑动监听OnScrollListener, 当快速滑动是设置标志位不请求网络

    这个也属于listview优化的一步

    设置监听器

     1 package com.example.listview_01;
     2 
     3 import android.widget.AbsListView;
     4 import android.widget.BaseAdapter;
     5 import android.widget.AbsListView.OnScrollListener;
     6 
     7 public class ListViewOnscrollLister implements OnScrollListener {
     8 
     9     // 添加标志位
    10     public static boolean isLoadImg = true;
    11 
    12     public BaseAdapter basedapter;
    13 
    14     public ListViewOnscrollLister(BaseAdapter basedapter) {
    15         this.basedapter = basedapter;
    16     }
    17 
    18     @Override
    19     public void onScrollStateChanged(AbsListView view, int scrollState) {
    20         switch (scrollState) {
    21         case OnScrollListener.SCROLL_STATE_FLING:
    22             isLoadImg = false;
    23             break;
    24         case OnScrollListener.SCROLL_STATE_IDLE:
    25             isLoadImg = true;
    26             basedapter.notifyDataSetChanged();
    27             break;
    28         case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
    29             isLoadImg = false;
    30             break;
    31 
    32         }
    33 
    34     }
    35 
    36     @Override
    37     public void onScroll(AbsListView view, int firstVisibleItem,
    38             int visibleItemCount, int totalItemCount) {
    39         // TODO Auto-generated method stub
    40 
    41     }
    42 
    43 }

    adapter设置

     1 package com.example.listview_01;
     2 
     3 import java.util.List;
     4 import java.util.concurrent.ArrayBlockingQueue;
     5 import java.util.concurrent.BlockingQueue;
     6 import java.util.concurrent.ThreadPoolExecutor;
     7 import java.util.concurrent.TimeUnit;
     8 
     9 import android.content.Context;
    10 import android.os.Handler;
    11 import android.util.Log;
    12 import android.view.LayoutInflater;
    13 import android.view.View;
    14 import android.view.ViewGroup;
    15 import android.widget.BaseAdapter;
    16 import android.widget.ImageView;
    17 import android.widget.TextView;
    18 
    19 import com.example.listview_01.ImageLoader.ImgResult;
    20 
    21 public class ImgAdapter extends BaseAdapter {
    22 
    23     private Context context;
    24     private List<ViewItem> list;
    25     private LayoutInflater inflater;
    26     private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(
    27             200);
    28     // 设置线程线程池
    29     private ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 10, 200,
    30             TimeUnit.SECONDS, workQueue);
    31     private Handler handler = new Handler() {
    32         public void handleMessage(android.os.Message msg) {
    33             ImgResult imgresult = (ImgResult) msg.obj;
    34             imgresult.img.setImageBitmap(imgresult.bitmap);
    35         };
    36     };
    37 
    38     public ImgAdapter(Context context, List<ViewItem> list) {
    39         this.context = context;
    40         this.list = list;
    41         inflater = LayoutInflater.from(context);
    42     }
    43 
    44     @Override
    45     public int getCount() {
    46         return list.size();
    47     }
    48 
    49     @Override
    50     public Object getItem(int position) {
    51         return list.get(position);
    52     }
    53 
    54     @Override
    55     public long getItemId(int position) {
    56         return position;
    57     }
    58 
    59     @Override
    60     public View getView(int position, View convertView, ViewGroup parent) {
    61         // TODO Auto-generated method stub
    62         ImgHolder imgHolder = null;
    63         if (null == convertView) {
    64             convertView = inflater.inflate(R.layout.activity_img, null);
    65             imgHolder = new ImgHolder(convertView);
    66             convertView.setTag(imgHolder);
    67         } else {
    68             imgHolder = (ImgHolder) convertView.getTag();
    69         }
    70         ViewItem item = list.get(position);
    71         imgHolder.addressTxt.setText(item.getAddress());
    72         imgHolder.titleTxt.setText(item.getTitle());
    73         // 设置快速滚动不加载图片停止滚动时加载图片
    74         if (MainActivity.onscroll.isLoadImg) {
    75             executor.submit(new ImageLoader(item.getImgUrl(), handler,
    76                     imgHolder.imgView));
    77         }
    78         return convertView;
    79     }
    80 
    81     class ImgHolder {
    82         public TextView titleTxt;
    83         public TextView addressTxt;
    84         public ImageView imgView;
    85 
    86         public ImgHolder(View convertView) {
    87             titleTxt = (TextView) convertView.findViewById(R.id.title);
    88             addressTxt = (TextView) convertView.findViewById(R.id.content);
    89             imgView = (ImageView) convertView.findViewById(R.id.imageView1);
    90         }
    91     }
    92 }

    线程下载图片

     1 package com.example.listview_01;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.net.HttpURLConnection;
     6 import java.net.MalformedURLException;
     7 import java.net.URL;
     8 
     9 import android.graphics.Bitmap;
    10 import android.graphics.BitmapFactory;
    11 import android.os.Handler;
    12 import android.os.Message;
    13 import android.widget.ImageView;
    14 
    15 public class ImageLoader implements Runnable {
    16 
    17     private String path;
    18     private Handler handler;
    19     private ImageView imgView;
    20 
    21     public ImageLoader(String url, Handler handler, ImageView imgView) {
    22         this.path = url;
    23         this.handler = handler;
    24         this.imgView = imgView;
    25     }
    26 
    27     @Override
    28     public void run() {
    29         try {
    30             URL url = new URL(path);
    31             HttpURLConnection connection = (HttpURLConnection) url
    32                     .openConnection();
    33             connection.setDoInput(true);
    34             connection.setRequestMethod("GET");
    35             InputStream is = connection.getInputStream();
    36             Bitmap bitmap = BitmapFactory.decodeStream(is);
    37             Message msg = Message.obtain();
    38             msg.obj = new ImgResult(imgView, bitmap);
    39             handler.sendMessage(msg);
    40             is.close();
    41             connection.disconnect();
    42         } catch (IOException e) {
    43             // TODO Auto-generated catch block
    44             e.printStackTrace();
    45         }
    46     }
    47 
    48     public class ImgResult {
    49         public ImageView img;
    50         public Bitmap bitmap;
    51 
    52         public ImgResult(ImageView imgs, Bitmap bitmaps) {
    53             img = imgs;
    54             bitmap = bitmaps;
    55         }
    56     }
    57 }

    ListView 设置监听

    1 adapter = new ImgAdapter(this, list);
    2 listView.setAdapter(adapter);
    3 onscroll = new ListViewOnscrollLister(adapter);
    4 listView.setOnScrollListener(onscroll);

  • 相关阅读:
    在vs2008中集成JavaScript Lint检查JavaScript语法
    (转载)SQL分页语句
    设置出错页
    判断2个输入框至少输入1个
    C#获取用户桌面等特殊系统路径
    创建存储过程的代码
    SqlParameter关于Like的传参数无效问题
    (转)利用Office里面的OWC组件进行画图
    firefox3不能获得html file的全路径的问题
    (转)使用ASP.NET上传图片汇总
  • 原文地址:https://www.cnblogs.com/lihaolihao/p/4415919.html
Copyright © 2020-2023  润新知