Better way for header in ListView - android

I want to show a fairly complex header above an ExpandableListView. The header basically holds some artwork and some text. When I scroll the List I want the header to scroll off the screen.
I can do it by adding a section to the list view with no children but it makes my BaseExpandableListAdapter very clunky. does anyone know of a better way. I've tried adding the header in the xml before the ExpandableListView tags but that makes it static at the top of the page when I scroll the ExpandableListView.

if you do not like native View header http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View) you can inflate special view and return it in the first position in your Adapter.
Actually we make adapter to control getting view in our ListView. So you can always return special views in special positions and for me that is ok. Not always view in your ListView must be similar. You just need to implement some populating logic for all this views.
Good luck!

Related

How to inflate a recycleview inside an adapter?

So basically here is the setup I want and this is what I have. I currently have 2 adapters merged into 1 mergeAdapter to get the functionality of stories being the "header" of the app. Now the problem is they are all together inside a recycleview that's vertical. I need the first adapter to be inside the mergeAdapter but have a horizontal layout. I've asked around and it seems that I have to inflate the recycle view inside my header adapter and then use that as the layout, but I have no clear idea how to achieve this.
This is the response that I got:
"Technically it really is in theory "as simple as" inflating a layout that contains a RecyclerView as an item, and setting it up as an item view type. and getting the list to show into that item.
You really just need to create 2 adapters (if you go the MergeAdapter route), where one displays 1 item of a horizontal recycler view, and the other displays n items of the vertical list. Then put the two adapters in a MergeAdapter, now you don't even need to worry about item view types. That's what's thrown people off for almost a decade."
Edit:
To clarify, I have no idea how can adapters inflate anything other than view holders. And how can I inflate a recycle view and then use attach views to it, where do I even create it, how do I hold a reference to it in the adapter? I understand the concept behind it but I couldn't' find any examples that utilize this online so I've had no luck. Basically, if anyone can just describe the thought process behind it but a little bit more concrete, I cannot wrap my head around it atm.
Eg. You'd inflate the recycle view with inflater.inflate(R.id.recycleview) inside the init of the adapter and then store it in a variable to use in bindviewholder...

Multiple HeaderView for ListView

I wanted to know that is there any way to have multiple HeaderViews for a ListView or on the other hand i want to implement a ListView with three static rows at top and other rows with an adapter which will loaded from AsyncTask,according to ListView recycling i can't make three first row static by using position in getView() function of my adapter body. is there any suggestion to this issue?
If your data is static and always visible, then just put it outside of your listview, above it. And you should have the effect you need.
I used this Library it was useful for implementing Multiple Header ListView, if the first three rows are static then you can add them statically first above the ListView then set the adapter for the ListView and in this case you don't need to use the library
Wrap three views inside a layout (For example a vertical LinearLayout), and add that layout as the list header.

How to list Views (or Widgets) in one scrollable screen

I have the following prototype:
What is the way to list views in a scroll-able way in a screen?
What I have already tried:
I tried to create a class and extends the LinearLayout class and dynamically adds my Views (the compound controls class is my view also extends LinearLayout) to the main LinearLayout. So I got a list of my views which is scroll-able.
I'm not sure what you mean, but looking at what you have tried, what's wrong with using a ListView? As the name implies, it should do exactly what you want:
What is the way to list views in a scroll-able way in a screen?
Here is a quote from the android documentation of a ListView
ListView is a view group that displays a list of scrollable items. The
list items are automatically inserted to the list using an Adapter
that pulls content from a source such as an array or database query
and converts each item result into a view that's placed into the list.
You should be able to add a ListView directly as a sub-view under your LinearLayout.
your questions seems like hard to understand.
did you mean how to set the whole page(activity) into a listview?
listviews are scrollable in default (of course, you can see it if the list is long enough and you can set the whole activity as a list).. you can customize it also by using templates (xml)
and extending ArrayAdapter of your type (let's say ArrayAdapter<UpcomingEvents>) and supply data from db...
is this what you mean?

How to use a ListView inside a ExpandableList?

I've been working with a custom ExpandableList (see example picture below) where each item always has one child. This child consists of three parts. Each part has a header (red bars) and below that an Empty item OR a list of items. The length of this list will vary.
The first way I tried to do this is by adding a ViewStub below the empty item, which I inflated with a custom view, which also contained a ViewStub at the end which I inflated in turn for the next item, thus adding items recursively to create this sort of list of items. Sadly this resulted in StackOverflowError's when the list became too long (With short lists this worked perfectly).
So on my second try I tried using a ListView with a custom adapter instead. Sadly this list only used a small part of my screen and the rest of the items where occluded behind the header of the next part (This mini list looked scrollable as a second scrollbar appeared next to it, but it did not scroll. Not that I would consider this scrolling inside a scrolling list to be a good solution, but just wanted to mention this).
Can anyone tell me how I can tell this list of items to not be scrollable and take up all the room that it needs (does it know what size it is going to be when the child node is created??) or help me with a alternative solution to my problem? (Ooh, and I have considered putting an unholy amount of ViewStubs inside my layout, but that just seems idiotic and really bad practice. Correct me if I'm wrong)
If I understood you correctly, then why don't you just take an ExpandableList + Adapter that implements ExpandableListAdapter? It's a somewhat ugly approach but it works and isn't much hassle.
MyAdapter implements ExpandableListAdapter
#Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
}
In this method you could simply figure out if the current childPosition would be one of the headers or one of the children and inflate the appropriate View from xml.

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