I am creating layouts at run time using heavy data, and adding these layouts to a scroll view. After the view get created, its working fine.
The problem here is, the data is very heavy and it takes more than a minute to create the screen, which is not so good user experience.
I want to create layouts only for the part of screen that are visible, and rest I can create on scrolling the scroll view.
Pls suggest how is that possible? Also, If someone has a better approach, Pls suggest.

- 2,787
- 1
- 12
- 10
1 Answers
You can start by creating only a set number of views each time[1], but always add a dummy 'loading' view at the end of the list if there are more views 'pending'. As soon as the user scrolls the ScrollView at the end of the list, start loading the next part of views on a background thread, and as soon as they are built, remove the dummy loading view, and add the new views into your container.
An other approach would be to start loading the next group of views, as soon as the previous group is done finish, but that might be a waste of resources.
An even better approach, is to combine those two methods described, and always have the next group of views being created, if the user is halfway done scrolling to the end
You can check how to know when the scrollview scrolls to the bottom here: Android: Detecting When ScrollView Hits Bottom
[1] Since you care about UX I would suggest that the number of rows should depending on the row's height and device max height. I.e. 4 views on a small device, 6 on a medium, 10 on a large.