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

Add the ability to show/hide headers on demand. #387

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions library/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

<!-- StickyListHeaders attributes -->
<attr name="hasStickyHeaders" format="boolean" />
<attr name="showHeaders" format="boolean" />
<attr name="isDrawingListUnderStickyHeader" format="boolean" />
</declare-styleable>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void onStickyHeaderChanged(StickyListHeadersListView l, View header,
/* --- Settings --- */
private boolean mAreHeadersSticky = true;
private boolean mClippingToPadding = true;
private boolean mShowHeaders = true;
private boolean mIsDrawingListUnderStickyHeader = true;
private int mStickyHeaderTopOffset = 0;
private int mPaddingLeft = 0;
Expand All @@ -102,6 +103,7 @@ void onStickyHeaderChanged(StickyListHeadersListView l, View header,
private OnStickyHeaderOffsetChangedListener mOnStickyHeaderOffsetChangedListener;
private OnStickyHeaderChangedListener mOnStickyHeaderChangedListener;
private AdapterWrapperDataSetObserver mDataSetObserver;
private StickyListHeadersAdapter mOriginalAdapter;
private Drawable mDivider;
private int mDividerHeight;

Expand Down Expand Up @@ -205,6 +207,7 @@ public StickyListHeadersListView(Context context, AttributeSet attrs, int defSty

// -- StickyListHeaders attributes --
mAreHeadersSticky = a.getBoolean(R.styleable.StickyListHeadersListView_hasStickyHeaders, true);
mShowHeaders = a.getBoolean(R.styleable.StickyListHeadersListView_showHeaders, mShowHeaders);
mIsDrawingListUnderStickyHeader = a.getBoolean(
R.styleable.StickyListHeadersListView_isDrawingListUnderStickyHeader,
true);
Expand Down Expand Up @@ -290,7 +293,7 @@ private void clearHeader() {

private void updateOrClearHeader(int firstVisiblePosition) {
final int adapterCount = mAdapter == null ? 0 : mAdapter.getCount();
if (adapterCount == 0 || !mAreHeadersSticky) {
if (adapterCount == 0 || !mAreHeadersSticky || !mShowHeaders) {
return;
}

Expand Down Expand Up @@ -659,6 +662,18 @@ public void setAdapter(StickyListHeadersAdapter adapter) {
if (mAdapter != null) {
mAdapter.unregisterDataSetObserver(mDataSetObserver);
}
mOriginalAdapter = adapter;
if(!mShowHeaders) {
mAdapter = null;
mList.setAdapter(adapter);
mList.setDivider(mDivider);
mList.setDividerHeight(mDividerHeight);
clearHeader();
return;
} else {
mList.setDivider(null);
mList.setDividerHeight(0);
}

if (adapter instanceof SectionIndexer) {
mAdapter = new SectionIndexerAdapterWrapper(getContext(), adapter);
Expand Down Expand Up @@ -1077,4 +1092,21 @@ public void setBlockLayoutChildren(boolean blockLayoutChildren) {
mList.setBlockLayoutChildren(blockLayoutChildren);
}

/**
* Shows or hides all headers, when changed the scroll would be reset.
*/
public void setShowHeaders(boolean value) {
mShowHeaders = value;
if (!mShowHeaders && !mAreHeadersSticky) {
clearHeader();
} else {
updateOrClearHeader(mList.getFixedFirstVisibleItem());
}
setAdapter(mOriginalAdapter);
}

public boolean showHeaders() {
return mShowHeaders;
}

}
7 changes: 7 additions & 0 deletions sample/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@
android:id="@+id/sticky_checkBox"
android:checked="true"/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_header"
android:id="@+id/show_checkBox"
android:checked="true"/>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions sample/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="update_list">Update list</string>
<string name="clear_list">Clear list</string>
<string name="sticky_header">Sticky header</string>
<string name="show_header">Show header</string>
<string name="fade_header">Fade header</string>
<string name="draw_behind_header">Draw behind header</string>
<string name="fast_scroll">Fast scroll</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class TestActivity extends ActionBarActivity implements
private Button clearButton;

private CheckBox stickyCheckBox;
private CheckBox showCheckBox;
private CheckBox fadeCheckBox;
private CheckBox drawBehindCheckBox;
private CheckBox fastScrollCheckBox;
Expand Down Expand Up @@ -104,6 +105,8 @@ public void run() {

stickyCheckBox = (CheckBox) findViewById(R.id.sticky_checkBox);
stickyCheckBox.setOnCheckedChangeListener(checkBoxListener);
showCheckBox = (CheckBox) findViewById(R.id.show_checkBox);
showCheckBox.setOnCheckedChangeListener(checkBoxListener);
fadeCheckBox = (CheckBox) findViewById(R.id.fade_checkBox);
fadeCheckBox.setOnCheckedChangeListener(checkBoxListener);
drawBehindCheckBox = (CheckBox) findViewById(R.id.draw_behind_checkBox);
Expand Down Expand Up @@ -153,6 +156,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
stickyList.setFastScrollEnabled(isChecked);
stickyList.setFastScrollAlwaysVisible(isChecked);
break;
case R.id.show_checkBox:
stickyList.setShowHeaders(isChecked);
break;
}
}
};
Expand Down