Sunday, September 18, 2011

Re: [android-developers] Listview: heterogenous rows: if statements in XML layout file?


Mark Murphy wrote:

What Romain suggested was:

-- Override getViewTypeCount() to return the number of distinct types
of rows (e.g., 2)

-- Override getItemViewType() to return a value from 0 to
getViewTypeCount()-1, indicating which type of row to use for a given
position

I see now what these BaseAdapter methods are meant to do. You can
define a certain number of row types each defined by a type integer.
These default to a type count of 1 with a view type of 0. But I do not
understand where the view type gets passed in since getView only
receives a row position. Eventually the data will specify which row
type to use, so the row position should be enough to identify the
row type should it not?
 

-- In your getView() (or newView(), if you are extending
CursorAdapter), inflate the appropriate row layout for the position's
type,

So here we have getView from class Adapter. The position which is
the row number should be enough to inflate the desired row layout...
so in this case the two bulleted methods above are not needed?
 
or recycle the supplied convertView -- by overriding the two
methods in the previous bullets, you ensure that convertView will be
of the desired layout

What do you mean by recycle here?

-- In your getView() (or bindView(), if you are extending
CursorAdapter), bind your model data into the right widgets based on
the row type

But getView does not receive a row type, only position, convertView, and parent...
 

See also:

http://stackoverflow.com/questions/5300962/getviewtypecount-and-getitemviewtype-methods-of-arrayadapter
http://stackoverflow.com/questions/1660417/android-efficientadapter-with-two-different-views
http://stackoverflow.com/questions/3144555/different-views-on-a-single-list-android

The last one of these links has exactly my solution of using an if statement with a different inflator
in each case, which should do the same thing. See the pic, odd rows contain less data than even rows:

bubbles2.png
 

and probably another 50 or so answers on StackOverflow, where
getViewTypeCount() and getItemViewType() appear.

I will take another look...

Thanks,

John Goche

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment