Question

When a list is shown and scrolled around, ListAdapter.getView() either creates a new list item view, or retools an existing view passed to it (as "convertView) to display new content.

I'm wondering, if there's a way to get all the views created by getView()? E.g. if the list item views needs to free some resource when the activity is destroyed, it would be nice to go through all list item views and tell them to release the resource.

Was it helpful?

Solution

Well, it's not as simple as iterating through the ListView using ListView#getChildAt(int) - the ListView is an AbsListView subclass - and AbsListView implements the RecycleBin used to hold Views that will be reused.

Before a View is placed in the RecycleBin its detached from its parent (the list view) using ViewGroup#detachViewFromParent(View), thus rendering it unaccessible using getChildAt(int).

What you should implement is the RecyclerListener interface that will notify you when a View is moved to the RecycleBin.

OTHER TIPS

Since ListView is a ViewGroup, you should be able to iterate on child views like this:

for (int i=0; i < myListView.getChildCount(); i++) {
    View v = myListView.getChildAt(i);

    //Check to see if this view has the resource, then release it
}

Something similar was needed in this question: Android: Access child views from a ListView

It is java. You can't order the application or instance to be really destroyed so as to free resources. With finalize() you can only rise the probability that java machine will do it.

Unless your adapter is used outside of this Activity (in which case, you don't really know that you no longer need these views right?), your only reference(s) to this adapter is within the Activity. That means that if your Activity is garbage collected your adapter will be garbage collected, which means that your views will get collected. I wouldn't bother trying to optimize this unless you have a measurable problem with this behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top