Where is the code in Android that manages View reuse?
Where is the source code for managing views
? Reuse in Android? I can think of three different parts of the process, but there may be more:
- Determine whether the logic for
a View
is eligible for reuse - The code that manages
the View
pool can be reused - Remove reusable
code from the
View from the pool and reset its property values to represent logically differentView
values
EDIT: Blog post Developing applications for Android – gotchas and quirks give the following examples:
public class PencilWise extends ListActivity {
View activeElement;
// ...
@Override
public void onCreate ( Bundle savedInstanceState ) {
// ...
this.getListView( ).setOnItemClickListener ( new OnItemClickListener ( ) {
public void onItemClick ( AdapterView<?> parent, View view, int position, long id ) {
MyActivity.this.activeElement = view;
MyActivity.this.showDialog ( DIALOG_ANSWER );
}
} );
}
}
The
showDialog
method will display the answer dialog, which needs to know what question the user has opened. The problem is that by the time the dialog opens, the view passed toonItemClick
might have been reused, and soactiveElement
would no longer point to the element the user clicked to open the dialog in the first place!
Solution
View recycling is performed by AbsListView
and its subclasses ListView
and GridView
. You can find the source code for these classes here: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget
Start with ListView
and AbsListView
.