How to generate a ListView with headers above some sections? - android

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.

Related

Android: What’s the best way to implement the BaseAdapter if item content in ListView is dynamic?

In most case, we need to implement the BaseAdapter’s getItemViewType() and getViewTypeCount() for dynamic item content of the listview, as this post says. But I think this solution is only suitable for finite number and knowing beforehand, such as listview item with sending layout and receving layout.
What about the case that the listview with its item content impossible knowing beforehand?
For example, I need to show a contact list from server, the contact list size is about several thousand. For each item, I need to show, for example, the hobby “list”. It is a small range of 0 to tens of string. So in this case:
The item types is relatively bigger than normal case using “getItemViewType”
Though each item may be different, but similar to a certain degree: the item content is different in the number of views, but common in view type. Item A is different from item B only because it have more TextViews.
For each time in the getView, the convertview is hard to simply reuse because they are different, but if we create new TextView and added to the convertView, it will impact the scrolling of the listview. I don't think it's appropriate such way. What should i do in such case?
Unfortunately you cannot easily change the number of item view types on the fly. The getViewTypeCount() method is only invoked when the adapter is set on the ListView. That means, were you to dynamically change it, then you would have to call setAdapter() again. This is a huge performance hit as the ListView will toss out all the recycled views and re-generate everything from scratch again.
Honestly, it looks like you should be using an ExpandableListView instead. It allows you to displays lists of items under groups. The only difference is the groups are placed on top of the list. So where you have A, B, C, D on the left side in your picture...in the ExpandableListView it'll sit on top.
The ExpandableListView can easily handle your situation where a given grouping could have any variable number of items within it. You mentioned needing to store a contact list. I'd suggest taking a look at a RolodexArrayAdapter for use with the ExpandableListView. It may be of help.

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.

Implement a list with section divider in android

Hi how can i implement a list with section divider like the one on android's building blocks lists
Can you point any articles or tutorials to achieve this?
I implemented a possible version here:
http://steprobe.wordpress.com/2013/03/29/google-building-blocks-style-listview-for-android/
There are many tutorials to get grouped lists. The trick is to look for "adapter" and not "list". For instance, this one: http://android.cyrilmottier.com/?p=440
Each row in your list can be totally separated in terms of layout from others. So if you got 3 rows on your list, you can have each one can look totally different. You need to write your own adapter (i.e. extending ArrayAdapter), override getView(), getViewTypeCount() and getItemViewType(). Then for each row your getView() shall do any logic you want, inflate any layout you want and return that View to the list.

Android - ListView items maintaining style when scrolling

I know that similar questions have already been asked but I do not understand what would be the correct approach of solving the issue, yet.
I would like to change the background color of a ListView row when the user clicks it. However due to Android reusing the row layouts when scrolling, the background color gets repeated for other rows. I am wondering what would be the correct approach of maintaining the original layout for all rows except the one changed programmaticaly and also maintain the changed layout information for that row for scrolling back. I am using a SimpleAdapter which is passed the rows layout's XML.
Regards
Your rows' capabilities within your ListView largely depend on the kind of Adapter you are using. In any Adapter where you manually construct or inflate the View per item, you can change the layout properties per item, as long as you do so within the Adapter. Simply add your background color code to when the item's View is built, and it will work like a charm. If you are not able to do so with the current Adapter, consider extending the current one or using a different adapter.
Note: I haven't placed code directly within this answer because where you add it depends upon your own implementation. For instance, I would add .setBackgroundDrawable() to bindView() in an extended CursorAdapter.
Hope this helps,
FuzzicalLogic

is this a correct way to make sectioned list in android?

i have to make a sectioned list in my application. For this i am using the following approach:
make a listview to contain the headers of each sections
the xml inflated in the getview of each item in the above list consists of a textview and a tablelayout
the custom adapter used to make the views for the above listview, i fill the textview with the header and add rows into the tablelayout until all the section is filled.
Naturally i maintain two arrays: 1) for the headers 2) for the section details(actually for this i use a hashmap with the section header index in its array as the key, this is my of identifying which header belongs to which section).
for some reason the above code is not working and the data is being repeated in different sections...eg. the second section contains data of the 1st and the 2nd section combined?
why is this happening?
Doesn't the idea mentioned above seem correct?
what is going wrong over here?
thank you in advance.
For hierarchical data structures you should rather use ExpandableListView/ExpandableListActivity then ListView (see this example for reference).
This way you'll use a BaseExpandableListAdapter to populate your list, which has two methods for the renderers:
getGroupView should be overridden
for the headers,
getChildView should be overridden
for the child items of the different
headers
If you don't need your list to be collapsable/expandable, you set all of your groups expanded, and disable collapsing as described here.

Categories

Resources