2015-08-28 29 views
-1

当我上下滚动并且列表项中的图像每次都更新时,我的列表视图正在更新。我如何克服这个问题?我的listview更新时,在android中上下滚动?

这是Adapter类

public class CustomAdapter extends BaseAdapter { 

    ArrayList<String> p_id; 
    ArrayList<String> p_name; 
    Context context; 
    ArrayList<String> imageId; 
    ArrayList<String> wash_v; 
    ArrayList<String> dry_v; 
    ArrayList<String> iron_v; 
    Holder holder; 
    Typeface tf; 
    ProgressDialog dialog; 
    Intent i; 
    float washItemCount = 0.0f; 
    private static LayoutInflater inflater = null; 

    Handler handler; 
    Runnable myRunnable; 

    public CustomAdapter(Context con, ArrayList<String> pid, 
      ArrayList<String> pname, ArrayList<String> pimg, 
      ArrayList<String> wash, ArrayList<String> dry, 
      ArrayList<String> iron) { 
     p_id = pid; 
     context = con; 
     p_name = pname; 
     imageId = pimg; 
     wash_v = wash; 
     dry_v = dry; 
     iron_v = iron; 
     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    @Override 
    public int getCount() { 
     return imageId.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    public class Holder { 
     TextView tv1, tv2, tv3; 
     ImageView img; 
     LinearLayout ll1, ll2, ll3; 
    } 

    @SuppressLint("UseValueOf") 
    @Override 
    public View getView(final int position, final View convertView, 
      ViewGroup parent) { 
     holder = new Holder(); 

     final View rowView; 

     rowView = inflater.inflate(R.layout.dynamic, null); 
     holder.ll1 = (LinearLayout) rowView.findViewById(R.id.ll1); 
     holder.ll2 = (LinearLayout) rowView.findViewById(R.id.ll2); 
     holder.ll3 = (LinearLayout) rowView.findViewById(R.id.ll3); 
     holder.tv1 = (TextView) rowView.findViewById(R.id.tv1); 
     holder.tv2 = (TextView) rowView.findViewById(R.id.tv3); 
     holder.tv3 = (TextView) rowView.findViewById(R.id.tv5); 
     holder.img = (ImageView) rowView.findViewById(R.id.ivImage); 

     final TextView tvVal = (TextView) rowView.findViewById(R.id.tvVal); 
     final TextView tvVal1 = (TextView) rowView.findViewById(R.id.tvVal1); 
     final TextView tvVal2 = (TextView) rowView.findViewById(R.id.tvVal2); 

     holder.tv1.setText(wash_v.get(position)); 
     holder.tv2.setText(dry_v.get(position)); 
     holder.tv3.setText(iron_v.get(position)); 

     holder.ll1.setTag(new Integer(position)); 
     holder.ll2.setTag(new Integer(position)); 
     holder.ll3.setTag(new Integer(position)); 

     i = new Intent("com.example.safewash"); 
     final String image = imageId.get(position).toString(); 
     Log.i("Image in String", "" + image); 

     holder.ll1.setOnClickListener(new OnClickListener() { 

      @SuppressLint("NewApi") 
      @Override 
      public void onClick(final View v) { 
       int count = 0; 
       int colorStart = Color.LTGRAY; 
       int colorEnd = 0; 

       ValueAnimator animator = ObjectAnimator.ofInt(v,"backgroundColor", colorStart, colorEnd); 
       animator.setDuration(800); 
       animator.setEvaluator(new ArgbEvaluator()); 
       animator.setRepeatCount(0); 
       animator.start(); 

       String washItem = wash_v.get(position); 
       int washItems = Integer.parseInt(washItem); 

       if (washItems != 0) { 

        washItemCount = Float.parseFloat(washItem); 
        i.putExtra("washItemCount", washItemCount); 
        context.sendBroadcast(i); 
        tvVal.setVisibility(View.VISIBLE); 

        count = Integer.parseInt(tvVal.getText().toString()); 
        int countVal = ++count; 
        tvVal.setText("" + countVal); 

       } 

      } 

     }); 
     holder.ll2.setOnClickListener(new OnClickListener() { 

      @SuppressLint("NewApi") 
      @Override 
      public void onClick(final View v) { 
       int count = 0; 
       String washItem = dry_v.get(position); 
       int colorStart = Color.LTGRAY; 
       int colorEnd = 0; 

       ValueAnimator animator = ObjectAnimator.ofInt(v, 
         "backgroundColor", colorStart, colorEnd); 
       animator.setDuration(800); 
       animator.setEvaluator(new ArgbEvaluator()); 
       animator.setRepeatCount(0); 
       animator.start(); 
       washItemCount = Float.parseFloat(washItem); 
       int washItems = Integer.parseInt(washItem); 
       if (washItems != 0) { 

        washItemCount = Float.parseFloat(washItem); 
        i.putExtra("washItemCount", washItemCount); 
        context.sendBroadcast(i); 
        tvVal1.setVisibility(View.VISIBLE); 
        count = Integer.parseInt(tvVal1.getText().toString()); 
        int countVal = ++count; 
        tvVal1.setText("" + countVal); 
       } 

      } 

     }); 

     holder.ll3.setOnClickListener(new OnClickListener() { 

      @SuppressLint("NewApi") 
      @Override 
      public void onClick(final View v) { 

       int count = 0; 

       int colorStart = Color.LTGRAY; 
       int colorEnd = 0; 

       ValueAnimator animator = ObjectAnimator.ofInt(v, 
         "backgroundColor", colorStart, colorEnd); 
       animator.setDuration(800); 
       animator.setEvaluator(new ArgbEvaluator()); 
       animator.setRepeatCount(0); 
       animator.start(); 
       String washItem = iron_v.get(position); 
       washItemCount = Float.parseFloat(washItem); 
       int washItems = Integer.parseInt(washItem); 
       if (washItems != 0) { 

        washItemCount = Float.parseFloat(washItem); 
        i.putExtra("washItemCount", washItemCount); 
        context.sendBroadcast(i); 

        tvVal2.setVisibility(View.VISIBLE); 
        count = Integer.parseInt(tvVal2.getText().toString()); 
        int countVal = ++count; 
        tvVal2.setText("" + countVal); 

       } 
      } 
     }); 

     ImageDownloadTask imageDownloadTask = new ImageDownloadTask(); 
     imageDownloadTask.execute(image); 

     return rowView; 
    } 

    public class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> { 

     @Override 
     protected Bitmap doInBackground(String... urls) { 
      Bitmap map = null; 
      for (String url : urls) { 
       map = downloadImage(url); 

       Log.i("BitMap in DoinBackGround::", "" + map); 
      } 
      return map; 
     } 

     @SuppressWarnings("deprecation") 
     @Override 
     protected void onPostExecute(Bitmap result) { 

      BitmapDrawable ob = new BitmapDrawable(context.getResources(), 
        result); 

      holder.img.setBackgroundDrawable(ob); 
      super.onPostExecute(result); 
     } 

    } 

    public Bitmap downloadImage(String url) { 
     Bitmap bitmap = null; 
     InputStream stream = null; 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 

     try { 
      stream = getHttpConnection(url); 
      bitmap = BitmapFactory.decodeStream(stream, null, bmOptions); 

      stream.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     return bitmap; 
    } 

    private InputStream getHttpConnection(String urlString) throws IOException { 
     InputStream stream = null; 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 

     try { 
      HttpURLConnection httpConnection = (HttpURLConnection) connection; 
      httpConnection.setRequestMethod("GET"); 
      httpConnection.connect(); 

      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       stream = httpConnection.getInputStream(); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return stream; 
    } 

} 
+0

除去所有图像下载相关的代码和与http://square.github.io/替换毕加索/ – Budius

+0

尝试在您的Holder类之前放置'static'。看到这个链接更多信息http://developer.android.com/intl/zh-cn/training/improving-layouts/smooth-scrolling.html –

回答

1

错误的持有人格局

试试这个代码:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if(convertView == null){ 
     convertView = mInflater.inflate(R.layout...., null); 
     holder = new ViewHolder(); 

     holder.img = (ImageView) convertView.findViewById(R.id....); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    // set row code 
} 

,您可以使用图像加载库(快&有用...)

如果你有imageurl,你可以替换为AUIL。 (代替ImageDownloadTask)

Android的通用-图像 - 装载机:https://github.com/nostra13/Android-Universal-Image-Loader

例如:

ImageLoader.getInstance().displayImage(imageId.get(position).toString(), holder.img, YOUOPTION); 
+0

这里YOUOPTION意味着 –

+0

您搜索“android通用图像加载器”。 – LEETAEJUN

+0

或排球和presso ...等。无论 – LEETAEJUN