Java – Where is the code in Android that manages View reuse?

Where is the code in Android that manages View reuse?… here is a solution to the problem.

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:

  1. Determine whether the logic for a View is eligible for reuse
  2. The code that manages the View pool can be reused
  3. Remove reusable code from the View from the pool and reset its property values to represent logically different View
  4. 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 to onItemClick might have been reused, and so activeElement 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.

Related Problems and Solutions