获取GridView的不同单元格

时间:2014-01-24 14:54:29

标签: android gridview background cell

我正在尝试更改GridView的背景颜色。这与以下代码完美配合:

GridView gv = (GridView) findViewById(R.id.gvSpeelveld);
gv.setBackgroundColor(Color.RED);

但现在我想改变不同细胞的颜色。例如:第2行单元格2应为蓝色。 我应该使用什么方法将GridView项目放在特定位置以更改颜色?

我试过这些方法,但效果不好

//Attempt 1
gv.getChildAt(1).setBackgroundColor(Color.BLUE);
//Attempt 2 (returns data, not the whole object)
gv.getItemAtPosition(5);

那我怎么能不同细胞的内容呢?

1 个答案:

答案 0 :(得分:1)

在执行适配器后设置特定列表/网格项的背景并不是一个好习惯。最好的方法是通过识别position.e.g。

在适配器中设置它
  class YourAdapter extends BaseAdapter<T>{ ....

 getView(int position, View convertView, ViewGroup parent){



 View v = convertView;
       ......



      if(position == your_postion){



                  v.setbackground(Color.parseColor("#FF0000");
            }

        }

     }
相关问题