Recycle views in scrollview in android - android

I have a scrollview in which I have inflated the view dynamically (same as facebook wall of android app, there is an imageview and textview in it.).
All the images are dynamic. If I load too much data in it then I get outofmemory exception. I know that listview will solve my problem but I cannot use the listview.
So, is there any way to optimize i.e recycle the views when they are not visible?

By your comments, I believe you still can use a list view:
A listview can have many different kinds of view. For more information on this, read Android ListView with different layouts for each row.
This way, views will be recycled and hopefully your memory usage will be smaller.
Anyway, I would probably investigate your code as I guess that for running out of memory, you probably are leaking objects or doing something way too many times than needed.

you can not handle Errors in normal circumstance but you can use following techneques to remove thias errror
1-Please clear your Arraylist before item add .
2-Use garbage collection techniques
System.gc() or finalize method use for Memory reclaim from object.

Related

Android ScrollView VS ListView show many dynamic items

There is object array received from server
there are two ways to show items :
Adapt Objects To ListView
Use Scroll View
So If I use scroll View and add items programmatically, There are some Questions:
Does Adding Items Programmatically Cause Out Of Memory ?
Does It Need To Remove Items from memory ? or it will remove automatic after onDestroy Called or Items moved out of screen?
Regards
If the data set is large, a ListView won't stop OOMs by itself, you will have to avoid loading the entire data set in memory by using a CusorAdapter backed by some non-memory data store (like a sql database).
A ListView will reduce the amount of memory that the Views use to represent the data to the user, since it will only initialise Views that the user can see (and recycle Views if possible when the user scrolls).
Since only the Views visible are loaded with a ListView, performance should* also be better since the layout process will be quicker.
In most cases, a ListView would be better for this type of thing.
(*) If the adapter's getView or bindView e.t.c. are complex, or each row in the list has its own view type, the scrolling performance could actually be worse.
Listview is the Best case for you. and I will prefer to use volly library of android . It is easy to use and very handy and helpfull.

TreeView component by LinearLayout - out of memory error

I make a treeview component by LinearLayout base. But when inner items count is very large android close it by out of memory error.
I used only 3 bitmap by 3kb size in each items.
How can I reduce memory using of my component (LinearLayout) I know ListView has scrollingCache but I use LinearLayout ?
Can anyone help?
Thank you.
The ListView use a system that initialize only the row(view) that are visible to the user. When a row in not visible anymore it doesn't get destroyed, but it is used to inflate the layout of the next view that will be visible. With this system you have to save in memory only for the object that are visible, even if there is 1000 of item in your list.
Here you can find a more accurate description of the recycle system
I think you should also take a look at this video. It's a lesson on ListView from Google I/O. I find out it is very useful to understand how ListView works .
In your case, if you cannot use ListView, i think you should implements manually this system. This is pretty hard so the best advice i can give you is to try to create a custom ListView that fit your needs and use it.
PS:The method recycle() is a bit different, it is used to delete any reference to the Bitmap on which you are using the method. This way, the next time the GC run, it will be able to delete from memory the Bitmap

Gallery widget with SpinnerAdapter has getView called once for each element on load

I have a regular Gallery widget. For some reason, the getView method in my adapter is getting called once for every single element in my list of items. Effectively creating all the layout elements many more times than necessary and leading to slower loading times as well as outofmemory exceptions.
Reading through several posts I understand that Gallery does not recycle, but I don't think that it should be creating every single item from the get go. Would appreciate any suggestions on how I can prevent all images from being loaded on load.
I had the same issue but they way I solved it was related to the component that I use for loading images. Anyway I think this post can be useful for finding a solution
So I actually ended up sort of solving this. Using the answer from this question the problem disappeared and the Gallery no longer pre-loads all of the pictures.
I still don't know the cause of the original problem, or the way to solve it there. But this works for me.

Android View Scrolling Performance Troubleshooting

I'm currently experiencing some scrolling issues in a few of my views. The performance on the Galaxy Tab (7 inch) is rather poor. Rather than trying to explain my view hierarchy here, I was wondering if anyone had any general guidance on how to troubleshoot scroll performance issues on Android. I'm interested in any approaches or gotchas that I should be aware of when building views, and how to find the trouble spots that's causing the laggy scrolling
I these few suggestions would help you,
1- Use an efficient ListAdapter (taking advantage of the ConvertView in getView() method) as such:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
2- If you are loading images over the net, thread your loader and set images to views asynchronously.
3- And if you're rendering images in your list items, keep a cache in the form of an HashMap; i.e. HashMap where integer would be the position and Drawable would be the materialized images. Use the Drawables availabe in the Cache in your getView() method and only load images if the Drawable is null when you ask for a particular index/position.
As you may already know, list items are rendered (getView() is called) every time the the item at the index is scrolled to the visible screen; so caching images and using the convertView (the view already been created once) would make your adapter and list efficient.
You could even put some system.outs in your getView() method to see the difference.
I hope these help
Best
-serkan

Reuse inflated views

I am building a complex view based on dynamic data. Depending on the number of data elements in collections I am adding more views. Each of these subviews are complex and get inflated in the loop through the data collection.
This is of course inefficient and I would like to figure out a way to inflate the subview only once and then reusing them instead. Is this possible somehow?
PS: I do not want to build up the subviews in code (I know I could) because that would make things even messier due to the complexities and number of subviews, but if the performance would increase considerably I might take a look at that.
PPS: There is no visible performance problem but traceview that most of the time is spent inflating and if I can make it faster I would love to ;-)
you can check out the Google IO Session entitled 'The world of ListView'.
It explains very nicely how to prevent inflating the same view again and again, and how to reuse a particular view if it has been already inflated earlier.
Here is the link.
http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
You can either download the .pdf file or view the video.
Hope it helps.
Regards,
Mahendra Liya.

Categories

Resources