android网格视图上单元格的颜色在滚动时更改

时间:2014-04-28 17:27:24

标签: android gridview scroll

所以我有一个textViews网格,由228个单元格组成。如果用户点击网格上的任何单元格,单元格的颜色会发生变化。但是,只要我向上或向下滚动,颜色就会移动到另一个单元格。我使用了customgridview Adapter。这是getView()的代码。

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

   View gridView;
    if(convertView == null) {
        convertView = inflater.inflate(R.layout.fingerprintlayout, null);
    }



    TextView tv = (TextView)convertView.findViewById(R.id.textView);
    tv.setText(gridV[position]);  //Setting the text 

    if(position == getClickedCellPosition()){
     convertView.setBackgroundColor(Color.BLUE); //Setting the background color

    }
    return convertView;
}

1 个答案:

答案 0 :(得分:2)

它实际上是移动到不同的单元格值还是布局中的不同单元格? convertView == null代码用于重用视图(如果可用)。因此,您可以设置单元格1,1的颜色,但滚动时该视图可以重复使用2,1。

VERIFY的一个解决方案是问题是删除空检查并再次膨胀布局。请勿发布您的代码。这是低效的。

您可以做的是保留哪些单元格突出显示的缓存。调用getView时,如果未选择它所代表的单元格,则应将背景重置为默认值;如果选择了单元格,则应将其高亮显示。