Skip to content
Browse files

fixed issue #50

  • Loading branch information...
1 parent 0879d7e commit 9bd04c528b6bd6ed7c5dd53ccf1a1c919ca24d0a @emilsjolander committed
View
32 library/src/com/emilsjolander/components/stickylistheaders/StickyListHeadersListView.java
@@ -11,6 +11,7 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
+import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
@@ -118,7 +119,7 @@ private void reset() {
header = null;
currentHeaderId = null;
}
-
+
@Override
public boolean performItemClick(View view, int position, long id) {
view = ((WrapperView) view).item;
@@ -208,7 +209,7 @@ protected void dispatchDraw(Canvas canvas) {
if (header == null || !areHeadersSticky) {
return;
}
-
+
int headerHeight = getHeaderHeight();
int top = headerBottomPosition - headerHeight;
clippingRect.left = getPaddingLeft();
@@ -226,12 +227,18 @@ protected void dispatchDraw(Canvas canvas) {
header.draw(canvas);
canvas.restore();
}
-
- private void measureHeader(){
+
+ private void measureHeader() {
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(getWidth(),
MeasureSpec.EXACTLY);
- int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0,
- MeasureSpec.UNSPECIFIED);
+ int heightMeasureSpec = 0;
+
+ ViewGroup.LayoutParams params = header.getLayoutParams();
+ if (params != null && params.height > 0) {
+ heightMeasureSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY);
+ } else {
+ heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
+ }
header.measure(widthMeasureSpec, heightMeasureSpec);
header.layout(getLeft() + getPaddingLeft(), 0, getRight()
- getPaddingRight(), header.getMeasuredHeight());
@@ -277,7 +284,7 @@ private void scrollChanged(int firstVisibleItem) {
measureHeader();
}
currentHeaderId = newHeaderId;
-
+
final int childCount = getChildCount();
if (childCount != 0) {
WrapperView viewToWatch = null;
@@ -292,12 +299,13 @@ private void scrollChanged(int firstVisibleItem) {
} else {
childDistance = child.getTop();
}
-
- if(childDistance<0){
+
+ if (childDistance < 0) {
continue;
}
- if (viewToWatch == null || !viewToWatch.hasHeader()
+ if (viewToWatch == null
+ || !viewToWatch.hasHeader()
|| (child.hasHeader() && childDistance < watchingChildDistance)) {
viewToWatch = child;
watchingChildDistance = childDistance;
@@ -307,7 +315,7 @@ private void scrollChanged(int firstVisibleItem) {
int headerHeight = getHeaderHeight();
if (viewToWatch != null && viewToWatch.hasHeader()) {
-
+
if (firstVisibleItem == 0 && super.getChildAt(0).getTop() > 0
&& !clippingToPadding) {
headerBottomPosition = 0;
@@ -332,7 +340,7 @@ private void scrollChanged(int firstVisibleItem) {
}
}
}
-
+
int top = clippingToPadding ? getPaddingTop() : 0;
for (int i = 0; i < childCount; i++) {
WrapperView child = (WrapperView) super.getChildAt(i);
View
40 library/src/com/emilsjolander/components/stickylistheaders/WrapperView.java
@@ -44,18 +44,18 @@ void update(View item, View header, Drawable divider, int dividerHeight) {
if (item == null) {
throw new NullPointerException("List view item must not be null.");
}
-
- if(this.item != item){
+
+ if (this.item != item) {
removeView(this.item);
this.item = item;
- // keep track of whether or not the wrapped item is Checkable
+ // keep track of whether or not the wrapped item is Checkable
// to know if we can delegate Checkable methods to it.
checkable = item instanceof Checkable;
addView(item);
}
-
- if(this.header != header){
- if(this.header != null){
+
+ if (this.header != header) {
+ if (this.header != null) {
removeView(this.header);
}
this.header = header;
@@ -63,8 +63,8 @@ void update(View item, View header, Drawable divider, int dividerHeight) {
addView(header);
}
}
-
- if(this.divider != divider){
+
+ if (this.divider != divider) {
this.divider = divider;
this.dividerHeight = dividerHeight;
invalidate();
@@ -78,18 +78,30 @@ boolean hasHeader() {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
+ int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth,
+ MeasureSpec.EXACTLY);
int measuredHeight = 0;
if (header != null) {
- header.measure(MeasureSpec.makeMeasureSpec(measuredWidth,
- MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0,
- MeasureSpec.UNSPECIFIED));
+ ViewGroup.LayoutParams params = header.getLayoutParams();
+ if (params != null && params.height > 0) {
+ header.measure(childWidthMeasureSpec,
+ MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
+ } else {
+ header.measure(childWidthMeasureSpec,
+ MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
+ }
measuredHeight += header.getMeasuredHeight();
} else if (divider != null) {
measuredHeight += dividerHeight;
}
- item.measure(
- MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
+ ViewGroup.LayoutParams params = item.getLayoutParams();
+ if (params != null && params.height > 0) {
+ item.measure(childWidthMeasureSpec,
+ MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY));
+ } else {
+ item.measure(childWidthMeasureSpec,
+ MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
+ }
measuredHeight += item.getMeasuredHeight();
setMeasuredDimension(measuredWidth, measuredHeight);

0 comments on commit 9bd04c5

Please sign in to comment.
Something went wrong with that request. Please try again.