Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #13 from novoda/id-restoration
Browse files Browse the repository at this point in the history
View Id restoration
  • Loading branch information
ataulm authored Apr 19, 2017
2 parents ca7662a + ab521fe commit 4a0b8e6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public abstract class ViewPagerAdapter<V extends View> extends PagerAdapter {
@Override
public V instantiateItem(ViewGroup container, int position) {
V view = createView(container, position);
SparseArray<Parcelable> viewState = viewPagerAdapterState.get(position);
bindView(view, position, viewState);
SparseArray<Parcelable> viewState = viewPagerAdapterState.getViewState(position);

int restoredId = viewPagerAdapterState.getId(position);
view.setId(restoredId == View.NO_ID ? viewIdGenerator.generateViewId() : restoredId);

view.setId(viewIdGenerator.generateViewId());
bindView(view, position, viewState);
instantiatedViews.put(view, position);
container.addView(view);

Expand Down Expand Up @@ -70,12 +72,13 @@ public void notifyDataSetChanged() {
super.notifyDataSetChanged();
for (Map.Entry<V, Integer> entry : instantiatedViews.entrySet()) {
int position = entry.getValue();
SparseArray<Parcelable> viewState = viewPagerAdapterState.get(position);
SparseArray<Parcelable> viewState = viewPagerAdapterState.getViewState(position);
bindView(entry.getKey(), position, viewState);
}
}

@SuppressWarnings("unchecked") // `key` is the object we return in `instantiateItem(ViewGroup container, int position)`
@SuppressWarnings("unchecked")
// `key` is the object we return in `instantiateItem(ViewGroup container, int position)`
@Override
public void destroyItem(ViewGroup container, int position, Object key) {
V view = (V) key;
Expand All @@ -86,7 +89,7 @@ public void destroyItem(ViewGroup container, int position, Object key) {
private void saveViewState(int position, V view) {
SparseArray<Parcelable> viewState = new SparseArray<>();
view.saveHierarchyState(viewState);
viewPagerAdapterState.put(position, viewState);
viewPagerAdapterState.put(view.getId(), position, viewState);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.View;

public class ViewPagerAdapterState implements Parcelable {

Expand All @@ -18,16 +20,31 @@ public ViewPagerAdapterState[] newArray(int size) {
}
};

private static final String KEY_VIEW_IDS = "id";

private final SparseIntArray viewIds;
private final SparseArray<SparseArray<Parcelable>> viewStates;

public static ViewPagerAdapterState newInstance() {
return new ViewPagerAdapterState(new SparseArray<SparseArray<Parcelable>>());
SparseIntArray viewIds = new SparseIntArray();
SparseArray<SparseArray<Parcelable>> viewStates = new SparseArray<>();
return new ViewPagerAdapterState(viewIds, viewStates);
}

private static ViewPagerAdapterState from(Parcel in) {
Bundle bundle = in.readBundle(ViewPagerAdapterState.class.getClassLoader());
SparseArray<SparseArray<Parcelable>> viewStates = extractViewStatesFrom(bundle);
return new ViewPagerAdapterState(viewStates);
SparseIntArray viewIds = extractIdsFrom(bundle);
return new ViewPagerAdapterState(viewIds, viewStates);
}

private static SparseIntArray extractIdsFrom(Bundle bundle) {
SparseIntArray output = new SparseIntArray();
int[] ids = bundle.getIntArray(KEY_VIEW_IDS);
for (int index = 0; index < ids.length; index++) {
output.put(index, ids[index]);
}
return output;
}

private static SparseArray<SparseArray<Parcelable>> extractViewStatesFrom(Bundle bundle) {
Expand All @@ -39,28 +56,44 @@ private static SparseArray<SparseArray<Parcelable>> extractViewStatesFrom(Bundle
return viewStates;
}

private ViewPagerAdapterState(SparseArray<SparseArray<Parcelable>> viewStates) {
private ViewPagerAdapterState(SparseIntArray viewIds, SparseArray<SparseArray<Parcelable>> viewStates) {
this.viewIds = viewIds;
this.viewStates = viewStates;
}

public void put(int position, SparseArray<Parcelable> viewState) {
public void put(int viewId, int position, SparseArray<Parcelable> viewState) {
viewIds.put(position, viewId);
viewStates.put(position, viewState);
}

public SparseArray<Parcelable> get(int position) {
public SparseArray<Parcelable> getViewState(int position) {
return viewStates.get(position);
}

public int getId(int position) {
return viewIds.get(position, View.NO_ID);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
Bundle bundle = new Bundle();
int[] viewIds = viewIdsArray();
bundle.putIntArray(KEY_VIEW_IDS, viewIds);
for (int i = 0; i < viewStates.size(); i++) {
SparseArray<Parcelable> viewState = viewStates.get(i);
bundle.putSparseParcelableArray(String.valueOf(i), viewState);
}
dest.writeBundle(bundle);
}

private int[] viewIdsArray() {
int[] output = new int[viewIds.size()];
for (int index = 0; index < viewIds.size(); index++) {
output[index] = viewIds.get(index);
}
return output;
}

@Override
public int describeContents() {
return 0;
Expand Down

0 comments on commit 4a0b8e6

Please sign in to comment.