Created
July 6, 2019 11:00
-
-
Save matthew-carroll/ae0a7872ac47f0e50148d02a395dee72 to your computer and use it in GitHub Desktop.
Saving child state in custom ViewGroup with new SparseArray.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { | |
// Create a SparseArray just for us! | |
SparseArray mySparseArray = new SparseArray(mChildrenCount + 1); // +1 for our ViewGroup's state. | |
// Repeat the normal logic for this method, but pass our "mySparseArray" instead of "container" | |
super.dispatchSaveInstanceState(mySparseArray); | |
final int count = mChildrenCount; | |
final View[] children = mChildren; | |
for (int i = 0; i < count; i++) { | |
View c = children[i]; | |
if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { | |
c.dispatchSaveInstanceState(mySparseArray); | |
} | |
} | |
// Put mySparseArray in the container we were given. | |
container.put(getId(), mySparseArray); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can you put a
SparseArray
to theSparseArray
whose generic type isParcelable
? The IDE complain that the required type isParcelable
but provided isSparseArray
.The correctly solution should be like this.