Skip to content

Commit

Permalink
save collapsed headers in state
Browse files Browse the repository at this point in the history
  • Loading branch information
mtotschnig committed May 17, 2020
1 parent a17001c commit b2789b4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;


/**
* @author lsjwzh
*/
class ExpandableStickyListHeadersAdapter extends BaseAdapter implements StickyListHeadersAdapter {
class ExpandableStickyListHeadersAdapter extends BaseAdapter implements StickyListHeadersAdapter {

protected final StickyListHeadersAdapter mInnerAdapter;
DualHashMap<View,Long> mViewToItemIdMap = new DualHashMap<View, Long>();
DistinctMultiHashMap<Integer,View> mHeaderIdToViewMap = new DistinctMultiHashMap<Integer, View>();
List<Long> mCollapseHeaderIds = new ArrayList<Long>();
List<Long> mCollapseHeaderIds = new ArrayList<>();

ExpandableStickyListHeadersAdapter(StickyListHeadersAdapter innerAdapter){
this.mInnerAdapter = innerAdapter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package se.emilsjolander.stickylistheaders;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -19,7 +22,7 @@ public interface IAnimationExecutor{

ExpandableStickyListHeadersAdapter mExpandableStickyListHeadersAdapter;


List<Long> mCollapseHeaderIds;

IAnimationExecutor mDefaultAnimExecutor = new IAnimationExecutor() {
@Override
Expand Down Expand Up @@ -55,6 +58,10 @@ public void setAdapter(StickyListHeadersAdapter adapter) {
mExpandableStickyListHeadersAdapter = (adapter instanceof SectionIndexingStickyListHeadersAdapter) ?
new SectionIndexingExpandableStickyListHeadersAdapter(((SectionIndexingStickyListHeadersAdapter) adapter)) :
new ExpandableStickyListHeadersAdapter(adapter);
if (mCollapseHeaderIds != null) {
mExpandableStickyListHeadersAdapter.mCollapseHeaderIds = mCollapseHeaderIds;
mCollapseHeaderIds = null;
}
super.setAdapter(mExpandableStickyListHeadersAdapter);
}

Expand Down Expand Up @@ -124,4 +131,57 @@ private void animateView(final View target, final int type) {

}

@Override
public Parcelable onSaveInstanceState() {
return new SavedState(super.onSaveInstanceState(), mExpandableStickyListHeadersAdapter.mCollapseHeaderIds);
}

@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
mCollapseHeaderIds = ss.collapsedIds;
if (mExpandableStickyListHeadersAdapter != null) {
mExpandableStickyListHeadersAdapter.mCollapseHeaderIds = mCollapseHeaderIds;
}
}

static class SavedState extends BaseSavedState {
private List<Long> collapsedIds;

/**
* Constructor called from {@link StickyListHeadersListView#onSaveInstanceState()}
*/
SavedState(Parcelable superState, List<Long> collapsedIds) {
super(superState);
this.collapsedIds = collapsedIds;
}

/**
* Constructor called from {@link #CREATOR}
*/
private SavedState(Parcel in) {
super(in);
collapsedIds = new ArrayList<>();
in.readList(collapsedIds, null);
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeList(collapsedIds);
}

public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}

}

0 comments on commit b2789b4

Please sign in to comment.