列表视图的古怪行为(Android版) [英] Erratic behaviour of listview (Android)

查看:128
本文介绍了列表视图的古怪行为(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ListView我有一个自定义适配器,我所要建造一个使用TreeMap的,我定义的getView方法,该方法如下。我想在点击列表视图的certian TextView的罢工出来的文字,只有那些textviews将striken掉其中包含的字符有一定的规律(Y @ Y)。然而在点击列表视图一排我正在重拳出击一些其他行的效果。

In my listview I have a custom Adapter, which I build using a TreeMap, I defined the getView method which is as follows. I am trying to strike out the text in a certian textview of the listview on click, only those textviews will be striken off which contain a certain pattern of characters (y@y). However on clicking one row in the listview I am getting strike out effect on some other row.

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



        if (convertView == null) { 
            convertView = inflater.inflate(R.layout.chklistlayout, parent, false); 
        }
        TextView textView = ((TextView) convertView.findViewById(R.id.textView1));
        TextView imageview = ((TextView) convertView.findViewById(R.id.textView2));
        textView.setText(values[position]);
        imageview.setText(mValues[position]);


        String s = mValues[position];
        if (s.contains("y@y")) {
            System.out.println("In if of getview");
            System.out.println(s);
            imageview.setPaintFlags(imageview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        } else {

        }

        return convertView;
    }
}

我试图用一个支架模式也使用静态holder类,但这个问题似乎仍然存在。任何指针?

I tried using a holder pattern too, using a static holder class, but the problem seems to persist. Any pointers?

推荐答案

这答案是穆罕默德巴巴尔矿的一半,另一半,我们都对这个问题作出评论一起相当不错的答案:

this answer is half mine and half from Muhammad Babar that we both commented on the question making together a quite nice answer:

使用else块来处理这个问题,imageview.setPaintFlags()不罢工

use else block to handle this, imageview.setPaintFlags() not to strike

这是发生

由于convertView的。当您滚动列表相同的看法
  那是以前使用的回馈getView方法。所以同样
  你之前删除线画的ImageView仍然绘
  用它。所以,你必须恢复该行动。

Because of the convertView. When you scroll the list the same view that was used before is give back to getView method. So the same imageView that you painted with StrikeThrough before is still painted with it. So you have to revert that action.

更多了,因为这个循环,通常处理适配器必须随时撤消/还原/重绘/更改文本/改变图像的所有元素,以确保它会在屏幕上你想要的方式。

more over, because of this recycling, usually dealing with Adapters you must ALWAYS undo/revert/repaint/change text/change image to all the elements to make sure it will be on screen the way you want.

PS:现在你应该申请一个持有人的模式,以获得更好的性能。

ps.: now you should apply a Holder pattern to get better performance.

这篇关于列表视图的古怪行为(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆