如何设置个别gridview的细胞的背景色 [英] How to set the background colour of individual gridview cells

查看:133
本文介绍了如何设置个别gridview的细胞的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些文本的每个单元一个GridView,我想可以设置单个单元格的背景颜色。

I have a GridView with each cell containing some text, and I want to be able to set the background colour of individual cells.

中的XML我的GridView的是:

The XML for my GridView is:

<GridView android:id="@+id/students_grid"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:numColumns="6"
          android:gravity="center"
          android:stretchMode="columnWidth">
</GridView>

在code我的GridView的是:

The code for my GridView is:

GridView gridView = (GridView) findViewById(R.id.students_grid);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, student_array);
gridView.setAdapter(adapter);

我希望我能设置单个细胞中使用的背景颜色:

I had hoped I would be able to set the background colour of individual cells using:

gridView.getChildAt(random_student).setBackgroundColor(Color.parseColor("#18A608"));

不过,这会引发空指针异常,并在进一步检查似乎gridview.getChildCount()返回0。我已经看到了gridview.getCount返回正确的gridview的项目数,但是这并没有帮助我来设置各个单元的背景色。

However, this throws a null pointer exception, and on further examination it seems that gridview.getChildCount() returns 0. I have seen that gridview.getCount returns the number of items in the gridview correctly, but this doesn't help me to set the background colour of individual cells.

任何想法,我走到哪里下?

Any ideas where I go next?

推荐答案

的关键,解决这个问题的方法是首先了解如何的ListView 的GridView 的工作。 的GridView 创建和破坏孩子的意见,你上下滚动。如果你不能看到一个的GridView 这意味着有没有为它子视图中的项目,它会在用户实际滚动到它被创建。 的GridView 使用了适配器以创建的意见和的GridView 再循环意见,当他们去屏幕外,并要求适配器重新使用回收的意见为来在屏幕上的新观点。该适配器通常膨胀的资源布局,以创造新的看法。

The key to solving this problem is to first understand how ListView and GridView work. GridView creates and destroys child views as you scroll up and down. If you can't see an item in a GridView that means there is no child view for it, it will be created when the user actually scrolls to it. GridView uses an Adapter to create the views and GridView recycles views when they go offscreen and asks the Adapter to reuse the recycled views for new views that come on screen. The Adapter usually inflates a resource layout to create new views.

所以,这是什么意思是,的GridView 将调用 getView(...)适配器每次都想在屏幕上显示一个子视图时,它可以通过一个循环查看名为 convertView

So what this means is that GridView will call getView(...) on the Adapter each time it wants to display a child view on screen, it may pass a recycled View called convertView.

解决的办法是重写 getView(...),叫超让适配器创建和填充从字符串阵列数据视图正常,但在最后加一点code给出的观点回的GridView ,设置视图的颜色。

The solution is to override getView(...), call super to let the Adapter create and populate the view with data from the String array normally but add a bit of code at the end before we give the view back to GridView that sets the color of the view.

new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, student_array) {
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);

    int color = 0x00FFFFFF; // Transparent
    if (someCondition) {
      color = 0xFF0000FF; // Opaque Blue
    }

    view.setBackgroundColor(color);

    return view;
  }
};

这篇关于如何设置个别gridview的细胞的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆