简体   繁体   English

更改ListView项目背景错误

[英]Changing ListView Item background bug

When I'm using parent.getChildAt(position).setBackgroundColor(Color.GRAY); 当我使用parent.getChildAt(position).setBackgroundColor(Color.GRAY); in my public void onItemClick(AdapterView<?> parent, View view, int position, long id) it colorizes, but it works strange. 在我的public void onItemClick(AdapterView<?> parent, View view, int position, long id)它会着色,但是效果很奇怪。

When I click first or second item it colorizes it... and every item away ~five records. 当我单击第一个或第二个项目时,它会给它上色...然后每一个项目都会消失5条记录。 Sometimes I've got NullPointerException. 有时我遇到了NullPointerException。 Completely weird, because position is unique and it should recieve me appropriate View, but it doesn't. 完全奇怪,因为位置是唯一的,它应该为我提供适当的视图,但事实并非如此。

I saw solution with overriding getView method, but I'm using this adapter in different places. 我看到了覆盖getView方法的解决方案,但是我在不同的地方使用了此适配器。 I just want to color clicked item. 我只想给点击的项目涂上颜色。 How to get reference to selected view? 如何获得对选定视图的引用?

EDIT: 编辑:

In my adapter class I created: 在我的适配器类中,我创建了:

public static int selectedItem = -1;

I added this to my overrided getView method: 我将此添加到了重写的getView方法中:

    if(selectedItem == position){
        parent.getChildAt(position).setBackgroundColor(Color.GRAY);
    }

In my activity I added that: 在我的活动中,我补充说:

myAdapter.selectedItem = position;
myAdapter.notifyDataSetChanged();

And It doesn't work. 而且它不起作用。 Where I do a mistake? 我在哪里弄错了?

It's not a bug - it's the way ListView re-uses the views to save resources. 不是错误 ,而是ListView重用视图以节省资源的方式。

So to avoid this behavior you should on every getView() set all used attributes for all your views. 因此,为避免这种行为,您应该在每个getView()上为所有视图设置所有使用的属性。

Updated - to be quite clear 更新 -非常清楚
In your particular case it means that you should set color like this: 在您的特定情况下,这意味着您应该像这样设置颜色:
1) In onItemClick() - in your actitivity - you should remember given position as selected: 1)在onItemClick()中-在您的活动性中-您应该记住选定的给定位置:

myAdapter.selectedItem = position

2) In getView() - in your adapter: 2)在getView()中-在您的适配器中:

if(selectedItem == position)
    parent.getChildAt(position).setBackgroundColor(Color.GRAY);
else
    parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color

Update 2 更新2
If you want to select many items you should use some structure (like HashSet) to hold all the selected items: 如果要选择许多项目,则应使用某种结构(例如HashSet)来保存所有选定的项目:
1) In your activity class add member: 1)在您的活动课程中添加成员:

public static HashSet<Integer> mSelectedItems = new HashSet<Integer>();

2) In onItemClick() use following to flip selected state: 2)在onItemClick()中,使用以下命令翻转所选状态:

if(mSelectedItems.contains(position))
   mSelectedItems.remove(position);
else 
   mSelectedItems.add(position);

3) In getView(): 3)在getView()中:

   if(MainActivity.mSelectedItems.contains(position))
        parent.getChildAt(position).setBackgroundColor(Color.GRAY);
    else
        parent.getChildAt(position).setBackgroundColor(0);//or whatever defauld color

At first read this article; 首先阅读这篇文章;

Then use ViewHolder pattern; 然后使用ViewHolder模式;

And try to setBackgroundColor() in onItemClick() like this: 并尝试像这样在onItemClick()设置setBackgroundColor()

    view.setBackgroundColor(0);//or whatever defauld color

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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