列表视图重复动作 [英] Listview duplicate action

查看:174
本文介绍了列表视图重复动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好计算器

这一次,我用含TextViews一个ListView战斗。

This time I'm fighting with a ListView Containing TextViews.

我添加OnItemClick监听器。

I add an OnItemClick Listener.

    v.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            TextView tvItm = (TextView) arg1;
            int Col = tvItm.getTextColors().getDefaultColor();
            if (Col == Color.WHITE)
                tvItm.setTextColor(Color.GREEN);
            else
                tvItm.setTextColor(Color.WHITE);
        }
    });

正如你可以看到我切换文本的颜色,它的作品..但是,它同时适用于多个项目,即使我只点击一个项目。所以,当我单击第一个项目,它变绿,则有六白的项目,第7项是绿色的,但我从来没有点击的第7项!!

As you can see I toggle the color of the text, and it works.. BUT, it works on several items at a time, even though i only click one item. So when I click the first item, it turns green, then there's six white items, and the 7th item is green, but i never clicked the 7th item!!

ITEM1 - 点击 - 绿色

ITEM2 - 不点击 - 白

项目3 - 不点击 - 白

ITEM4 - 不点击 - 白

ITEM5 - 不点击 - 白

ITEM6 - 不点击 - 白

item7 - 不点击 - 绿色

item8 - 不点击 - 白

item9 - 不点击 - 白

等等...

item1 - clicked - green
item2 - not clicked - white
item3 - not clicked - white
item4 - not clicked - white
item5 - not clicked - white
item6 - not clicked - white
item7 - not clicked - green
item8 - not clicked - white
item9 - not clicked - white
etc...

和该模式将继续为所有名单。

And that pattern continues for all of the list.

此外,如果我轻弹/移动列表上下快,图案shitfs上下1至2项。

Additionally, if I flick/move the list up and down fast, the pattern shitfs up or down with 1 to 2 items.

下面是截图:

Here's a screenshot:

第一个图片:什么都不做

二图片:我点击'AK Kusine

第三图片:我向下滚动,艾伦·马尔卡也发生了变化。

First image: Nothing is done
Second image: I clicked 'AK Kusine'
Third image: I scroll down, and 'Allan Malka' is also changed..

如果我在监听器设置一个断点只每点击一次停止在名单上。
是什么原因导致这种行为?而更重要的是,我怎么能解决这个问题?

If I set a breakpoint in the Listener it only stops once per click on the list. What causes this behaviour? And more important, how can I fix it?

推荐答案

这是因为 getView 方法回收您的意见,以优化性能。
实现您的适配器 getView(INT位置,查看convertView,一个ViewGroup父)办法(所以你需要扩展一个适配器)的方式,它得到了 convertView ,使得它根据位置的绿色或白色。

This is because getView method recycle your views to optimize the performance. Implement the getView(int position, View convertView, ViewGroup parent)method in your adapter(so you need to extend an Adapter) in a way that it gets the convertView and makes it green or white according to the position..

这获取详细的解释。它可能看起来长,但它是非常有用的。

Watch this for detailed explanation. It may seem long but it's very useful.

这篇关于列表视图重复动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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