From 6d929220490793756a0216339eb211c84f712399 Mon Sep 17 00:00:00 2001 From: patedit Date: Wed, 13 Apr 2016 18:31:02 -0700 Subject: [PATCH 1/2] Enable the user to choose whether or not the adapter should display header views --- .../stickylistheaders/AdapterWrapper.java | 28 ++++++++++++------- .../ExpandableStickyListHeadersAdapter.java | 5 ++++ .../StickyListHeadersAdapter.java | 8 ++++++ .../StickyListHeadersListView.java | 5 +++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/library/src/se/emilsjolander/stickylistheaders/AdapterWrapper.java b/library/src/se/emilsjolander/stickylistheaders/AdapterWrapper.java index f86364e5..0fb989e1 100644 --- a/library/src/se/emilsjolander/stickylistheaders/AdapterWrapper.java +++ b/library/src/se/emilsjolander/stickylistheaders/AdapterWrapper.java @@ -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; } @@ -222,4 +225,9 @@ public long getHeaderId(int position) { return mDelegate.getHeaderId(position); } + @Override + public boolean shouldShowHeaders() { + return mDelegate.shouldShowHeaders(); + } + } diff --git a/library/src/se/emilsjolander/stickylistheaders/ExpandableStickyListHeadersAdapter.java b/library/src/se/emilsjolander/stickylistheaders/ExpandableStickyListHeadersAdapter.java index 5c932070..c7ae771f 100644 --- a/library/src/se/emilsjolander/stickylistheaders/ExpandableStickyListHeadersAdapter.java +++ b/library/src/se/emilsjolander/stickylistheaders/ExpandableStickyListHeadersAdapter.java @@ -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(); diff --git a/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java b/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java index 8b80b71f..9e918b23 100644 --- a/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java +++ b/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java @@ -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(); } diff --git a/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersListView.java b/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersListView.java index cd80f136..0e169163 100644 --- a/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersListView.java +++ b/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersListView.java @@ -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 From 5f1d61c642468276ec455d49f9ff380bf6f13aae Mon Sep 17 00:00:00 2001 From: patedit Date: Wed, 13 Apr 2016 18:34:13 -0700 Subject: [PATCH 2/2] Space --- .../stickylistheaders/StickyListHeadersAdapter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java b/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java index 9e918b23..e5a7590b 100644 --- a/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java +++ b/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersAdapter.java @@ -39,8 +39,8 @@ public interface StickyListHeadersAdapter extends ListAdapter { /** * Check whether or not the adapter should display header views. * - * @return + * @return * If false, no headers will be displayed. - */ + */ boolean shouldShowHeaders(); }