简体   繁体   中英

Bugs in CustomListView when using blinking imageView in android

Currently, I'm making a simple app that uses customlistview that displays a "new!" icon in the first position of the listView and shows a blinking "new!!" icon when the current date matches the date from the json data.

When displaying the normal "new!" icon in the first postion (0), everything is fine as it shows only in the first position. However, when using the blinking icon, it displays in random positions of the listview when scrolled down.
Since there is no error, I find it difficult to solve this problem.
I've provided the sample code that I'm using below.

  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 

Since you are using a viewholder the view will be reused again when it comes back onscreen again. So if you set a view visible in position one it will be visible in different location unless you explicitly set it invisible. The answer would be something like this

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM