Android - ListView and Dynamic data

       One of the challenges working with ListView dealing with dynamic data is to determine the number of records good enough to fill the entire ListView. In some cases, apps try to determine this value by using list view's height and the height of the view from the adapter and then requesting to download so much of data when the activity starts. The idea is basically to ensure optimum data usage and good user experience. Fetching too little records isn't good user experience and too much is a lot of data usage. Besides, these apps anyway have a logic in the ListView's onScrollListener to fetch more data when needed.

       This scroll listener callback can be used to handle all use cases. The obvious one is to fetch more data when the user scrolls the list view. The other one is when the adapter is set on the list view. Lets consider the worst case where in the adapter provides a data set not enough to fill the entire list view. In this case, ListView displays the available data (from the adapter) via the usual View measure and layout process. At the end of the layout process, ListView invokes onScroll callback and this basically ends up triggering the same logic to fetch more data (although in this case, its not an user initiated scroll event).  From here on, the additional data is loaded to the adapter and a notifyDataSetChange is invoked on the adapter which causes the list view to load this new data and go through the layout process again. This logic is recursive until a point where the list view need not be invalidated as the additional data wouldn't be visible to the user.

   This ensures a ListView having a page full of data and doesn't require any assumptions on the number of records. This also works in orientation change use cases where the ListView is basically invalidated and redrawn.

No comments: