Last active
August 3, 2016 07:33
-
-
Save ultraon/c13ce710f12327a7bbdb to your computer and use it in GitHub Desktop.
Example of ChildrenViewStateHelper for saving view states with the same id in ViewGroup
This file contains 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
package mypackage.presentation.utils.view; | |
import android.os.Bundle; | |
import android.os.Parcelable; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.util.SparseArray; | |
import android.view.ViewGroup; | |
public class ChildrenViewStateHelper { | |
public static final String DEFAULT_CHILDREN_STATE_KEY = ChildrenViewStateHelper.class.getSimpleName() + ".childrenState"; | |
private ViewGroup mClientViewGroup; | |
public static ChildrenViewStateHelper newInstance(@NonNull final ViewGroup viewGroup) { | |
final ChildrenViewStateHelper handler = new ChildrenViewStateHelper(); | |
handler.mClientViewGroup = viewGroup; | |
return handler; | |
} | |
private ChildrenViewStateHelper() { | |
} | |
/** | |
* Handles saving of children view states into SparseArray and returns it for next storing. | |
* Also you need override {@link ViewGroup#dispatchSaveInstanceState(SparseArray)} and call {@link ViewGroup#dispatchFreezeSelfOnly(SparseArray)}. | |
*/ | |
public SparseArray<Parcelable> saveChildrenState() { | |
final SparseArray<Parcelable> array = new SparseArray<>(); | |
for (int i = 0; i < mClientViewGroup.getChildCount(); i++) { | |
final Bundle bundle = new Bundle(); | |
final SparseArray<Parcelable> childArray = new SparseArray<>(); //create independent SparseArray for each child (View or ViewGroup) | |
mClientViewGroup.getChildAt(i).saveHierarchyState(childArray); | |
bundle.putSparseParcelableArray(DEFAULT_CHILDREN_STATE_KEY, childArray); | |
array.append(i, bundle); | |
} | |
return array; | |
} | |
/** | |
* Handles restoring of children view states from SparseArray which need to extract before. | |
* Also you need override {@link ViewGroup#dispatchRestoreInstanceState(SparseArray)} and call {@link ViewGroup#dispatchThawSelfOnly(SparseArray)}. | |
*/ | |
public void restoreChildrenState(@Nullable final SparseArray<Parcelable> childrenState) { | |
if (null == childrenState) { | |
return; | |
} | |
for (int i = 0; i < mClientViewGroup.getChildCount(); i++) { | |
final Bundle bundle = (Bundle) childrenState.get(i); | |
final SparseArray<Parcelable> childState = bundle.getSparseParcelableArray(DEFAULT_CHILDREN_STATE_KEY); | |
mClientViewGroup.getChildAt(i).restoreHierarchyState(childState); | |
} | |
} | |
} |
This file contains 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
package mypackage..presentation.view; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Parcelable; | |
import android.util.AttributeSet; | |
import android.util.SparseArray; | |
import android.widget.LinearLayout; | |
import mypackage.utils.ChildrenViewStateHelper; | |
public class StateLinearLayout extends LinearLayout { | |
public StateLinearLayout(final Context context) { | |
super(context); | |
} | |
public StateLinearLayout(final Context context, final AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public StateLinearLayout(final Context context, final AttributeSet attrs, final int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public StateLinearLayout(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
} | |
@Override | |
protected Parcelable onSaveInstanceState() { | |
final Bundle state = new Bundle(); | |
state.putParcelable("superState", super.onSaveInstanceState()); | |
state.putSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY, ChildrenViewStateHelper.newInstance(this).saveChildrenState()); | |
return state; | |
} | |
@Override | |
protected void onRestoreInstanceState(final Parcelable state) { | |
if (state instanceof Bundle) { | |
final Bundle localState = (Bundle) state; | |
super.onRestoreInstanceState(localState.getParcelable("superState")); | |
ChildrenViewStateHelper.newInstance(this).restoreChildrenState(localState.getSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY)); | |
} else { | |
super.onRestoreInstanceState(state); | |
} | |
} | |
@Override | |
protected void dispatchSaveInstanceState(final SparseArray<Parcelable> container) { | |
dispatchFreezeSelfOnly(container); | |
} | |
@Override | |
protected void dispatchRestoreInstanceState(final SparseArray<Parcelable> container) { | |
dispatchThawSelfOnly(container); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment