Im trying to implement a GridView with section headers. I found one other question here Android GridView with categories? , however following the accepted answer gives me "header" as cells instead of the header being in its own row.
Im not sure what Im missing here (have tried the MergeAdapter too), can someone point out the nuance thats needed to implement the section headers in a GridView ?
This is not strictly possible. GridView does everything in terms of cells.
Now, you are welcome to create a set of cells that, when displayed together in a row, happen to look like a header. Presumably my MergeAdapter can handle that, though I have certainly never tried it. It will be up to you to ensure that:
the header cells are all in one row, which implies that...
empty cells go in any spaces in the preceding row, after the last "real" cell and before your header cells, and...
that you have the right number of header cells (i.e., equal to your number of columns), and...
that the header cells render correctly to look like a contiguous header, and...
that you use areAllItemsEnabled() and isEnabled() correctly, so that the header cells and the empty cells are not enabled
Try modifying the usual listview adapter to return grid cells see here
Related
I'm looking to create a music app and I'd like to make a GridView similar to what Google Play uses where they inject elements that will span rows and columns like the Soilwork album does in this screenshot:
I've thought about using a ListView and populating rows with custom elements, but I couldn't think of a good way to use that with ViewHolder pattern, or really a way to make that reusable and account for differing number of items in width based on screen size (for example, the grid is only two items wide on phones, and "large" items span both columns and only one row).
I've also thought about using a ScrollView filled with custom ViewGroups, but that seems to run into the same issues I previously mentioned.
I next thought about using GridLayout, but that doesn't accept ListAdapters, and doesn't seem tuned to the kind of usage I'm looking at (nor does it seem to scroll)
I'd greatly appreciate if anyone could give me somewhere to start on this, or could point me to a library that does this. I've already checked out StaggeredGridView but it doesn't seem to accomplish what I'd like. My ideal solution would be a view which lays out items on an even grid like GridView and accepts view from a ListAdapter, also like GridView, but allow for elements to span, using the single cell constraint of GridView as the default behaviour.
Cheers.
EDIT
I have a perfectly functioning GridView as shown below, but I'd like to make items at regular intervals (every nth item) span more than one column and/or row, as shown in the previous screenshot.
Check out Parchment. GridDefinitionView may help you achieve the UI you are looking to build.
I want a gridview with empty grid lines as follows
Here is what i got till now, gridview with gridlines in the occupied cells only. I want gridlines in empty cells also. Is this possible?
I'm almost positive a GridView was never intended for this, so you have two options.
-Write your own GridView that supports a defaulted view.
OR
-If this is not a dynamic view that is changing all the time in real time:
In your adapter, set a minimum in your getCount method (return Math.max(actualSize, minimumCount)). And set the views with no data to your empty boxes.
Make sure the count is always some mod of 4 to ensure each row will be filled beyond that.
That's just what I'm coming up with on the top of my head, there's most likely a better way to do it, but hopefully I'm moving you in the right direction.
Try putting an empy View within the empty cells of the grid.
For example, use a TextView with no text, or simply a new View().
I've just added a header to my ListView and I have to change a bunch of code because the header essentially becomes position 0 (Meaning the Cursor indices of my CursorAdapter do not line up with the indicies of the list. They are off by 1 now). Why? This seems a bit silly to me.
The only reason I can come up with is that a developer may want to access the header. Fine. Provide something like getListView().getHeader().
For some reason the position (from the onItemClick) is tied up with the number of items in the ListView (not the adapter), including headers and footers. When you set an OnItemClickListener you should retrieve the clicked item by calling listView.getItemAtPosition(position) instead of adapter.getItem(position).
In fact, you should always use the getItemAtPosition, because that way not matter if your ListView has headers and footers, and if you add new headers you won't need to change your code.
And if you don't want your header to be selectable, you should add it in this way: listView.addHeaderView(headerView, null, false).
I believe a ListView is nothing more than a list of View elements. If you add a header (or a footer for that matter, it shouldn't make any difference) to your ListView, that element is basically no different from any of the other elements that gets added automatically through the ListAdapter. The only minor difference being that the header (and footer) element will be fixed and stay unaffected by what the ListAdapter does with the list - they are still nothing but ordinary elements though.
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.
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.