1

我有一个包含项目列表的 ListView。每个项目都有一个带有星号的小 ImageButton,将其标记为收藏。单击星形 ImageButton 时,我希望它进行一点旋转。在 Android 4.3 中运行良好,但在 2.3 中,不仅点击的星会旋转,而且其上方项目中的星也会旋转。

我已经尝试不使用 ViewHolder 作为星星,因为我认为这可能会导致它,但这并没有帮助。

顺便说一句,我在 onClickListener 中做了其他事情,而且效果很好,并且只适用于被点击的项目。

我的问题可能是什么?

这是列表适配器中我的 getView 方法的一部分:

    CheckableImageButton favBtn = (CheckableImageButton) convertView
            .findViewById(R.id.favbtn);

    favBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            v.startAnimation(AnimationUtils.loadAnimation(ctx,
                    R.anim.spinanim));

        }
    });

这是我的整个 getView 方法:

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    checkLoadMore(position);

    final Item item = combinedResult.getItems().get(position);

    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.item_fewo_v7, null);

        holder = new ViewHolder();

        holder.Iv = (RecyclingImageView) convertView
                .findViewById(R.id.imageView1);
        holder.Tv1 = (TextView) convertView.findViewById(R.id.textView1);
        holder.Tv2 = (TextView) convertView.findViewById(R.id.textView2);
        holder.Tv3 = (TextView) convertView.findViewById(R.id.textView3);
        holder.Tv4 = (TextView) convertView.findViewById(R.id.textView4);
        holder.Root = (RelativeLayout) convertView.findViewById(R.id.root);
        holder.Starbg = (RelativeLayout) convertView
                .findViewById(R.id.starbg);

        convertView.setTag(holder);
    }

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

    CheckableImageButton favBtn = (CheckableImageButton) convertView
            .findViewById(R.id.favbtn);

    favBtn.setChecked(FavItems.contains(item.getGuid()));

    favBtn.setOnClickListener(new OnClickListener() {

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

            if (FavItems.contains(item.getGuid())) {
                FavItems.removeFav(item.getGuid());
            }

            else {
                FavItems.addFav(item);
            }

            v.startAnimation(AnimationUtils.loadAnimation(ctx,
                    R.anim.spinanim));

        }
    });

    holder.Tv1.setText(item.getTitle());
    holder.Tv2.setText(item.getLocation().getDescription());
    holder.Tv3.setText(item.getPrice().getPriceText() + " "
            + item.getPrice().getPaymentIntervalShort());
    holder.Tv4.setText("bis " + item.getPersonsMax() + " Personen");

    displayImage(Tools.convertImageUrl(item.getThumbnailUrl()), holder.Iv,
            holder.Root);


    return convertView;
}

在此处输入图像描述

4

2 回答 2

3

Gopal Rao所说的相反,您应该使用 convertView 和 ViewHolder 模式。此外,您可以将 FavBtn 移动到 ViewHolder 中。但是,当视图被重用时,您确实需要清除动画,这在 else 子句中完成。以下应该有效:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    checkLoadMore(position);

    final Item item = combinedResult.getItems().get(position);

    if (convertView == null) {
        convertView = myInflater.inflate(R.layout.item_fewo_v7, null);

        holder = new ViewHolder();
        holder.Iv = (RecyclingImageView) convertView.findViewById(R.id.imageView1);
        holder.Tv1 = (TextView) convertView.findViewById(R.id.textView1);
        holder.Tv2 = (TextView) convertView.findViewById(R.id.textView2);
        holder.Tv3 = (TextView) convertView.findViewById(R.id.textView3);
        holder.Tv4 = (TextView) convertView.findViewById(R.id.textView4);
        holder.Root = (RelativeLayout) convertView.findViewById(R.id.root);
        holder.Starbg = (RelativeLayout) convertView.findViewById(R.id.starbg);
        holder.FavBtn = (CheckableImageButton) convertView.findViewById(R.id.favbtn);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
        holder.FavBtn.setAnimation(AnimationUtils.loadAnimation(ctx, R.anim.spinanim));
    }

    holder.FavBtn.setChecked(FavItems.contains(item.getGuid()));
    holder.FavBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (FavItems.contains(item.getGuid())) {
                FavItems.removeFav(item.getGuid());
            } else {
                FavItems.addFav(item);
            }

            holder.FavBtn.getAnimation().startNow();
        }
    });

    holder.Tv1.setText(item.getTitle());
    holder.Tv2.setText(item.getLocation().getDescription());
    holder.Tv3.setText(item.getPrice().getPriceText() + " " + item.getPrice().getPaymentIntervalShort());
    holder.Tv4.setText("bis " + item.getPersonsMax() + " Personen");

    displayImage(Tools.convertImageUrl(item.getThumbnailUrl()), holder.Iv, holder.Root);

    return convertView;
}
于 2013-10-24T09:36:42.277 回答
0

尝试这个...

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

        checkLoadMore(position);

        final Item item = combinedResult.getItems().get(position);

        convertView = myInflater.inflate(R.layout.item_fewo_v7, null);

        RecyclingImageView Iv = (RecyclingImageView) convertView
                .findViewById(R.id.imageView1);

        TextView Tv1 = (TextView) convertView.findViewById(R.id.textView1);
        TextView Tv2 = (TextView) convertView.findViewById(R.id.textView2);
        TextView Tv3 = (TextView) convertView.findViewById(R.id.textView3);
        TextView Tv4 = (TextView) convertView.findViewById(R.id.textView4);
        RelativeLayout Root = (RelativeLayout) convertView.findViewById(R.id.root);
        RelativeLayout Starbg = (RelativeLayout) convertView
                .findViewById(R.id.starbg);



        CheckableImageButton favBtn = (CheckableImageButton) convertView
                .findViewById(R.id.favbtn);

        favBtn.setChecked(FavItems.contains(item.getGuid()));

        favBtn.setOnClickListener(new OnClickListener() {

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

                if (FavItems.contains(item.getGuid())) {
                    FavItems.removeFav(item.getGuid());
                }

                else {
                    FavItems.addFav(item);
                }

                v.startAnimation(AnimationUtils.loadAnimation(ctx,
                        R.anim.spinanim));

            }
        });

        Tv1.setText(item.getTitle());
        Tv2.setText(item.getLocation().getDescription());
        Tv3.setText(item.getPrice().getPriceText() + " "
                + item.getPrice().getPaymentIntervalShort());
        Tv4.setText("bis " + item.getPersonsMax() + " Personen");

        displayImage(Tools.convertImageUrl(item.getThumbnailUrl()), Iv,
                Root);


        return convertView;
    }

停止使用 convertview 检查空值,因为当 convertview 不为空时,您将返回相同的视图实例。因此具有相同实例的视图将具有相同的动画

于 2013-10-24T07:45:49.043 回答