在android中使用闪烁的imageView时,CustomListView中的bug



目前,我正在制作一个简单的应用程序,使用customlistview在listView的第一个位置显示一个"new!"图标,并在当前日期与json数据的日期匹配时显示一个闪烁的"new!!"图标。

在第一个位置(0)显示正常的"new!"图标时,一切正常,因为它只显示在第一个位置。但是,当使用闪烁图标时,当向下滚动时,它会显示在列表视图的随机位置。
既然没有错误,我觉得很难解决这个问题。
我已经提供了下面使用的示例代码。

  public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.customloto7newpack,
                    null);
            holder = new ViewHolder();
            // setting up the basic things in here
            holder.left = (ImageView) convertView
                    .findViewById(R.id.newicons);
            holder.txt_maintext = (TextView) convertView 
                    .findViewById(R.id.loto7newdesu);
            holder.txt_lotodate = (TextView) convertView
                    .findViewById(R.id.datenew7);
            holder.lotoname = (TextView) convertView
                    .findViewById(R.id.lotoname);
        holder.txt_mtext = (TextView) convertView
         .findViewById(R.id.txt_mtext);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (key == 1) {


            holder.lotoname.setText("key1");
            // setting up the 3 variables in here
            holder.txt_maintext.setText(kai.get(position));
            holder.txt_lotodate.setText("New Date:" + loto_date.get(position));
            if (position == 0) {
                if (newIconParam.get(0).equals("OK")
                        || newIconParam.get(0) == "OK") {
                    dateonly = loto_date.get(0).trim().toString();
                    dateonly2 = dateonly.replaceAll("\(.*?\) ?", "");
                    String date3 = dateonly2.trim();// use this to check

                        if (date3.equals(today)) {
                        logicflag = true;
                        holder.left.setBackgroundResource(R.drawable.blinker);
                        AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
                                .getBackground();
                        frameAnimation.start();

                    } else if (!date3
                            .equals(today)) {
                        holder.left.setImageResource(R.drawable.new_icon);
                        // holder.left.setImageResource(android.R.color.transparent);
                    }
                } else {
                }
            } else {
                holder.left.setImageResource(android.R.color.transparent);
            }
        } else if (key2 == 2) {
            holder.lotoname.setText("Key2");
            holder.txt_maintext.setText(kai.get(position));
            holder.txt_lotodate.setText("New Date: " + loto_date.get(position));
            if (position == 0) {
                if (newIconParam.get(0).equals("OK")
                        || newIconParam.get(0) == "OK") {
                    dateonly = loto_date.get(0).trim().toString();
                    dateonly2 = dateonly.replaceAll("\(.*?\) ?", "");
                    String date3 = dateonly2.trim();

                    if (date3.equals(today)) {
                        // if (loto_date.get(0).trim().toString().equals(today))
                        // {
                        logicflag = true;
                        /*
                         * holder.left.setBackgroundResource(R.drawable.blinker);
                         * AnimationDrawable frameAnimation =
                         * (AnimationDrawable) holder.left.getBackground();
                         * frameAnimation.start();
                         * 
                         * 
                         */
                    } else if (!date3.equals(today) || date3 != today) {
                        holder.left.setImageResource(R.drawable.new_icon);
                    }

                } 
            } else {
                holder.left.setImageResource(android.R.color.transparent);
            }

        } else if (key3 == 3) {
        //similar 

由于您正在使用viewholder,因此当视图再次返回屏幕时,视图将再次被重用。如果你在第一个位置设置一个视图可见它会在其他位置可见除非你显式地设置它不可见。答案应该是这样的

AnimationDrawable frameAnimation = (AnimationDrawable) holder.left.getBackground();
if (position == 0) {
    if (newIconParam.get(0).equals("OK")|| newIconParam.get(0) == "OK") {
        dateonly = loto_date.get(0).trim().toString();
        dateonly2 = dateonly.replaceAll("\(.*?\) ?", "");
        String date3 = dateonly2.trim(); // use this to check
        if (date3.equals(today)) {
            //If the current date matches ,then show the blinking icon in the first position            
            logicflag = true;
            holder.left.setBackgroundResource(R.drawable.blinker);
            frameAnimation.start();
    }
    else if (!date3.equals(today) || date3 != today) {
        // want to show only the normal icon in the first position when the current date doesn't match
        holder.left.setImageResource(R.drawable.new_icon);
    }
}
else {
    //dont show icons in other parts of listview
    holder.left.setImageResource(android.R.color.transparent);
    frameAnimation.stop();
}

最新更新