Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically show/hide headers without changing the adapter #453

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions library/src/se/emilsjolander/stickylistheaders/AdapterWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,21 @@ public WrapperView getView(int position, View convertView, ViewGroup parent) {
WrapperView wv = (convertView == null) ? new WrapperView(mContext) : (WrapperView) convertView;
View item = mDelegate.getView(position, wv.mItem, parent);
View header = null;
if (previousPositionHasSameHeader(position)) {
recycleHeaderIfExists(wv);
} else {
header = configureHeader(wv, position);
}
if((item instanceof Checkable) && !(wv instanceof CheckableWrapperView)) {
// Need to create Checkable subclass of WrapperView for ListView to work correctly
wv = new CheckableWrapperView(mContext);
} else if(!(item instanceof Checkable) && (wv instanceof CheckableWrapperView)) {
wv = new WrapperView(mContext);
if (mDelegate.shouldShowHeaders()) {
if (previousPositionHasSameHeader(position)) {
recycleHeaderIfExists(wv);
} else {
header = configureHeader(wv, position);
}
if((item instanceof Checkable) && !(wv instanceof CheckableWrapperView)) {
// Need to create Checkable subclass of WrapperView for ListView to work correctly
wv = new CheckableWrapperView(mContext);
} else if(!(item instanceof Checkable) && (wv instanceof CheckableWrapperView)) {
wv = new WrapperView(mContext);
}
}
wv.update(item, header, mDivider, mDividerHeight);

return wv;
}

Expand Down Expand Up @@ -222,4 +225,9 @@ public long getHeaderId(int position) {
return mDelegate.getHeaderId(position);
}

@Override
public boolean shouldShowHeaders() {
return mDelegate.shouldShowHeaders();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public long getHeaderId(int position) {
return mInnerAdapter.getHeaderId(position);
}

@Override
public boolean shouldShowHeaders() {
return mInnerAdapter.shouldShowHeaders();
}

@Override
public boolean areAllItemsEnabled() {
return mInnerAdapter.areAllItemsEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public interface StickyListHeadersAdapter extends ListAdapter {
* The id of the header at the specified position.
*/
long getHeaderId(int position);

/**
* Check whether or not the adapter should display header views.
*
* @return
* If false, no headers will be displayed.
*/
boolean shouldShowHeaders();
}
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,10 @@ public void onScroll(AbsListView view, int firstVisibleItem,
mOnScrollListenerDelegate.onScroll(view, firstVisibleItem,
visibleItemCount, totalItemCount);
}
updateOrClearHeader(mList.getFixedFirstVisibleItem());

if (mAdapter != null && mAdapter.shouldShowHeaders()) {
updateOrClearHeader(mList.getFixedFirstVisibleItem());
}
}

@Override
Expand Down