1

My app needs a display with repeating pattern. Think a chess board where pieces can move within a square but not from one square to the next. Each cell is independent from the others. Each cell has a fixed size. Larger displays accommodate more cells. A typical screen might have one or two hundred of these cells.

Each cell can be an independent Java object, so my initial idea it to make a View subclass for the cell and pack them into a ViewGroup. On the other hand, the cells don't really play the Android layout game in the sense that they have a fixed size in pixels and cannot grow or shrink their size to accommodate different layouts. From this PoV, it makes sense to make one big View that can draw all of these cells. The CellArray view can then grow and shrink as the layout engine requests.

Which approach seems more natural?

iter
  • 4,171
  • 8
  • 35
  • 59

1 Answers1

3

Use a CellArray. Not because it's "more natural" (I'm not sure what that means) but for technical reasons. Views in Android are pretty heavy-weight objects and having a couple of hundred views in your activity will not be easy for the system to handle. (The Android lint rule for TooManyViews will complain if it detects that your view count for a layout exceeds ANDROID_LINT_MAX_VIEW_COUNT, which defaults to 80. You can ignore lint warnings, but they usually are there for good reasons.)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Does it matter if only a small number of cells need to redraw themselves on every update? Once big `CellArray` would need to redraw the whole screen every time. – iter Sep 25 '14 at 23:15
  • 1
    @iter - When you need to draw only a portion of the overall view, you can invalidate a particular rectangle and then in `onDraw` call `getClipBounds` to find what part or parts need to actually be drawn. See [this thread](http://stackoverflow.com/questions/2949036/android-how-to-get-a-custom-view-to-redraw-partially) for an example and some other ideas. – Ted Hopp Sep 28 '14 at 00:05