ListView and dynamic row sizes - android

I'd like to use different row descriptors (from the XML layout resource) in one ListView. Can I and how?
Also, I'd like to be able to programmatically change the size of the rows, based on the data in each row, in a ListView. Can I and how?
Thank you in advance--I LOVE stackoverflow.
M

I'd like to use different row
descriptors (from the XML layout
resource) in one ListView. Can I and
how?
Step #1: Override your Adapter class and implement newView()/bindView() (or getView() if ArrayAdapter)
Step #2: Inflate the rows you want when you want them from the layouts you want
Step #3: Override getViewTypeCount() and getItemViewType(), returning appropriate values, so Android knows to use different object pools for each distinct type of row
This book excerpt covers #1 and #2, though for only one type of row. I don't have any samples handy for multiple types of rows, sorry.
Also, I'd like to be able to
programmatically change the size of
the rows, based on the data in each
row, in a ListView. Can I and how?
Put in bigger stuff in the row. Heights of rows are determined by their contents.

Related

ListView Adapter with multiple Item layouts

I'm looking at adding a listview to my Android application with a thumbnail image. The thumbnail on the left and text on the right.
Is there any way of populating the list view with seperate xml files, for example, could I have separate xmls for Austria, UK, USA etc and add them to the listview?
The reason I ask is, I plan to reuse the individual countries multiple times, as in, shown in an A to Z but then again by Continent etc.
If this is possible, could I get an example of how this is done or a link with a tutorial etc?
Thanks
Do you really want separate .xml files? Or do you want to reuse one .xml file and just replace its content?
If you want separate .xml files (meaning multiple different Views in your Adapter)
take a look at the Adapters methods:
getViewTypeCount() - returns how many different view layouts you have
getItemViewType(int pos) - returns correct view type id by position
With these methods you can specify the number of different types in your adapter, and load different layout files depending on the type.
You could for example have an Adapter like this supporting multiple layouts. (e.g. one item being an actual item, the other one being a separator between the items)
For more specific information, please take a look at this great tutorial:
http://android.amberfog.com/?p=296

Android - listview contains different header and different layouts

I need to implement listview with multiple headers and list item under each header are different.
For example the first header name is weather, under this header, each list item has city name and current temprature.
The second header name is contact, and the each item under this header contains person name, contact number, call icon, message icon etc.
Can anybody know how to implement this in android?
Thanks
Mindus
In order to achieve Multiple header and different layout in a ListView you should use Section ListView
Section is like Header
And you can inflate different layout. Smiler example is given here Link
For Complete Source code go through the below link.
http://amitandroid.blogspot.in/2013/05/android-section-listview.html
As far as I know ListView can have multiple headers, but you can't locate them at the postion you want.The all will be located at the top http://developer.android.com/reference/android/widget/HeaderViewListAdapter.html
I advice you to use separators.Also this can help you https://github.com/commonsguy/cwac-merge
With this MergeAdapter you can insert adapters and views in whatever order you want and then present them all as a single adapter to a listview and consequently you can achieve multiple headers simulataion.
you will have to make different layouts for each type of cell (separate for weather and separate for contacts) , you will have to override getView method of list view as well and upon need, just set your desired view for cell to update it.
Thanks guys, Finally i got it with two different listview with separate linearlayouts. And use textview as a header in each linear layout.
I would suggest not using a straight linear layout for entire sections.
For headers and lists, I would recommend a MergedAdapter, https://github.com/commonsguy/cwac-merge, either that or roll your own.
For multiple cell layout types, this is support in the listviewadapter with the following methods from BaseAdapter
getItemViewType(int position)
getViewTypeCount()
This allows you to specify how many different types of layout are in use and then recycle the layouts appropriately.

Android dynamically calculate number of columns

I have a dynamically created list of items which should be displayed in either two or one column depending on the space the containing text needs e.g. if the text is long it will only be one item per row, otherwise two. Obviously they should all be the same size (half the screen size).
As far as I know there's no standard Android view with Adapter that does that. With a GridView you can have multiple columns, but not some rows one column and others two. A TableLayout could stretch views, but also here you have to know how many columns you need per row. Plus it doesn't have an Adapter.
So what I want to know a) is there any control that I'm missing that supports something like this or b) what would be the easiest solution for this problem?
EDIT: the items also contain a CheckBox and I need to keep track of the checked state so I can't just put two items in one view using an Adapter.
I'd say that this is a hard problem to solve using standard components, due to the problem of mapping data to items to rows.
If you for example use a Cursor with x rows to feed the adapter with data, then the total item count as seen from the Adapter is also x. However, since you're conditionally mapping two items to the same row, it means that a ListView will see y rows in the Adapter, where y <= x. But you cannot easily tell from the beginning what y will be. Furthermore, if the ListView asks the Adapter for item i where 0 <= i < y, there would be no (easy) way for the Adapter to determine which elements from the Cursor that i would map to.
That being said, a viable solution would be to subclass AdapterView or ListView and implement the layout of the elements yourself. As you're getting each item from the Adapter, you'll measure and layout it, depending on the sizes of the surrounding adapter items.
A different solution that could work for you if you don't have a large number of elements is to use two custom Adapters, one called ItemAdapter and one called RowAdapter. The ItemAdapter will inflate the actual items based on (the presumed) Cursor. The RowAdapter will use the ItemAdapter to get the items and merge them into rows. The ListView will in turn use the RowAdapter. The issue is that to know how many rows the RowAdapter will produce, it is necessary to measure all the items from ItemAdapter before the RowAdapter is connected to the ListView.
As far as I know there is no ready solution for your problem. I haven't yet tried something like this, but I would use a LinearLayout as the list item. Then just create your own Deflater (e.g. CursorAdapter) that deflates the Layout and checks the length. If needed you should be able to add a new View (e.g. TextView) to the LinearLayout.

Image in a custom List Android

I have created a custom adapter to display a list, there is an image that is displayed in each row ( the image is the same for all rows, except using an array i am assigning it different values depending on the position). An xml file defines the relative layout that i am using. My problem is that i can either get the entire row to be clickable or nothing at all, I only want this image to be clickable, instead of the entire row. How would i be able to do this ? i am new to android and am pretty much following different tutorials trying to create my list. Any help would be appreciated.
layout is like this :
TEXT:
[Image]
TEXT:
thats wat a row looks like...getting two texts from two different arrays and shows it, a third array is used to link to the image. I just want this image to be clickable instead of the entire row.
Thanks
Android's list component manages clicks per row. This makes it very difficult to achieve what you want to do. Two solutions come into mind:
1) If your list is never very long you could simply use linear layout and scroll view to build the list. This approach won't work if you fill in the list dynamically and you can't be sure that there won't be a very large number of rows as it would use too much memory in that case.
2) Other option is to use ListView but make your text components and images different view types in list ie. break you row into three.
That can be achieved overriding list adapter's getItemViewType(int)
http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)
In this approach you can make the image rows clickable but the text rows not.

How to generate a ListView with headers above some sections?

I want to generate a ListView that has some dividers between some of the entries, like it can be seen in some of the property sections. See the example below. I try to generate a List that consists of some textviews followed by one of the fancy dividers explaining the next part of the list and then again some text views. How can this be done? I thought about creating different views to add to the list? Is this the way to go?
I got a solution. I don't know if it is the best one.
I use a custom adapter derived from ArrayAdapter for the list as described in this tutorial. In the adapter class I check if the position in the getView method is a normal row, then I inflate the row layout. If it is the first row from a new group I inflate a headline layout that is a normal row plus the group headline above it.
If you don't want to mix the header into one of your rows. Consider the following solution:
You can overwrite the two methods getItemViewType and getViewTypeCount.
You now have a list that can display different rows. You need to check the expected view type for the item in the getView Method and inflate different layouts depending on it.
The list will handle the recycling for you in a way that it will return only correct recycle views to your getView method, this means if the recycleView is not null it can be used to display your current cell.
You can use my SectionedAdapter, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use my MergeAdapter, if you need something more flexible and with a less-limiting license (Apache 2).
I think you might be looking for android.widget.ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
I'm also interested in an answer to this. There must be a more straightforward way to do this.
In looking at the Adapter, there's a method, Adapter.getItemViewType(int position).
ListView defines a return value, ITEM_VIEW_TYPE_HEADER_OR_FOOTER which indicates if the returned item is a header or footer.
I haven't tried it, but I assume if you create your own Adapter and return an item with the type indicating it is a header or footer, that the ListView will display it appropriately.

Categories

Resources