Skip to content

Commit

Permalink
Merge pull request #256 from dmccartney/master
Browse files Browse the repository at this point in the history
added OnStickyHeaderChangedListener
  • Loading branch information
emilsjolander committed Feb 9, 2014
2 parents fdcb779 + 9c28bcf commit 12adcdd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ public interface OnStickyHeaderOffsetChangedListener {
}
```

A `OnStickyHeaderChangedListener` listens for changes to the header. This enables UI elements elsewhere to react to the current header (e.g. if each header is a date, then the rest of the UI can update when you scroll to a new date).
```java
public void setOnStickyHeaderChangedListener(OnStickyHeaderChangedListener listener);

public interface OnStickyHeaderChangedListener {
void onStickyHeaderChanged(StickyListHeadersListView l, View header, int itemPosition, long headerId);
}
```

Here are two methods added to the API for inspecting the children of the underlying `ListView`. I could not override the normal `getChildAt()` and `getChildCount()` methods as that would mess up the underlying measurement system of the `FrameLayout` wrapping the `ListView`.
```java
public View getListChildAt(int index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public interface OnStickyHeaderOffsetChangedListener {
public void onStickyHeaderOffsetChanged(StickyListHeadersListView l, View header, int offset);
}

/**
* Notifies the listener when the sticky header has been updated
*/
public interface OnStickyHeaderChangedListener {
/**
* @param l The view parent
* @param header The new sticky header view.
* @param itemPosition The position of the item within the adapter's data set of
* the item whose header is now sticky.
* @param headerId The id of the new sticky header.
*/
public void onStickyHeaderChanged(StickyListHeadersListView l, View header,
int itemPosition, long headerId);

}

/* --- Children --- */
private WrapperViewList mList;
private View mHeader;
Expand All @@ -81,6 +97,7 @@ public interface OnStickyHeaderOffsetChangedListener {
/* --- Other --- */
private OnHeaderClickListener mOnHeaderClickListener;
private OnStickyHeaderOffsetChangedListener mOnStickyHeaderOffsetChangedListener;
private OnStickyHeaderChangedListener mOnStickyHeaderChangedListener;
private AdapterWrapperDataSetObserver mDataSetObserver;
private Drawable mDivider;
private int mDividerHeight;
Expand Down Expand Up @@ -310,10 +327,12 @@ private void updateHeader(int firstVisiblePosition) {
}
swapHeader(header);
}

ensureHeaderHasCorrectLayoutParams(mHeader);
measureHeader(mHeader);

if(mOnStickyHeaderChangedListener != null) {
mOnStickyHeaderChangedListener.onStickyHeaderChanged(this,
mHeader, firstVisiblePosition, mHeaderId);
}
// Reset mHeaderOffset to null ensuring
// that it will be set on the header and
// not skipped for performance reasons.
Expand Down Expand Up @@ -572,6 +591,10 @@ public void setOnStickyHeaderOffsetChangedListener(OnStickyHeaderOffsetChangedLi
mOnStickyHeaderOffsetChangedListener = listener;
}

public void setOnStickyHeaderChangedListener(OnStickyHeaderChangedListener listener) {
mOnStickyHeaderChangedListener = listener;
}

public View getListChildAt(int index) {
return mList.getChildAt(index);
}
Expand Down
11 changes: 10 additions & 1 deletion sample/src/se/emilsjolander/stickylistheaders/TestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
public class TestActivity extends ActionBarActivity implements
AdapterView.OnItemClickListener, StickyListHeadersListView.OnHeaderClickListener,
StickyListHeadersListView.OnStickyHeaderOffsetChangedListener {
StickyListHeadersListView.OnStickyHeaderOffsetChangedListener,
StickyListHeadersListView.OnStickyHeaderChangedListener {

private TestBaseAdapter mAdapter;
private DrawerLayout mDrawerLayout;
Expand Down Expand Up @@ -44,6 +45,7 @@ public void onCreate(Bundle savedInstanceState) {
stickyList = (StickyListHeadersListView) findViewById(R.id.list);
stickyList.setOnItemClickListener(this);
stickyList.setOnHeaderClickListener(this);
stickyList.setOnStickyHeaderChangedListener(this);
stickyList.setOnStickyHeaderOffsetChangedListener(this);
stickyList.addHeaderView(getLayoutInflater().inflate(R.layout.list_header, null));
stickyList.addFooterView(getLayoutInflater().inflate(R.layout.list_footer, null));
Expand Down Expand Up @@ -166,4 +168,11 @@ public void onStickyHeaderOffsetChanged(StickyListHeadersListView l, View header
header.setAlpha(1 - (offset / (float) header.getMeasuredHeight()));
}
}

@Override
public void onStickyHeaderChanged(StickyListHeadersListView l, View header,
int itemPosition, long headerId) {
Toast.makeText(this, "Sticky Header Changed to " + headerId,
Toast.LENGTH_SHORT).show();
}
}

0 comments on commit 12adcdd

Please sign in to comment.