Multiple non-scrollable lists in a ScrollView in Android - android

I am trying to put multiple lists that don't need to be scrollable inside a ScrollView. In other words, I have a TextView as a header for a category, with a list under it. Then another TextView as another header, with another list under that. And so on. This is in a vertical LinearLayout.
I want to be able to scroll through this layout as it gets off the screen when it has too much info, but I want all the contents of the lists to be displayed.
I've tried putting the headers (the TextViews) and the lists inside a vertical LinearLayout, and that LinearLayout inside a ScrollView. The problem with that approach is - all the lists have a max vertical dimension of exactly one element (if I set them to "wrap content"), despite the lists having multiple elements. Believe it or not, the lists do scroll (and they're inside of a ScrollView, more exactly, inside of a ScrollView -> LinearLayout).
If I remove the ScrollView, then the lists do indeed appear with all their elements, but I can't scroll the view if it goes off the screen.
So, if I use a ScrollView, I only get one element per each list. If I don't use it, I get all the elements of the lists correctly, but it doesn't scroll.
Any ideas of solving this? Thanks!

Nevermind, eventually found the answer here and it works perfectly:
https://stackoverflow.com/a/27818748/5627584
This ^^^ is a very underrated answer as I have hardly found it, but it's very, very useful when trying to include lists inside of superior layout node that has to be scrollable (inside of a ScrollView -> LinearLayout that contains other interface elements).

Related

How to make the entire screen containting a (LinearLayout + ListView) scrollable

I have an Android fragment in which contains a Vertical LinearLayout with two layouts inside it: Another inner Vertical LinearLayout (which takes at least half of the screen) and a ListView (which shows a number of smaller Views). A good way to think about it is something like a facebook post along with several comments showing below it.
The problem I am facing is that the the inner LinearLayout is not scrollable and therefore parts of the ListView stays outside of the screen and there is no way to scroll the screen up to see the entire ListView (the inner layout could possibly take up the entire screen). Placing the entire layout inside a ScrollView is also known to be a bad practice.
I know one solution is to make the entire layout to be a ListView with the first cell to be the inner LinearLayout, followed by the cells from the original ListView. That however has its own issues: It is harder to recycle the views in the ListView, and generally I also see it as a bad practice where one cell has a different behavior from all other cells in the ListView.
Is there a better solution to this? I can imagine this is a typical problem and I hope to find a better solution than above.
This may help you:
ListView yourListView = new ListView(this);
yourListView.addHeader(getHeaderView());
You can also add footer to a ListView. When you do this, the header(or footer) will become part of the ListView, and so it will be scrollable. You can find the document here: http://developer.android.com/reference/android/widget/ListView.html.

Need suggestions for dynamically creating views in android

I am going to start one app where my activity page will contain "n" grouped views. Grouped view means "collections of views (i.e. One group can have TextView+Button+ImageView)". So the page will have "n" number of such grouped views.
I need suggestions like what would be the best practice to implement this. I could think of below ones:
1) Should a ScrollView be used (Then I will have to create groups in runtime and place one under another)?
2) Or a ListView be used (Then how can I accommodate the height of each row as grouped views height may differ from each other?)
Or is there any other way I can go along with?
Appreciate the suggestions and any sample examples if have. Advance Thanks.
Both options would work, it really depends on your use case.
Place a vertical LinearLayout inside of a ScrollView and add your grouped-views to the LinearLayout. I would recommend this if you have a relatively small number of such views (not necessarily a fixed number, but small enough that you wouldn't have to scroll many "pages" to see them all). Make sure the ScrollView has android:layout_height="match_parent" and the LinearLayout has android:layout_height="wrap_content".
If the number of grouped-views is not small, you could use a ListView and make an Adapter for it. This lets you take advantage of ListView's automatic view recycling when items get scrolled off screen.
For either case, make an XML file for just the grouped-views. In code, you would get a LayoutInflater object (usually by calling Activity.getLayoutInflater()) and call inflate(R.layout.your_grouped_views, null). If using the LinearLayout, you would add it in code with one of the LinearLayout.addView(..) methods; if using the ListView, your adapter would return the whole thing from getView(...).
create one xml layout containing the constant elements of your group view.
in you main xml layout which will be the contentView of your application, put a ScrollView and a single LinearLayout.
then in the program inflate as many views of your group view as you want.
For your answer i want to give you referance of this website, on this website you can learn create dynamic view in android...

Activity with two layout views, where one view has vertical scrollbar

I need to do an activity with two "parts". The first one, uses the top half of the screen, and there, I will show some fields (name, date,...). The second one, uses the bottom half of the same screen, and there, I will show a table, with three columns, and with some rows. The quantity of rows is undefined, because they come from a web service.
My doubt is how to do this second part, with a vertical scrollbar table, where the user will see the first half static, and could be able to roler the bottom half to see all rows of the table (like an iframe). I need to use table, because my cells need to be aligned.
I have already tried tablelayout, listview, gridview, and I can't find a way to show the vertical scrollbar.
You could use a ScrollView.
Note though that ScrollView supports one direct child so you may wrap everything up.
You can use something like the following:
MainLayout
TopHalfElements
ScrollView
Linear/relative/frame/whatever layout
Rows From Webservice etc.
You need to have a main layout inside the ScrollView as it only supports one direct child.

Linear layout with nested listview scrolling

I have a LinearLayout with a nested listview which looks like this:
<LinearLayout ... >
<LinearLayout>
</LinearLayout>
<ListView>
</ListView>
</LinearLayout>
The problem is that the listview owns the scrollbar (so only content in the listview is scrollable) but i actually want the parent LinearLayout to own the scrollbar (so making the entire content scrollable).
Wrapping parent ListView in a ScrollView hasn't been successful because the ScrollView doesn't recognize ListView height (which looks like is rendered at running time)
Thanks
Edit: SOLVED My perfect solution was using a MergeAdapter, as advised by Barak
You can use CommonWares MergeAdapter which allows you to define views and list adapters, pour them into the MergeAdapter and get a single list adapter out, containing everything you poured in, and it scrolls as one list.
A previous answer about MergeAdapter I gave with some instructions is here
You could replace the Listview with a Tableview instead, as long as the listview does not have too many items in it. You can still use a childview with the tableview in much the same way as the listview, you just won't be able to databind it in the same way as you can with a listview, and the items won't recycle either.
Since the listview is designed to contain more items than can be displayed the height will never exceed the screen size (best case) which is the intent of the control, though I suppose you could force it to be larger that seems generally like a bad idea for a lot of reasons.
I suspect what you should do is either create a custom listview adapter, and based on position in the list create the view you want, this would allow all items to scroll like you want.
Listview with different view types per row
This may or may not work depending on what exactly you are trying to do, otherwise you might just want to add views to a linear view inside a scroll view san's the listview.
It just depends on the use case (how many items in the list view, memory issues etc) and what the purpose of the other views are.

Is it possible & efficient to put a LinearView and ExpandableListView inside a ScrollView

I'm making a GUI with two different parts. The first part (at the top) is composed of some banners, several fixed buttons. So I think using LinearLayout is the most straightforward way to implement. The second part is composed of several similar items grouped together which can be implemented by using ExpandableListView, I think.
However the problem is that the content exceeds the screen size. So I intend to put two of them into a ScrollView. I checked several sources, it seems that putting "ExpandableListView" inside a ScroolView is NOT possible, or not efficent, so I'm afraid...
Would you help to confirm if this is possible? efficient ?
If no, would you give me some recommendations for this layout design?
I'm indeed looking forward to your supports.
Sincerely.
If you have a fixed header at the top of a list, use ListView's header view feature.
Putting ListViews in ScrollViews fundamentally makes no sense and here is why:
ListView has one purpose: to efficiently display unbounded data sets. Since these can be extremely large (tens of thousands of items and more) you do not want to create a View for each item up front. Instead, ListView asks its Adapter for Views only for the items that currently fit in the ListView's measured space on screen. When an item's View is scrolled out of sight, ListView disconnects that View and hands it back to the adapter to fill out with new data and reuse to show other items. (This is the convertView parameter to an Adapter's getView method.)
ScrollView also has one purpose: to take a single child view and give it "infinite" vertical space to fit within. The user can then scroll up and down to see the full content.
Now given this, how many item Views would a ListView create for a 100,000 item Adapter if it had infinite height available to fill? :)
By putting a ListView inside a ScrollView you defeat ListView's key purpose. The parent ScrollView will give the ListView effectively infinite height to work with, but ListView wants to have a bounded height so that it can provide a limited window into a large data set.
Well Expandable List View itself has scrollable property by placing it in scroll view is really undesirable.As the both scroll would contradict and smooth scrolling can't be obtained in that case..
If we have any data to be shown prior or later to list...
Best way is to use header and footer view to list...
I recommend you use header and footer in your case.

Categories

Resources