Android - 适配器错位

时间:2015-01-01 12:26:41

标签: android performance android-layout

我在OnClickListener中设置了更改背景图标:

        holder.rating_good.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
                apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "up", holder.rating, holder.rating_good);
            }

        });

        holder.rating_bad.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_down_red);
                apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "down", holder.rating, holder.rating_bad);
            }

        });

但是在某些项目背景更改中,即使我没有按下按钮。 标准背景ImageView如果这个rating_good - R.drawable.ic_thumb_down_grey,如果这个rating_bad - R.drawable.ic_thumb_down_grey。

我的getView:

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

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.feed_item, parent, false);

            holder.rating_good = (ImageView) convertView.findViewById(R.id.imageRatingGood);
            holder.rating_bad = (ImageView) convertView.findViewById(R.id.imageRatingBad);
            holder.rating = (TextView) convertView.findViewById(R.id.feedRating);
            holder.rating.setTag(position);

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

            try {

                item = feedItems.get(position);
                ctx = parent.getContext();

            holder.rating_good.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
                    apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "up", holder.rating, holder.rating_good);
                }

            });

            holder.rating_bad.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    holder.rating_good.setBackgroundResource(R.drawable.ic_thumb_down_red);
                    apiMethods.ChangePostRating(ctx, String.valueOf(item.getId()), "down", holder.rating, holder.rating_bad);
                }

            });

            //OTHER SOME CODE

                } catch (Exception e) {
                    e.printStackTrace();
                    Mint.logException(e);
                }

        return convertView;
    }

static class ViewHolder {
      TextView nickname;
      TextView date;
      TextView description;
      TextView rating;
      TextView type;
      FeedImageView feedImageView;
      ImageView rating_good;
      ImageView rating_bad;
      ImageView type_image;
      LinearLayout linear;
}

ApiMethods课程:

public void ChangePostRating(Context new_ctx, String id, String rate, TextView textview, ImageView rView) {
          ctx = new_ctx;
          token = pref.GetToken(ctx);
          content_id = id;
          rating_string = rate;
          url = apiUrl.GetUrlChangeRating();
          app_id = info.app_key;
          myView = textview;
          imageButton = (ImageView) rView;

          RequestQueue queue = Volley.newRequestQueue(ctx);
          StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String responses) {
                    try {
                        JSONObject response = new JSONObject(responses);
                        String status = response.getString("status");
                        if (!status.contains("error")) {
                            JSONObject data = response.getJSONObject("data");
                            int rating = data.getInt("rating");
                            TextView text_rating = (TextView)  myView;
                            text_rating.setText(String.valueOf(rating));
                            if (Integer.valueOf(text_rating.getText().toString()) < 0) {
                                text_rating.setTextColor(ctx.getResources().getColor(R.color.feed_item_rating_bad));
                            } else {
                                text_rating.setTextColor(ctx.getResources().getColor(R.color.feed_item_rating_good));
                            }
                        } else {
                            JSONObject error = response.getJSONObject("error");
                            String message = error.getString("message");
                            parseError(message);
                            if (rating_string.contains("up")) {
                                imageButton.setBackgroundResource(R.drawable.ic_thumb_up_grey);
                            } else {
                                imageButton.setBackgroundResource(R.drawable.ic_thumb_down_grey);
                            }

                        }

                    } catch (JSONException e) {
                        Mint.logException(e);
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Mint.logException(error);
                }
            });
           queue.add(sr);
       }

1 个答案:

答案 0 :(得分:0)

你应该只为每个视图订阅一次事件处理程序,如下所示(因为我在这里打字但未经测试,但你明白了)。由于您正在缓存它们(因此使用ViewHolder),多次订阅它们会导致问题。

现在,这是否可以解决您的问题是另一回事,但它仍然是您需要解决的问题。

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

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();

    if (convertView == null) {
        if (inflater == null)
           inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.feed_item, parent, false);

        holder.rating_good = (ImageView) convertView.findViewById(R.id.imageRatingGood);
        holder.rating_bad = (ImageView) convertView.findViewById(R.id.imageRatingBad);
        holder.rating = (TextView) convertView.findViewById(R.id.feedRating);

        holder.rating_good.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ViewHolder viewHolder = (ViewHolder)v.getTag()  
                viewHolder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_red);
                apiMethods.ChangePostRating(ctx, String.valueOf(viewHolder.item.getId()), "up", viewHolder.rating, viewHolder.rating_good);
            }
        });

        holder.rating_bad.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               ViewHolder viewHolder = (ViewHolder)v.getTag()  
               viewHolder.rating_good.setBackgroundResource(R.drawable.ic_thumb_up_green);
                apiMethods.ChangePostRating(ctx, String.valueOf(viewHolder.item.getId()), "down", viewHolder.rating, viewHolder.rating_bad);
            }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

        try {

            item = feedItems.get(position);
            ctx = parent.getContext();
            holder._rating_bad.setTag(holder);
            holder._rating_good.setTag(holder);
            holder.rating.setTag(position);
            holder._item = item;
        });

        //OTHER SOME CODE

            } catch (Exception e) {
                e.printStackTrace();
                Mint.logException(e);
            }

    return convertView;
}