Skip to content

Commit

Permalink
Fix on < HONEYCOMB, optimise. Suppress unused warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
James Smith committed Sep 8, 2012
1 parent bbc12f7 commit be839b5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ public static interface PinnedSectionedHeaderAdapter {
private float mHeaderOffset;
private boolean mShouldPin = true;

@SuppressWarnings("unused")
public PinnedHeaderListView(Context context) {
super(context);
super.setOnScrollListener(this);
}

@SuppressWarnings("unused")
public PinnedHeaderListView(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnScrollListener(this);
}

@SuppressWarnings("unused")
public PinnedHeaderListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setOnScrollListener(this);
}

@SuppressWarnings("unused")
public void setPinHeaders(boolean shouldPin) {
mShouldPin = shouldPin;
}
Expand All @@ -54,35 +58,54 @@ public void setAdapter(ListAdapter adapter) {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (mAdapter == null || !mShouldPin) return;
int section = mAdapter.getSectionForPosition(firstVisibleItem);
mCurrentHeader = mAdapter.getSectionHeaderView(section, null, this);
if (mAdapter.isSectionHeader(firstVisibleItem + 1)) {
mHeaderOffset = getChildAt(1).getTop() - getChildAt(1).getMeasuredHeight();
} else if (mAdapter.isSectionHeader(firstVisibleItem)) {
getChildAt(0).setAlpha(0.0f);
} else {
mHeaderOffset = 0;
mCurrentHeader = getHeaderView(section, mCurrentHeader);
mHeaderOffset = 0.0f;

for (int i = firstVisibleItem; i < firstVisibleItem + visibleItemCount; i++) {
if (mAdapter.isSectionHeader(i)) {
View header = getChildAt(i - firstVisibleItem);
float headerTop = header.getTop();
float pinnedHeaderHeight = mCurrentHeader.getMeasuredHeight();
header.setVisibility(VISIBLE);
if (pinnedHeaderHeight >= headerTop && headerTop > 0) {
mHeaderOffset = headerTop - header.getHeight();
} else if (headerTop <= 0) {
header.setVisibility(INVISIBLE);
}
}
}
ensurePinnedHeaderLayout();

invalidate();
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

private void ensurePinnedHeaderLayout() {
if (mCurrentHeader.isLayoutRequested()) {
private View getHeaderView(int section, View oldView) {
View view;
if (oldView == null) {
view = mAdapter.getSectionHeaderView(section, oldView, this);
ensurePinnedHeaderLayout(view);
} else {
view = mAdapter.getSectionHeaderView(section, oldView, this);
}
return view;
}

private void ensurePinnedHeaderLayout(View header) {
if (header.isLayoutRequested()) {
int widthSpec = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY);
int heightSpec;
ViewGroup.LayoutParams layoutParams = mCurrentHeader.getLayoutParams();
ViewGroup.LayoutParams layoutParams = header.getLayoutParams();
if (layoutParams != null && layoutParams.height > 0) {
heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
} else {
heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
mCurrentHeader.measure(widthSpec, heightSpec);
int height = mCurrentHeader.getMeasuredHeight();
mCurrentHeader.layout(0, 0, getWidth(), height);
header.measure(widthSpec, heightSpec);
int height = header.getMeasuredHeight();
header.layout(0, 0, getWidth(), height);
}
}

Expand All @@ -92,6 +115,7 @@ protected void dispatchDraw(Canvas canvas) {
if (mAdapter == null || !mShouldPin || mCurrentHeader == null) return;
int saveCount = canvas.save();
canvas.translate(0, mHeaderOffset);
canvas.clipRect(0, 0, getWidth(), mCurrentHeader.getMeasuredHeight()); //needed for < HONEYCOMB
mCurrentHeader.draw(canvas);
canvas.restoreToCount(saveCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final int getItemViewType(int position) {
public final int getViewTypeCount() {
return getItemViewTypeCount() + getSectionHeaderViewTypeCount();
}

public final int getSectionForPosition(int position) {
int sectionStart = 0;
for (int i = 0; i < getSectionCount(); i++) {
Expand All @@ -63,7 +63,7 @@ public final int getSectionForPosition(int position) {
}
return 0;
}

private int getPositionInSectionForPosition(int position) {
int sectionStart = 0;
for (int i = 0; i < getSectionCount(); i++) {
Expand All @@ -76,10 +76,10 @@ private int getPositionInSectionForPosition(int position) {
}
return 0;
}

public final boolean isSectionHeader(int position) {
int sectionStart = 0;
for (int i = 0; i < getSectionCount(); i++) {;
for (int i = 0; i < getSectionCount(); i++) {
if (position == sectionStart) {
return true;
} else if (position < sectionStart) {
Expand All @@ -90,22 +90,22 @@ public final boolean isSectionHeader(int position) {
return false;
}

public int getItemViewType(int section, int position) {
public int getItemViewType(@SuppressWarnings("unused") int section, @SuppressWarnings("unused") int position) {
return ITEM_VIEW_TYPE;
}

public int getItemViewTypeCount() {
return 1;
}
public int getSectionHeaderViewType(int section) {

public int getSectionHeaderViewType(@SuppressWarnings("unused") int section) {
return HEADER_VIEW_TYPE;
}

public int getSectionHeaderViewTypeCount() {
return 1;
}

public abstract Object getItem(int section, int position);

public abstract long getItemId(int section, int position);
Expand Down

0 comments on commit be839b5

Please sign in to comment.