【问题标题】:Get different cells of GridView获取GridView的不同单元格
【发布时间】:2014-01-24 14:54:29
【问题描述】:

我正在尝试更改 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);

那么我怎样才能知道不同单元格的内容呢?

【问题讨论】:

  • 在 adpater getView 方法中执行此操作

标签: android gridview background cell


【解决方案1】:

在执行适配器后设置特定列表/网格项的背景不是一个好习惯。最好的方法是通过识别位置将其设置在适配器本身中。例如

  class YourAdapter extends BaseAdapter<T>{ ....

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



 View v = convertView;
       ......



      if(position == your_postion){



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

        }

     }

【讨论】:

  • 通过什么方式可以识别适配器中的位置?
  • 你可能已经使用了 gridview 的适配器。所以在那个适配器中你有方法 getView(int position, View convertView, ViewGroup parent)。通过这种方式你会得到位置
  • 我明白了,但是我应该在属性中填写什么? int Position 是合乎逻辑的,View convertView 看起来像 GridView 但 ViewGroup 父级应该给出什么?
  • 我明白了,但是我使用了默认的 ArrayAdapter 类 (ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrSpeelveld);) 这是一个问题还是我应该定义自己的自定义类吗?
  • No.. ArrayAdapter 不能满足您的要求。您必须使用自定义适配器。请看链接stackoverflow.com/questions/6958279/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
相关资源
最近更新 更多