Memory is a very very critical resource for any kind of application be it mobile or desktop. And a developer needs to manage it very efficiently in order to give pleasant experience to the users.
The concept of View Recycling comes into picture when we are dealing with a very long list of views, say for example a list of 1000 contacts. In a very simplistic application, we can assume that each contact is being displayed as a horizontal LinearLayout containing two views namely an ImageView and a TextView. We are not gonna display all the 1000 contacts at the same time. Our mobile screen may fit nearly 10-15 contacts depending upon the size of the screen. So allocating memory for all the 1000 contacts when the application is loaded is clearly an indicative of unwise usage of memory.
This is where the concept of View Recycling comes in the scene. Here what we do is inflate only as much Views as required and re-use them as and when they are not used.
The concept of View Recycling comes into picture when we are dealing with a very long list of views, say for example a list of 1000 contacts. In a very simplistic application, we can assume that each contact is being displayed as a horizontal LinearLayout containing two views namely an ImageView and a TextView. We are not gonna display all the 1000 contacts at the same time. Our mobile screen may fit nearly 10-15 contacts depending upon the size of the screen. So allocating memory for all the 1000 contacts when the application is loaded is clearly an indicative of unwise usage of memory.
This is where the concept of View Recycling comes in the scene. Here what we do is inflate only as much Views as required and re-use them as and when they are not used.
ListView and ArrayAdapter work hand in hand. ListView requests the ArrayAdapter for a list item view by indicating the position as seen in the above image. Initially when there are no View which can be re-used the ArrayAdapter inflates a new View from the XML. And then it returns the inflated view to the ListView which will be displayed on the screen. However, things happen quite different when there are Views available which can be re-used.
As can be seen in the above image, Scrap Views are nothing but the Views which are available for re-use. When a user, scrolls the screen horizontally some of the Views will be moved out of the screen. Those Views which are not rendered will be collected in the Scrap View. When the ListView requests the ArrayAdpater for a list view at some position, the ArrayAdapter will see if there is any View available for re-use. When it is available the ArrayAdapter will just modify the text or image or anything which are specific to the requested View and return the modified View to the ListView. Thus we save the cost and effort of inflating a new View from scratch and re-using things.
I felt this is a very cool technique which can be useful in a lot more practical scenarios. I am planning to implement this technique in my app which I have mentioned in my last post!!
I felt this is a very cool technique which can be useful in a lot more practical scenarios. I am planning to implement this technique in my app which I have mentioned in my last post!!