在滚动期间更改ListView图像

时间:2014-01-16 06:54:35

标签: android android-listview

我尝试使用动态图像制作listview,使用asynctask下载图像并设置为listview。我的问题是当向下滚动图像时随机改变..

class ps1 extends ArrayAdapter<String> {
    Context context;
    String[] images1;
    List mList;
    String[] namearray;
    String[] rating;

    static class ViewHolder {
        ImageView localImageView1;
        ImageView localImageView2;
        ImageView localImageView3;
    }

    ps1(Context paramContext, String[] paramArrayOfString1, String[] paramArrayOfString2, String[] paramArrayOfString3) {
        super(paramContext, R.layout.list2, R.id.imageView1, paramArrayOfString1);
        this.context = paramContext;
        this.images1 = paramArrayOfString3;
        this.namearray = paramArrayOfString1;
        this.rating = paramArrayOfString2;
    }

    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(context.LAYOUT_INFLATER_SERVICE);

    ViewHolder viewHolder = new ViewHolder();
    if (paramView == null) {
        paramView = inflater.inflate(R.layout.list2, paramViewGroup, false);

    }


    viewHolder.localImageView1 = (ImageView) paramView
            .findViewById(R.id.imageView1);
    viewHolder.localImageView2 = (ImageView) paramView
            .findViewById(R.id.imageView2);
    viewHolder.localImageView3 = (ImageView) paramView
            .findViewById(R.id.imageView3);


    viewHolder.localImageView1.setScaleType(ImageView.ScaleType.FIT_XY);
    viewHolder.localImageView2.setScaleType(ImageView.ScaleType.FIT_XY);
    viewHolder.localImageView3.setScaleType(ImageView.ScaleType.FIT_XY);

    viewHolder.localImageView1.setTag(this.namearray[paramInt]);
    new LoadImage().execute(viewHolder.localImageView1);
    viewHolder.localImageView2.setTag(this.rating[paramInt]);
    new LoadImage().execute(viewHolder.localImageView2);
    viewHolder.localImageView3.setTag(this.images1[paramInt]);
    new LoadImage().execute(viewHolder.localImageView3);


    return paramView;
    }
    }

    class LoadImage extends AsyncTask<Object, Void, Bitmap> {
    private ImageView imv;

    private Bitmap download_Image(String paramString) {
    Bitmap localBitmap = null;
    try {
        Object localObject = null;
        localBitmap = BitmapFactory
                .decodeStream(((HttpURLConnection) new URL(paramString)
                        .openConnection()).getInputStream());
        localObject = localBitmap;
        if (localObject != null) {
            return localBitmap;
        }
    } catch (Exception e) {

    }
    return localBitmap;
    }

    protected Bitmap doInBackground(Object... paramVarArgs) {
    this.imv = ((ImageView) paramVarArgs[0]);
    Log.d("fsdf", (String) this.imv.getTag());
    return download_Image((String) this.imv.getTag());
    }

    protected void onPostExecute(Bitmap paramBitmap) {
    this.imv.setImageBitmap(paramBitmap);
    }
}

2 个答案:

答案 0 :(得分:0)

我也经历过同样的事情。我也在寻找合适的解决方案。据我所知,我发现ListView在向下滚动时清除了上一个视图,并在向后滚动时重新加载它。因此,在向上和向下滚动时,您的图像可能会重新循环并且不对齐。 (我也在等待正确的解决方案)。

但是我使用SmartImageView来解决它,这是一个直接下载图像并将其设置为ImageView的库。它会将图像保存在缓存中,因此您可以获得正确的图像。

相比之下,这也更快。

答案 1 :(得分:-1)

尝试使用我在应用程序中使用过的代码段,它在我的应用程序中正常运行,我相信它会在你的最后工作。 在我的情况下,我正在从服务器检索图像和一些数据,并保持列表上的所有图像滚动正常。

class OfferCustomListAdapter extends ArrayAdapter<String>
    {    
       private Context context;
       Boolean OddNumber;
       ArrayList<String>  getDealID = new ArrayList<String>();
       ArrayList<String>  getInAdpterUNamedlist = new ArrayList<String>();
       ArrayList<String>  getShopNData = new ArrayList<String>();
       ArrayList<String>  getUserFav = new ArrayList<String>();
       ArrayList<String>  getTotalAmt = new ArrayList<String>();
       ArrayList<String>  getDealImage = new ArrayList<String>();
       ArrayList<Boolean> getBoolnState = new ArrayList<Boolean>();
       //String Oflist[] ;
       int favCount=0;
      public OfferCustomListAdapter(Context context,ArrayList<String> dealIdlist, ArrayList<Boolean> AddBoolnList, ArrayList<String> dealNamelist,ArrayList<String> ShopNList,ArrayList<String> UserFave,ArrayList<String> TotalAmt,ArrayList<String> ImageList) {
        super(context, android.R.layout.simple_list_item_1,dealNamelist);
        this.context=context;
        //Oflist = getFolwerUNamelis;
        getDealID = dealIdlist;
        getInAdpterUNamedlist = dealNamelist;
        getShopNData = ShopNList;
        getUserFav = UserFave;
        getTotalAmt = TotalAmt;
        getDealImage = ImageList;
        getBoolnState = AddBoolnList;


    }

    @Override
    public View getView(final int pos, View view, ViewGroup parent) {

        final ViewHolder holder;
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(this.context);
            //view = inflater.inflate(R.layout.offer_custom_list, parent,false);
            view = inflater.inflate(R.layout.reservatin_row, parent,false);
            holder = new ViewHolder();
            //holder.FollowrName = (TextView) view.findViewById(R.id.OfferNameTxt);
            holder.DealName = (TextView) view.findViewById(R.id.tv_name);
            holder.ShopName = (TextView) view.findViewById(R.id.tv_address);
            holder.FavBtn = (ImageView) view.findViewById(R.id.Ofr_FavBtn);
            holder.listLayout = (LinearLayout) view.findViewById(R.id.OfferListLayout);
            holder.profile_image = (ImageView)view.findViewById(R.id.profile_img);
            holder.OfferAmtBtn =(Button)view.findViewById(R.id.TotalOfrBtn);
            //holder.FavBtn = (ImageView) view.findViewById(R.id.offerFavBtn);
            holder.FavBtn.setTag(pos);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

         if ( pos % 2 == 0 ){
             System.out.println("You entered an even number.  "+pos % 2);
             holder.listLayout.setBackgroundResource(R.drawable.offer_list_bg);
         }else{
             System.out.println("You entered an odd number.");
             holder.listLayout.setBackgroundResource(R.drawable.special_offer_bg);
         }
          /*if(getUserFav.get(pos).equals("0")){
            //BolArraylist.add(false);
            holder.FavBtn.setBackgroundResource(R.drawable.fav_btn);
          }else{
            //BolArraylist.add(true);
            holder.FavBtn.setBackgroundResource(R.drawable.fav_active_btn);
          }*/
            holder.DealName.setText(getInAdpterUNamedlist.get(pos));
            holder.ShopName.setText(getShopNData.get(pos));
            holder.OfferAmtBtn.setText("$"+getTotalAmt.get(pos));
            imgLoader.DisplayImage(getDealImage.get(pos), holder.profile_image);

         holder.FavBtn.setOnClickListener(new View.OnClickListener() {

                @Override       
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (isNetworkAvailable()) {

                    if(!userid.equals("")){
                        Offer_ID = getDealID.get(pos);
                        GUsrFavState = getUserFav.get(pos);
                        if(GUsrFavState.equals("0")){
                            GUsrFavState="1";
                            getUserFav.remove(pos);
                            getUserFav.add(pos, "1");
                            holder.FavBtn.setBackgroundResource(R.drawable.fav_active_btn);
                            getBoolnState.set(pos, true);
                            new Call_OfferFavWS().execute();
                        }else{
                            GUsrFavState="0";
                            holder.FavBtn.setBackgroundResource(R.drawable.fav_btn);
                            getUserFav.remove(pos);
                            getUserFav.add(pos, "0");
                            getBoolnState.set(pos, false);
                            new Call_OfferFavWS().execute();
                        }  
                     }else{
                         Intent CallSignIn = new Intent(DollarMainActivity.this,SingInActivity.class);
                         startActivity(CallSignIn);
                       }
                    } else {
                        Toast alrtMsg = Toast.makeText(DollarMainActivity.this, "No network connection available !!!", Toast.LENGTH_LONG);
                        alrtMsg.setGravity(Gravity.CENTER, 0, 0);
                        alrtMsg.show();
                    }

                }
            });

          if(getBoolnState.get(pos)){
                holder.FavBtn.setBackgroundResource(R.drawable.fav_active_btn);
            }else{
                holder.FavBtn.setBackgroundResource(R.drawable.fav_btn);  
            }
            return view;
      }

      class ViewHolder {
             public TextView DealName,ShopName;
            public ImageView FavBtn, profile_image;
            public LinearLayout listLayout;
            public Button OfferAmtBtn;
      }
  }

希望它会对你有所帮助。

如果您需要任何帮助请告诉我。