Widget Implementation Advice Needed
I'm new to Android and I want to write a widget that contains (about)100 columns, each column contains various amount of child views (will be determined at run time via some service call). Users can scroll horizontally and vertically (similar behavior as GridView
but with horizontal scrolling). The child views will be customized widgets like ImageButton
, which will be used to load images and data via service call when they are exposed on the screen.
I have implemented a 2D scroll view that enables users to scroll vertically and horizontally. I'm using a Horizontal Linear Layout as a container for the columns. However, I'm having trouble designing the columns. I've thought about implementing开发者_运维百科 in the following ways:
Use a Vertical Linear Layout as column and a subclass of
ImageButton
as child view. The challenging part is how to load data and image only when the button become visible and how to dereference them once they are scrolled off the screen. From what I know Android doesn't have a method to tell whether a widget is on/off screen (like theonExpose(
) method), so I'm not sure if it's possible to do this (easily).Use some sort
AdapterView
as column.ListView
is very close to what I'm trying to do but each list view handles its own scrolling and it's hard to have all columns to scroll vertically together. I'd really prefer to use Adapter if possible since it recycles the views, which is really desirable since I'll potentially have thousands of child views and I'm not sure if I'll run out of memory otherwise.
I'd really appreciate some advice from more experienced Android developers on how to implement what I described above.
I'm not sure if I'll run out of memory otherwise
It is all but guaranteed that you will run out of memory unless you use AdapterView
techniques. You only have 16MB of heap space on many phones, and each widget takes 1-3KB, plus however much memory the bitmap takes. Thousands of these = crash.
However, I suspect that neither of your approaches will work. I will be fairly stunned if you can get the behavior you want, without running out of memory, merely using off-the-shelf widgets as a base.
If I had to implement this (and the thought of that scares the heck out of me), I'd start by trying to grok every line of GridView
's implementation. As you say, what you want is a two-dimensional scrolling GridView
. Hence, understand GridView
, then figure out how to add horizontal scrolling. In theory, that could be a subclass of GridView
, though I doubt it. More likely, it will need to be a custom subclass of AdapterView
or possibly AbsListView
(GridView
's immediate parent).
To put the level of effort into perspective, GridView
is 1879 lines long (including comments and whitespace). AbsListView
-- in case you need to reimplement that for horizontal scrolling -- is 4200 lines long.
精彩评论