Android 自定义适配器 - 错误项目上的动画

Android Custom Adapter - Animation on wrong Item

当我对这个问题感到困惑时,我正在 Android 中使用自定义适配器进行试验:我的 MainActivity 包含一个 ListView 和一个 TextViewImageView每行。 ImageView 只是显示一种颜色。我的 ListView 有一个模型,其中包含一个 String 作为名称和一个 int 作为键。 我的 Adapter 应该为每个数字(键)的 ImageView 设置不同的背景颜色。如果键是 4,ImageView 应该闪烁。 如果我 运行 我在 List 中的代码第一个元素也在闪烁,但键不是 4。 希望有人能解释我做错了什么。

public class myAdapter extends ArrayAdapter<Test>{

    private ArrayList<Test> liste;

    public myAdapter(Context context, int resource, ArrayList<Test> items) {
        super(context, resource, items);
        liste = items;
    }

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

        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.list_view_item_main, parent, false);
        }

        Test t = getItem(position);
        TextView tv = (TextView) convertView.findViewById(R.id.lvName);
        ImageView iv = (ImageView) convertView.findViewById(R.id.lvStatus);

        int k = t.getKey();
        tv.setText(t.getName());

        if (k == 1) {
            iv.setBackgroundColor(Color.rgb(102, 255, 51));
        }

        if (k == 2) {
            iv.setBackgroundColor(Color.rgb(255, 204, 51));
        }

        if (k == 3) {
           iv.setBackgroundColor(Color.rgb(245, 61, 0));
        }

        if (k == 4) {
            iv.setBackgroundColor(Color.rgb(245, 61, 0));
            final Animation animation = new AlphaAnimation(1, 0);
           animation.setDuration(1000);
            animation.setInterpolator(new LinearInterpolator());
            animation.setRepeatCount(Animation.INFINITE);
            animation.setRepeatMode(Animation.REVERSE);
            iv.startAnimation(animation);
        }


        return convertView;


    }

}

public class Test {

    private String name;
    private int key;


    public Test(String name, int key) {
        this.name = name;
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }
}


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<Test> liste = new ArrayList<>();
        liste.add(new Test("Milch", 1));
        liste.add(new Test("Käse", 2));
        liste.add(new Test("Schokolade", 3));
        liste.add(new Test("Capri Sonne", 4));

        ListView lv = (ListView) findViewById(R.id.lvShowFridge);
        myAdapter mya = new myAdapter(this, R.layout.list_view_item_main, liste);
        lv.setAdapter(mya);
    }   

}

请记住,项目的视图会被重复使用(如果 convertView 在传递给 getView(...) 时不为空)。曾经用于显示 item1 的一个特定视图可能会传递给 getView(...) 以显示,比方说,item4。这意味着您在调用 item1 时设置的所有属性仍然有效——包括背景和动画。

有什么作用?嗯...:[=​​14=]

1 - 如果你没有专门设置背景,它会保留以前的(在你的情况下,正如你所说,如果你在 k == 1 时取消设置背景,它只会保留以前的设置颜色,恰好是 k ==4 )

2 - 当您被调用 k == 4 时,您正在启动动画,但如果视图被重用于任何其他 color/index,您不会停止它。适当地添加对 iv.clearAnimation() 的调用(对于不需要动画的情况)。