1

I have a GridView with a custom view which basically look like buttons. The gridview starts off with no children, and everytime the user presses a button another custom view will be added.

There is some strange behaviour with this. I am drawing some things like text, lines etc on the custom view in onDraw and sometimes they are drawn at all. They are completely blank. The behaviour seems quite random in terms of which views show or don't show the drawn graphics.

I have a feeling it is to do with me setting the layoutparameters. I store the child views in an array once they are created, and in getView() I return the view relevant for the position parameter. So I only ever create buttons for each position once.

So I have two questions.

  1. What am I doing to cause this?

  2. Should I even be using a gridview for what I am doing?

The code for get view is:

public View getView(int position, View convertView, ViewGroup parent) 
    {
        GridButton button;
        GridView gridView = (GridView)parent;
        if(childBuittons.size() <= position) //if we need to create a new button
        {
                button = createButton(position);
                int nWidth = getButtonSize(gridView);
                GridView.LayoutParams params = new GridView.LayoutParams(nWidth, nWidth);
                button.setLayoutParams(params);
        }
        else //we already have a button that we created
        {
            button = buttons.get(position);
        }
        return button;
    }

Some more information: - The gridbutton class is just a class that extends View and overrides onDraw to draw some graphics such as text and lines - What im trying to achieve is a grid of squares that the user can add or remove (although they wont do this often) and then press the squares to perform certain functions - It is possible that there will be more squares than can fit in the screen

4

1 回答 1

0

我在做什么导致这个?

如果不提供与 GridButton 类相关的更多详细信息,没有人可以真正帮助您。最好知道您如何处理视图的测量以及您在该视图中究竟绘制了什么(以及如何)。

我感觉这与我设置布局参数有关。

我对此表示怀疑。为了确保您还可以在该自定义视图中绘制一个简单的颜色,并查看它是否出现在网格上。如果确实如此,LayoutParams则不是发生这种情况的原因。还要检查你得到什么值nWidth

创建子视图后,我将它们存储在一个数组中,并在 getView() 中返回与位置参数相关的视图。所以我只为每个位置创建一次按钮。

这不行。使用 a 的主要原因GridView是您可以使用它的适配器来避免必须预先创建所有网格视图(占用内存(如果没有崩溃)并减慢您的应用程序)。您应该考虑实施特定于GridView/的适当回收机制ListView。你的getView()方法应该是这样的:

View getView(int position, View convertView, ViewGroup parent) {
    GridButton button;
    GridView gridView = (GridView)parent;
    if(convertView == null) {
            button = createButton(position);
            int nWidth = getButtonSize(gridView);
            GridView.LayoutParams params = new GridView.LayoutParams(nWidth, nWidth);
            button.setLayoutParams(params);
    } else {
        button = (GridButton) convertView;
    }
    return button;
}

如果您需要访问这些按钮,请通过适配器访问它们,根据按钮的位置更改一些数据并调用notifyDataSetChanged().

我什至应该使用 gridview 来做我正在做的事情吗?

你没有说你想做什么。如果您认为单元格的数量足够多并且预计不会看到所有单元格,请使用GridView. 如果您认为所有单元格都是可见的,那么您可以使用不带GridView.

于 2013-02-24T15:33:12.893 回答