Using nested ListView or something else? - android

I have an layout like this:
I have almost 10 category to display, and each category have 10 books to display.
So it seems that I have to use the ListView for category with nested ListView for each book.
But I wonder if this will cause the measure problem? The inner book listivew does not need the scroll feature while the outer listview needs.
I have thought use the LinearLayout instead of ListView at all by adding each book and category dynamically , however I am not sure about the performance since the ListView can reuse some view, while LinearLayout cannot(or hard to).
Any suggestion?

Have you tried using ExpandableListView. Hope this helps..

You should not have problems, your inner ListView will not have limited height.
Consider using a ExpandableListView, the header will be the category and the childs the books. If you use this remember to override the method isChildSelectable to keep all the items expanded and remove the group indicator with setGroupIndicator(null).

Related

List(with out scroll) inside a RecyclerView row

I want a list inside a recycler view, I don't want nested scroll, what is the best approach for this?
What I am considering now is that dynamically add the inner list rows.
If that is the case, let the adapter hold the whole data for the inner list. The number of list rows in each row of the recycler view row is not fixed.
Please voice your opinions with pros and cons.
NB:- I dont have any code to post since I am working on this.
Inside List View you need to Disable scrolling.
i.e listview.setNestedScrollingEnable(false);

ListView with ArrayAdapter showing only 1 item for height wrap_content

I'm having a issue where my ListView is displaying only one row even though ArrayList containing data has 2 items in it.
For clarification, YES, I'm using ListView inside a ScrollView but to balance it out I'm using custom ExpandableHeightListView (retrieved from one of the answers on SO) which is useful for exactly this purpose. This ListView is not scrollable. It has fixed height.
Also, within the same ScrollView, couple of other ListView are working fine but this one ListView is not working properly.
Going further, when I logged the position variable value inside getView() method, it's always 0 for this ListView while for other ListView, it's increasing sequentially.
I've compared both Adapter's code and they are identical but still current ListView is not working.
I tried with overriding getView() method, but to no avail.
Any help appreciated.
EDIT
If I give fixed height to ListView like 250dp, both the rows are getting displayed but it shows only 1 row with height set to wrap_content
As I know that ListView within a scrollView does not worked properly.
Because both widget has scroll so they does not worked well.
So I recommend to delete the ScrollView
Thanks..
I know this is not what you want to hear, but seriously, whatever you are programming, consider displaying it on tabs or similar, such that you don't need the scrollview.
If you really must have so much data on one screen, consider using something like a LinearLayout with an adapter to display your data.
Listviews are not designed to work inside a scrollview because they are scrollable themselves. You can "hack" your way around it, but it's not nice. If you really need the features that listviews provide, consider splitting up your screens into tabs, displaying a listview or two in each tab.
This solution worked for me and also adjusts the layout automatically. On the top-level ListView adapter, inside the getView() method, you will create a view that references a layout with another ListView. Assuming that "inner" ListView is declared as "listView" with its adapter called "adapter", try using the code below. Also, ensure that the inner ListView layout is set to WRAP_CONTENT, so that even if the height is correct, it is not being overridden by the layout constraints.
listView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
listView.getLayoutParams().height = adapter.getCount() * {whatever your list item height is} OR do something else to add up all of the heights;
listView.requestLayout();
listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
);

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 custom color for each textview in listview that extends SimpleAdapter in Android?

I have a listview with custom rows and that extends SimpleAdapter.
Each row consist of two linear layouts : 1st having two textviews of which one is hidden in horizontal orientation, second having two textviews in horizontal orientation.
Now depending on the value in hidden textview , I want to setcolor for the remaining items for the row.
To put it as simple:
each listview item has some custom colors the value of which comes from the hidden field.
I have done this by overriding getview() for the simpleadapter and returning view for each, but this makes list very slow to render (and that I think is obvious as so much of work for each view before showing it).
Can I do this in some more efficient way ? like making views and then add up to list instead of using xml layout maybe one solution OR any other ? Any help ? Thanks.
If you use convertView in your adapter, I would not expect you to have any particular speed issues. Creating and garbage collecting rows is expensive -- setting some colors on a set of TextViews is not. So, make sure you are using the convertView parameter to recycle your rows.
Here is a free excerpt from one of my books that covers row recycling.

Should I use multiple ListViews?

I have a RelativeLayout with different elements. I was planning to have two ListViews on it, but I have noticed there are some problems with scrolling. Since each ListView only shows a maximum of 5 rows should I try to make some kind of custom adapter to merge those ListViews? Or is it better to replace the ListView with a LinearLayout/RelativeLayout and add the rows as I get them manually? (like the first answer in here: android listview display all available items without scroll with static header ).
Which should be the proper way on doing this? or is there another way? Also, each row will have an OnClickListener.
There's two solutions if you'd like to keep your list... list-y, without having to prerender all the row Views like the above solution suggests (which can be slow to render, eats RAM and doesn't scale nicely to more than a screen or two of Views, but is a fine quick solution for smaller lists, though I'd just use a bunch of Views in a LinearLayout in a ScrollView rather than a ListView in that case).
Write a custom ListAdapter, overriding getItemViewType, getViewTypeCount and GetView to inflate the proper kind of view and recycle appropriately for your two types of views. You'll also either need to override getItem to contain custom logic for figuring out which set of source data to look in and to map the data accordingly, or mush the data down into one list of Objects (if you're using an arrayadapter) and cast in the getView method (probably a bit slower than handling it in the getItem without casting).
Just use cwac-merge, a view-and-adapter wrapping adapter. You can put two ListAdapters into a MergeAdapter and set that as your single ListView's adapter.
I had problems with scrolling. I never figured out how to have the ListView share vertical space with a different View, and have a single scrollbar for them both.
I worked around it by having everything that needs to scroll on the layout a row in the ListView.
Adding views as rows to a LinearLayout may have problems scaling up, but I think you'll be OK if you only have 10 rows in total. On 1st gen Android devices it'll probably start to get sluggish around 20 items (depends on Layout complexity obviously). ListView scales up by only inflating views as they come on screen.
So in answer to your question either of the two alternatives you suggest will be OK, but the LinearLayout option will be the easiest to code.

Categories

Resources