@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mStickHeaderMap.isEmpty()) { return; } // measure header size int totalHeight = getPaddingTop(); int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); int size = mHeaderViews.size(); for (int i = 0; i < size; i++) { View view = mHeaderViews.get(i); view.measure(widthSpec, heightSpec); if (mStickHeaderMap.containsKey(view)) { StickView stickView = mStickHeaderMap.get(view); if (stickView.top == INVALID_TOP) { stickView.top = Math.min(totalHeight, getMeasuredHeight() - getPaddingBottom()); } ViewGroup.LayoutParams params = stickView.placeHolderView.getLayoutParams(); params.width = view.getMeasuredWidth(); params.height = view.getMeasuredHeight(); } totalHeight += (view.getMeasuredHeight() + mListView.getDividerHeight()); } }
@Override public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (null != mDelegateScrollListener) { mDelegateScrollListener.onScroll( view, firstVisibleItem, visibleItemCount, totalItemCount); } int maxStickTop = getPaddingTop(); int size = mHeaderViews.size(); int lastVisibleItem = firstVisibleItem + visibleItemCount - 1; for (int i = 0; i < size; i++) { View v = mHeaderViews.get(i); if (!mStickHeaderMap.containsKey(v)) { continue; } StickView stickView = mStickHeaderMap.get(v); if (i <= firstVisibleItem) { // top the view if (v.getTop() != maxStickTop) { stickView.top = maxStickTop; v.layout(v.getLeft(), maxStickTop, v.getRight(), maxStickTop + v.getHeight()); Log.d(TAG, "layout stick header " + stickView.position + "to " + maxStickTop); } maxStickTop += v.getHeight(); } else if (i > firstVisibleItem && i <= lastVisibleItem) { int alignTop = Math.max(stickView.placeHolderView.getTop() + getPaddingTop(), maxStickTop); if (v.getTop() != alignTop) { stickView.top = alignTop; v.layout(v.getLeft(), alignTop, v.getRight(), alignTop + v.getHeight()); Log.d(TAG, "layout stick header " + stickView.position + "to " + alignTop); } maxStickTop = alignTop + v.getHeight(); } else { if (v.getTop() != getHeight() - getPaddingBottom()) { stickView.top = getHeight() - getPaddingBottom(); v.layout(v.getLeft(), stickView.top, v.getRight(), stickView.top + v.getHeight()); Log.d( TAG, "layout stick header " + stickView.position + "to " + stickView.toString()); } } } }
@Override protected void onRestoreInstanceState(Parcelable state) { SavedState savedState = (SavedState) state; super.onRestoreInstanceState(savedState.getSuperState()); int size = mHeaderViews.size(); int index = 0; for (int i = 0; i < size; i++) { View view = mHeaderViews.get(i); if (!mStickHeaderMap.containsKey(view)) { continue; } StickView stickView = mStickHeaderMap.get(view); stickView.top = savedState.viewTops[index]; index++; if (index >= mStickHeaderMap.size()) { break; } } }