首页 > 代码库 > LIstview滑动时不加载图片,停止时加载!

LIstview滑动时不加载图片,停止时加载!

//参照 http://blog.csdn.net/yy1300326388/article/details/45153813 

public
class CarWashDistanceAdapter extends BaseAdapter { private static final String TAG = "CarWashDistanceAdapter"; private Context context; private MyDialog myDialog; private ArrayList<FourServiceBean> arrayList; private boolean scrollState=false; private float density = 1.5f; //定义当前listview是否在滑动状态 public void setScrollState(boolean scrollState) { this.scrollState = scrollState; } public CarWashDistanceAdapter(Context context, ArrayList<FourServiceBean> arrayList) { this.context = context; this.arrayList=arrayList; } public void refresh( ArrayList<FourServiceBean> arrayList) { this.arrayList=arrayList; notifyDataSetChanged(); } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return arrayList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { Hodler hodler; if (convertView == null) { hodler = new Hodler(); convertView = View.inflate(context, R.layout.listview_car_wash_item, null); hodler.mImageView = (ImageView) convertView.findViewById(R.id.car_wash_item_imageview); hodler.mTelImageview = (ImageView) convertView.findViewById(R.id.car_wash_item_phone_imageview); hodler.mTitleTextView = (TextView) convertView.findViewById(R.id.car_wash_item_title_textview); hodler.mDistanceTextView = (TextView) convertView.findViewById(R.id.car_wash_item_distance_textview); hodler.mSubTextView = (TextView) convertView.findViewById(R.id.car_wash_item_subtitle_textview); hodler.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.car_wash_item_relativelayout); hodler.mRatingBar = (RatingBar) convertView.findViewById(R.id.car_wash_item_ratingbar); convertView.setTag(hodler); } else { hodler = (Hodler) convertView.getTag(); } hodler.mTitleTextView.setText(arrayList.get(position).getShop_name()); hodler.mSubTextView.setText(arrayList.get(position).getDetail_address()); if(arrayList.get(position).getDistance()/1000 <= 1){ hodler.mDistanceTextView.setText((new DecimalFormat("0").format(arrayList.get(position).getDistance()) + "m")); }else { hodler.mDistanceTextView.setText((new DecimalFormat("0.00").format(arrayList.get(position).getDistance()/1000)+ "km")); } if(arrayList.get(position).getScore()!=null ) { hodler.mRatingBar.setRating(Float.valueOf(arrayList.get(position).getScore())); } final String[] sourceStrArray = arrayList.get(position).getTel_num_list().split(","); hodler.mTelImageview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myDialog = new MyDialog(context, R.style.MyDialog, new MyDialog.LeaveMyDialogListener() { @Override public void onClick(View view) { switch (view.getId()) { case R.id.mydialog_clear: myDialog.dismiss(); break; } } }, Arrays.asList(sourceStrArray),myDialog); myDialog.show(); } });
//这获取的是图片的url String url
= arrayList.get(position).getShop_pic_url(); if (!scrollState){ //如果当前不是滑动的状态,我们填充真数据 //加载图片 if(url!= null) {
//这是我添加图片的方法 THttpOpenHelper.newInstance().setImageBitmap(hodler.mImageView, url,(
int)(100 * density), (int)(75 * density), R.drawable.icon_default1, R.drawable.icon_default1); //设置tag为1表示已加载过数据 hodler.mImageView.setTag("1"); }else{ hodler.mImageView.setImageResource(R.drawable.icon_default1); hodler.mImageView.setTag("1"); } }else{ //如果当前是滑动的状态,我们填充假数据 //将数据image_url保存在Tag当中 hodler.mImageView.setTag(url); //设置默认显示图片(最好是本地资源的图片) hodler.mImageView.setImageResource(R.drawable.icon_default1); } return convertView; } private class Hodler { ImageView mImageView; ImageView mTelImageview; TextView mTitleTextView; TextView mSubTextView; TextView mDistanceTextView; RelativeLayout relativeLayout; RatingBar mRatingBar; } }

以上是Adapter中的代码:其中重要的就是

  //定义当前listview是否在滑动状态
    public void setScrollState(boolean scrollState) {
        this.scrollState = scrollState;
    }

还有这段是给imageview赋值的操作

 //这获取的是图片的url
        String url = arrayList.get(position).getShop_pic_url();
        if (!scrollState){
            //如果当前不是滑动的状态,我们填充真数据
            //加载图片
            if(url!= null) {
                //这是我添加图片的方法
                THttpOpenHelper.newInstance().setImageBitmap(hodler.mImageView, url,(int)(100 * density), (int)(75 * density), R.drawable.icon_default1, R.drawable.icon_default1);
                //设置tag为1表示已加载过数据
                hodler.mImageView.setTag("1");
            }else{
                hodler.mImageView.setImageResource(R.drawable.icon_default1);
                hodler.mImageView.setTag("1");
            }
        }else{
            //如果当前是滑动的状态,我们填充假数据
            //将数据image_url保存在Tag当中
              hodler.mImageView.setTag(url);
                //设置默认显示图片(最好是本地资源的图片)
                hodler.mImageView.setImageResource(R.drawable.icon_default1);
          }

最后在Activity中添加

     mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(AbsListView view, int scrollState) {
                        switch (scrollState){
                             //停止滚动
                            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                            {
                                //设置为停止滚动
                                carWashDistanceAdapter.setScrollState(false);
                                //当前屏幕中listview的子项的个数
                                int count = view.getChildCount();
                                for (int i = 0; i < count; i++) {
                                    //获取到item的图片显示的Imageview控件
                                    ImageView iv_show= (ImageView) view.getChildAt(i).findViewById(R.id.car_wash_item_imageview);
                                    if (!iv_show.getTag().equals("1")){//如果等于1说明图片资源已加载过,不等于说明没有去getTag()的图片url
                                      
                               //直接从Tag中取出我们存储的数据image——url
                                         String image_url = iv_show.getTag().toString();
                                         if (image_url != null) {//这个判断是防止图片的url是否为空,为空的话给默认图片。
                                             THttpOpenHelper.newInstance().setImageBitmap(iv_show, image_url, R.drawable.icon_default1, R.drawable.icon_default1);
                                             //设置为已加载过数据
                                             iv_show.setTag("1");
                                         } else {
                                             iv_show.setBackgroundResource(R.drawable.icon_default1);
                                             iv_show.setTag("1");
                                         }
                                    
                                    }
                                }
                                break;
                            }
                            //滚动做出了抛的动作
                            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                            {
                                //设置为正在滚动
                                carWashDistanceAdapter.setScrollState(true);
                                break;
                            }
                             //正在滚动
                            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                            {
                                //设置为正在滚动
                                carWashDistanceAdapter.setScrollState(true);
                                break;
                            }
                        }
                    }

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                    }
                });

 

LIstview滑动时不加载图片,停止时加载!