【问题标题】:Bugs in CustomListView when using blinking imageView in android在 android 中使用闪烁的 imageView 时 CustomListView 中的错误
【发布时间】:2015-06-05 02:43:22
【问题描述】:

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

当显示正常的“new!”时图标在第一个位置 (0),一切都很好,因为它只显示在第一个位置。但是,当使用闪烁的图标时,它会在向下滚动时显示在列表视图的随机位置。
由于没有错误,我发现这个问题很难解决。
我提供了我自己的示例代码'在下面使用。

  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 

【问题讨论】:

    标签: java android android-listview custom-lists


    【解决方案1】:

    由于您使用的是 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();
    }
    

    【讨论】:

    • 谢谢,但我想我是在上面发布的代码中这样做的。您认为我的代码中有一些错误吗?
    • 只有当位置为 0 时你才这样做。如果它是另一个位置,你什么都不做
    • 我去看看看看结果!
    • 谢谢。我试过你的代码,但遗憾的是我失败了。我已经更改了上面的代码,以便人们可以看到代码的整体。
    • 行为有什么变化吗?可能只是尝试设置它的可见性消失
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2017-07-24
    • 2018-02-20
    • 2016-07-03
    相关资源
    最近更新 更多