diff options
| author | 2013-06-26 00:56:39 +0000 | |
|---|---|---|
| committer | 2013-06-26 00:56:39 +0000 | |
| commit | 0fb018403b2163b366f41def4d2323891b3f7d1f (patch) | |
| tree | 1604069421543381d34feade623cd260481cfce4 | |
| parent | 9e424cfbf4404ae7c56ff374d66e7391ee9e84c4 (diff) | |
| parent | 6b40cc7f8a01ed31202aaf05c10bfac42ee87539 (diff) | |
Merge "Fix FastScroller for single-section lists and untitled sections."
| -rw-r--r-- | core/java/android/widget/FastScroller.java | 102 |
1 files changed, 61 insertions, 41 deletions
diff --git a/core/java/android/widget/FastScroller.java b/core/java/android/widget/FastScroller.java index 62e157866e81..d452f58b3ea9 100644 --- a/core/java/android/widget/FastScroller.java +++ b/core/java/android/widget/FastScroller.java @@ -274,9 +274,14 @@ class FastScroller { mPreviewPadding = res.getDimensionPixelSize(R.dimen.fastscroll_overlay_padding); + final int textMinSize = Math.max(0, previewSize - mPreviewPadding); mPrimaryText = createPreviewTextView(context, ta); + mPrimaryText.setMinimumWidth(textMinSize); + mPrimaryText.setMinimumHeight(textMinSize); mOverlay.add(mPrimaryText); mSecondaryText = createPreviewTextView(context, ta); + mSecondaryText.setMinimumWidth(textMinSize); + mSecondaryText.setMinimumHeight(textMinSize); mOverlay.add(mSecondaryText); mPreviewResId[PREVIEW_LEFT] = ta.getResourceId(PREVIEW_BACKGROUND_LEFT, 0); @@ -598,7 +603,11 @@ class FastScroller { transitionToVisible(); break; case STATE_DRAGGING: - transitionToDragging(); + if (transitionPreviewLayout(mCurrentSection)) { + transitionToDragging(); + } else { + transitionToVisible(); + } break; } @@ -673,10 +682,6 @@ class FastScroller { mDecorAnimation = new AnimatorSet(); mDecorAnimation.playTogether(fadeIn, slideIn); mDecorAnimation.start(); - - // Ensure the preview text is correct. - final String previewText = getPreviewText(); - transitionPreviewLayout(previewText); } private boolean isLongList(int visibleItemCount, int totalItemCount) { @@ -853,42 +858,35 @@ class FastScroller { sectionIndex = -1; } - if (sectionIndex >= 0 && sectionIndex < sections.length) { - // If we moved sections, display section. - if (mCurrentSection != sectionIndex) { - mCurrentSection = sectionIndex; - final String section = sections[sectionIndex].toString(); + if (mCurrentSection != sectionIndex) { + mCurrentSection = sectionIndex; + + if (transitionPreviewLayout(sectionIndex)) { transitionToDragging(); - transitionPreviewLayout(section); + } else { + transitionToVisible(); } - } else { - // No current section, transition out of preview. - transitionPreviewLayout(null); - transitionToVisible(); } } - private String getPreviewText() { - final Object[] sections = mSections; - if (sections == null) { - return null; - } - - final int sectionIndex = mCurrentSection; - if (sectionIndex < 0 || sectionIndex >= sections.length) { - return null; - } - - return sections[sectionIndex].toString(); - } - /** - * Transitions the preview text to a new value. Handles animation, - * measurement, and layout. + * Transitions the preview text to a new section. Handles animation, + * measurement, and layout. If the new preview text is empty, returns false. * - * @param text The preview text to transition to. + * @param sectionIndex The section index to which the preview should + * transition. + * @return False if the new preview text is empty. */ - private void transitionPreviewLayout(CharSequence text) { + private boolean transitionPreviewLayout(int sectionIndex) { + final Object[] sections = mSections; + String text = null; + if (sections != null && sectionIndex >= 0 && sectionIndex < sections.length) { + final Object section = sections[sectionIndex]; + if (section != null) { + text = section.toString(); + } + } + final Rect bounds = mTempBounds; final ImageView preview = mPreviewImage; final TextView showing; @@ -952,6 +950,8 @@ class FastScroller { } mPreviewAnimation.start(); + + return (text != null && text.length() > 0); } /** @@ -1012,26 +1012,46 @@ class FastScroller { final boolean hasSections = mSectionIndexer != null && mSections != null && mSections.length > 0; if (!hasSections || !mMatchDragPosition) { - return firstVisibleItem / (totalItemCount - visibleItemCount); + return (float) firstVisibleItem / (totalItemCount - visibleItemCount); } + // Ignore headers. firstVisibleItem -= mHeaderCount; if (firstVisibleItem < 0) { return 0; } - totalItemCount -= mHeaderCount; + // Hidden portion of the first visible row. + final View child = mList.getChildAt(0); + final float incrementalPos; + if (child == null || child.getHeight() == 0) { + incrementalPos = 0; + } else { + incrementalPos = (float) (mList.getPaddingTop() - child.getTop()) / child.getHeight(); + } + + // Number of rows in this section. final int section = mSectionIndexer.getSectionForPosition(firstVisibleItem); final int sectionPos = mSectionIndexer.getPositionForSection(section); - final int nextSectionPos = mSectionIndexer.getPositionForSection(section + 1); final int sectionCount = mSections.length; - final int positionsInSection = Math.max(1, nextSectionPos - sectionPos); + final int positionsInSection; + if (section < sectionCount - 1) { + final int nextSectionPos = mSectionIndexer.getPositionForSection(section + 1); + positionsInSection = nextSectionPos - sectionPos; + } else { + positionsInSection = totalItemCount - sectionPos; + } + + // Position within this section. + final float posWithinSection; + if (positionsInSection == 0) { + posWithinSection = 0; + } else { + posWithinSection = (firstVisibleItem + incrementalPos - sectionPos) + / positionsInSection; + } - final View child = mList.getChildAt(0); - final float incrementalPos = child == null ? 0 : firstVisibleItem + - (float) (mList.getPaddingTop() - child.getTop()) / Math.max(1, child.getHeight()); - final float posWithinSection = (incrementalPos - sectionPos) / positionsInSection; return (section + posWithinSection) / sectionCount; } |