【问题标题】:Get different items in GridView在 GridView 中获取不同的项目
【发布时间】:2014-01-27 03:53:25
【问题描述】:

我从 .txt 文件中读取值并使用 ArrayAdapter 将它们放入 GridView 中,它运行良好。 但我想根据项目的价值改变项目的颜色。

例如: 1 是灰色 2是红色 3是蓝色 4是...

但我无法更改 GridView 中单个项目的背景颜色,只能更改整个 GV。

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

此尝试不成功

//View is null
View v = gv.getChildAt(2);
v.setBackgroundColor(Color.CYAN);

但是我怎样才能获得单个 gridView 的不同项目呢? for 循环会非常有用。

【问题讨论】:

  • 您必须在 Adapter 的 getView() 方法中为单个项目设置背景...

标签: android gridview colors items


【解决方案1】:

在适配器的getView(int position, View view, ViewGroup parent) 方法中,获取给定位置的项目值(使用位置参数)。然后使用类似 switch-case 的东西并为该网格项设置背景颜色。

类似的东西。

//说你在getView方法中获取了item的值...

switch(value){

case 1:
   view.setBackgroundColor(Color.GRAY); //grey
   break;
case 2:
   view.setBackgroundColor(Color.RED); //red
   break;
}

//so on

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    相关资源
    最近更新 更多