The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.AnimatorListenerAdapter; |
| 22 | import android.animation.AnimatorSet; |
| 23 | import android.animation.ObjectAnimator; |
| 24 | import android.animation.PropertyValuesHolder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.res.ColorStateList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 28 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 30 | import android.os.Build; |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 31 | import android.os.SystemClock; |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 32 | import android.text.TextUtils; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 33 | import android.text.TextUtils.TruncateAt; |
| 34 | import android.util.IntProperty; |
| 35 | import android.util.MathUtils; |
| 36 | import android.util.Property; |
Alan Viverette | 7b63063 | 2013-10-04 11:44:51 -0700 | [diff] [blame] | 37 | import android.util.TypedValue; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 38 | import android.view.Gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.view.MotionEvent; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 40 | import android.view.View; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 41 | import android.view.View.MeasureSpec; |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 42 | import android.view.ViewConfiguration; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 43 | import android.view.ViewGroup.LayoutParams; |
| 44 | import android.view.ViewGroupOverlay; |
Adam Powell | d43bd48 | 2010-02-26 16:29:09 -0800 | [diff] [blame] | 45 | import android.widget.AbsListView.OnScrollListener; |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 46 | import android.widget.ImageView.ScaleType; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Helper class for AbsListView to draw and control the Fast Scroll thumb |
| 50 | */ |
| 51 | class FastScroller { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 52 | /** Duration of fade-out animation. */ |
| 53 | private static final int DURATION_FADE_OUT = 300; |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 54 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 55 | /** Duration of fade-in animation. */ |
| 56 | private static final int DURATION_FADE_IN = 150; |
| 57 | |
| 58 | /** Duration of transition cross-fade animation. */ |
| 59 | private static final int DURATION_CROSS_FADE = 50; |
| 60 | |
| 61 | /** Duration of transition resize animation. */ |
| 62 | private static final int DURATION_RESIZE = 100; |
| 63 | |
| 64 | /** Inactivity timeout before fading controls. */ |
| 65 | private static final long FADE_TIMEOUT = 1500; |
| 66 | |
| 67 | /** Minimum number of pages to justify showing a fast scroll thumb. */ |
| 68 | private static final int MIN_PAGES = 4; |
| 69 | |
| 70 | /** Scroll thumb and preview not showing. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | private static final int STATE_NONE = 0; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 72 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 73 | /** Scroll thumb visible and moving along with the scrollbar. */ |
| 74 | private static final int STATE_VISIBLE = 1; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 75 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 76 | /** Scroll thumb and preview being dragged by user. */ |
| 77 | private static final int STATE_DRAGGING = 2; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 78 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 79 | // Positions for preview image and text. |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 80 | private static final int OVERLAY_FLOATING = 0; |
| 81 | private static final int OVERLAY_AT_THUMB = 1; |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 82 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 83 | // Indices for mPreviewResId. |
| 84 | private static final int PREVIEW_LEFT = 0; |
| 85 | private static final int PREVIEW_RIGHT = 1; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 86 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 87 | /** Delay before considering a tap in the thumb area to be a drag. */ |
| 88 | private static final long TAP_TIMEOUT = ViewConfiguration.getTapTimeout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 90 | private final Rect mTempBounds = new Rect(); |
| 91 | private final Rect mTempMargins = new Rect(); |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 92 | private final Rect mContainerRect = new Rect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 94 | private final AbsListView mList; |
| 95 | private final ViewGroupOverlay mOverlay; |
| 96 | private final TextView mPrimaryText; |
| 97 | private final TextView mSecondaryText; |
| 98 | private final ImageView mThumbImage; |
| 99 | private final ImageView mTrackImage; |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 100 | private final View mPreviewImage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Preview image resource IDs for left- and right-aligned layouts. See |
| 104 | * {@link #PREVIEW_LEFT} and {@link #PREVIEW_RIGHT}. |
| 105 | */ |
| 106 | private final int[] mPreviewResId = new int[2]; |
| 107 | |
| 108 | /** |
| 109 | * Padding in pixels around the preview text. Applied as layout margins to |
| 110 | * the preview text and padding to the preview image. |
| 111 | */ |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 112 | private int mPreviewPadding; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 113 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 114 | private int mPreviewMinWidth; |
| 115 | private int mPreviewMinHeight; |
| 116 | private int mThumbMinWidth; |
| 117 | private int mThumbMinHeight; |
| 118 | |
| 119 | /** Theme-specified text size. Used only if text appearance is not set. */ |
| 120 | private float mTextSize; |
| 121 | |
| 122 | /** Theme-specified text color. Used only if text appearance is not set. */ |
| 123 | private ColorStateList mTextColor; |
| 124 | |
| 125 | private Drawable mThumbDrawable; |
| 126 | private Drawable mTrackDrawable; |
| 127 | private int mTextAppearance; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 128 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 129 | /** Total width of decorations. */ |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 130 | private int mWidth; |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 131 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 132 | /** Set containing decoration transition animations. */ |
| 133 | private AnimatorSet mDecorAnimation; |
| 134 | |
| 135 | /** Set containing preview text transition animations. */ |
| 136 | private AnimatorSet mPreviewAnimation; |
| 137 | |
| 138 | /** Whether the primary text is showing. */ |
| 139 | private boolean mShowingPrimary; |
| 140 | |
| 141 | /** Whether we're waiting for completion of scrollTo(). */ |
| 142 | private boolean mScrollCompleted; |
| 143 | |
| 144 | /** The position of the first visible item in the list. */ |
| 145 | private int mFirstVisibleItem; |
| 146 | |
| 147 | /** The number of headers at the top of the view. */ |
| 148 | private int mHeaderCount; |
| 149 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 150 | /** The index of the current section. */ |
| 151 | private int mCurrentSection = -1; |
| 152 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 153 | /** The current scrollbar position. */ |
| 154 | private int mScrollbarPosition = -1; |
| 155 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 156 | /** Whether the list is long enough to need a fast scroller. */ |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 157 | private boolean mLongList; |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 158 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 159 | private Object[] mSections; |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 160 | |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 161 | /** Whether this view is currently performing layout. */ |
| 162 | private boolean mUpdatingLayout; |
| 163 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Current decoration state, one of: |
| 166 | * <ul> |
| 167 | * <li>{@link #STATE_NONE}, nothing visible |
| 168 | * <li>{@link #STATE_VISIBLE}, showing track and thumb |
| 169 | * <li>{@link #STATE_DRAGGING}, visible and showing preview |
| 170 | * </ul> |
| 171 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | private int mState; |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 173 | |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 174 | /** Whether the preview image is visible. */ |
| 175 | private boolean mShowingPreview; |
| 176 | |
Alan Viverette | 4f434c7 | 2013-12-13 11:01:21 -0800 | [diff] [blame] | 177 | private Adapter mListAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | private SectionIndexer mSectionIndexer; |
| 179 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 180 | /** Whether decorations should be laid out from right to left. */ |
| 181 | private boolean mLayoutFromRight; |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 182 | |
Alan Viverette | 447cdf2 | 2013-07-15 17:47:34 -0700 | [diff] [blame] | 183 | /** Whether the fast scroller is enabled. */ |
| 184 | private boolean mEnabled; |
| 185 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 186 | /** Whether the scrollbar and decorations should always be shown. */ |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 187 | private boolean mAlwaysShow; |
| 188 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Position for the preview image and text. One of: |
| 191 | * <ul> |
| 192 | * <li>{@link #OVERLAY_AT_THUMB} |
| 193 | * <li>{@link #OVERLAY_FLOATING} |
| 194 | * </ul> |
| 195 | */ |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 196 | private int mOverlayPosition; |
| 197 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 198 | /** Current scrollbar style, including inset and overlay properties. */ |
| 199 | private int mScrollBarStyle; |
| 200 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 201 | /** Whether to precisely match the thumb position to the list. */ |
Adam Powell | 568ccd8 | 2011-08-03 22:38:48 -0700 | [diff] [blame] | 202 | private boolean mMatchDragPosition; |
| 203 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 204 | private float mInitialTouchY; |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 205 | private long mPendingDrag = -1; |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 206 | private int mScaledTouchSlop; |
| 207 | |
Alan Viverette | 4b95cc7 | 2014-01-14 16:54:02 -0800 | [diff] [blame] | 208 | private int mOldItemCount; |
| 209 | private int mOldChildCount; |
| 210 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Used to delay hiding fast scroll decorations. |
| 213 | */ |
| 214 | private final Runnable mDeferHide = new Runnable() { |
| 215 | @Override |
| 216 | public void run() { |
| 217 | setState(STATE_NONE); |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | /** |
| 222 | * Used to effect a transition from primary to secondary text. |
| 223 | */ |
| 224 | private final AnimatorListener mSwitchPrimaryListener = new AnimatorListenerAdapter() { |
| 225 | @Override |
| 226 | public void onAnimationEnd(Animator animation) { |
| 227 | mShowingPrimary = !mShowingPrimary; |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 228 | } |
| 229 | }; |
| 230 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 231 | public FastScroller(AbsListView listView, int styleResId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | mList = listView; |
Alan Viverette | 4b95cc7 | 2014-01-14 16:54:02 -0800 | [diff] [blame] | 233 | mOldItemCount = listView.getCount(); |
| 234 | mOldChildCount = listView.getChildCount(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 235 | |
Alan Viverette | 39bed69 | 2013-08-07 15:47:04 -0700 | [diff] [blame] | 236 | final Context context = listView.getContext(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 237 | mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 238 | mScrollBarStyle = listView.getScrollBarStyle(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 239 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 240 | mScrollCompleted = true; |
| 241 | mState = STATE_VISIBLE; |
| 242 | mMatchDragPosition = |
| 243 | context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 244 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 245 | mTrackImage = new ImageView(context); |
| 246 | mTrackImage.setScaleType(ScaleType.FIT_XY); |
| 247 | mThumbImage = new ImageView(context); |
| 248 | mThumbImage.setScaleType(ScaleType.FIT_XY); |
| 249 | mPreviewImage = new View(context); |
| 250 | mPreviewImage.setAlpha(0f); |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 251 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 252 | mPrimaryText = createPreviewTextView(context); |
| 253 | mSecondaryText = createPreviewTextView(context); |
| 254 | |
| 255 | setStyle(styleResId); |
| 256 | |
| 257 | final ViewGroupOverlay overlay = listView.getOverlay(); |
| 258 | mOverlay = overlay; |
| 259 | overlay.add(mTrackImage); |
| 260 | overlay.add(mThumbImage); |
| 261 | overlay.add(mPreviewImage); |
| 262 | overlay.add(mPrimaryText); |
| 263 | overlay.add(mSecondaryText); |
| 264 | |
| 265 | getSectionsFromIndexer(); |
Alan Viverette | 4b95cc7 | 2014-01-14 16:54:02 -0800 | [diff] [blame] | 266 | updateLongList(mOldChildCount, mOldItemCount); |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 267 | setScrollbarPosition(listView.getVerticalScrollbarPosition()); |
| 268 | postAutoHide(); |
| 269 | } |
| 270 | |
| 271 | private void updateAppearance() { |
| 272 | final Context context = mList.getContext(); |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 273 | int width = 0; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 274 | |
| 275 | // Add track to overlay if it has an image. |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 276 | mTrackImage.setImageDrawable(mTrackDrawable); |
| 277 | if (mTrackDrawable != null) { |
| 278 | width = Math.max(width, mTrackDrawable.getIntrinsicWidth()); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 281 | // Add thumb to overlay if it has an image. |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 282 | mThumbImage.setImageDrawable(mThumbDrawable); |
| 283 | mThumbImage.setMinimumWidth(mThumbMinWidth); |
| 284 | mThumbImage.setMinimumHeight(mThumbMinHeight); |
| 285 | if (mThumbDrawable != null) { |
| 286 | width = Math.max(width, mThumbDrawable.getIntrinsicWidth()); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 289 | // Account for minimum thumb width. |
| 290 | mWidth = Math.max(width, mThumbMinWidth); |
| 291 | |
| 292 | mPreviewImage.setMinimumWidth(mPreviewMinWidth); |
| 293 | mPreviewImage.setMinimumHeight(mPreviewMinHeight); |
| 294 | |
| 295 | if (mTextAppearance != 0) { |
| 296 | mPrimaryText.setTextAppearance(context, mTextAppearance); |
| 297 | mSecondaryText.setTextAppearance(context, mTextAppearance); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 300 | if (mTextColor != null) { |
| 301 | mPrimaryText.setTextColor(mTextColor); |
| 302 | mSecondaryText.setTextColor(mTextColor); |
| 303 | } |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 304 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 305 | if (mTextSize > 0) { |
| 306 | mPrimaryText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); |
| 307 | mSecondaryText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); |
| 308 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 309 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 310 | final int textMinSize = Math.max(0, mPreviewMinHeight); |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 311 | mPrimaryText.setMinimumWidth(textMinSize); |
| 312 | mPrimaryText.setMinimumHeight(textMinSize); |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 313 | mSecondaryText.setMinimumWidth(textMinSize); |
| 314 | mSecondaryText.setMinimumHeight(textMinSize); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 315 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 316 | refreshDrawablePressedState(); |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | public void setStyle(int resId) { |
| 320 | final Context context = mList.getContext(); |
| 321 | final TypedArray ta = context.obtainStyledAttributes(null, |
| 322 | com.android.internal.R.styleable.FastScroll, android.R.attr.fastScrollStyle, resId); |
| 323 | final int N = ta.getIndexCount(); |
| 324 | for (int i = 0; i < N; i++) { |
| 325 | final int index = ta.getIndex(i); |
| 326 | switch (index) { |
| 327 | case com.android.internal.R.styleable.FastScroll_position: |
| 328 | mOverlayPosition = ta.getInt(index, OVERLAY_FLOATING); |
| 329 | break; |
| 330 | case com.android.internal.R.styleable.FastScroll_backgroundLeft: |
| 331 | mPreviewResId[PREVIEW_LEFT] = ta.getResourceId(index, 0); |
| 332 | break; |
| 333 | case com.android.internal.R.styleable.FastScroll_backgroundRight: |
| 334 | mPreviewResId[PREVIEW_RIGHT] = ta.getResourceId(index, 0); |
| 335 | break; |
| 336 | case com.android.internal.R.styleable.FastScroll_thumbDrawable: |
| 337 | mThumbDrawable = ta.getDrawable(index); |
| 338 | break; |
| 339 | case com.android.internal.R.styleable.FastScroll_trackDrawable: |
| 340 | mTrackDrawable = ta.getDrawable(index); |
| 341 | break; |
| 342 | case com.android.internal.R.styleable.FastScroll_textAppearance: |
| 343 | mTextAppearance = ta.getResourceId(index, 0); |
| 344 | break; |
| 345 | case com.android.internal.R.styleable.FastScroll_textColor: |
| 346 | mTextColor = ta.getColorStateList(index); |
| 347 | break; |
| 348 | case com.android.internal.R.styleable.FastScroll_textSize: |
| 349 | mTextSize = ta.getDimensionPixelSize(index, 0); |
| 350 | break; |
| 351 | case com.android.internal.R.styleable.FastScroll_minWidth: |
| 352 | mPreviewMinWidth = ta.getDimensionPixelSize(index, 0); |
| 353 | break; |
| 354 | case com.android.internal.R.styleable.FastScroll_minHeight: |
| 355 | mPreviewMinHeight = ta.getDimensionPixelSize(index, 0); |
| 356 | break; |
| 357 | case com.android.internal.R.styleable.FastScroll_thumbMinWidth: |
| 358 | mThumbMinWidth = ta.getDimensionPixelSize(index, 0); |
| 359 | break; |
| 360 | case com.android.internal.R.styleable.FastScroll_thumbMinHeight: |
| 361 | mThumbMinHeight = ta.getDimensionPixelSize(index, 0); |
| 362 | break; |
| 363 | case com.android.internal.R.styleable.FastScroll_padding: |
| 364 | mPreviewPadding = ta.getDimensionPixelSize(index, 0); |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | updateAppearance(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 372 | /** |
Alan Viverette | 447cdf2 | 2013-07-15 17:47:34 -0700 | [diff] [blame] | 373 | * Removes this FastScroller overlay from the host view. |
| 374 | */ |
| 375 | public void remove() { |
| 376 | mOverlay.remove(mTrackImage); |
| 377 | mOverlay.remove(mThumbImage); |
| 378 | mOverlay.remove(mPreviewImage); |
| 379 | mOverlay.remove(mPrimaryText); |
| 380 | mOverlay.remove(mSecondaryText); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @param enabled Whether the fast scroll thumb is enabled. |
| 385 | */ |
| 386 | public void setEnabled(boolean enabled) { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 387 | if (mEnabled != enabled) { |
| 388 | mEnabled = enabled; |
Alan Viverette | 447cdf2 | 2013-07-15 17:47:34 -0700 | [diff] [blame] | 389 | |
Alan Viverette | 8ac22b0 | 2013-12-12 18:35:40 -0800 | [diff] [blame] | 390 | onStateDependencyChanged(true); |
Alan Viverette | 447cdf2 | 2013-07-15 17:47:34 -0700 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * @return Whether the fast scroll thumb is enabled. |
| 396 | */ |
| 397 | public boolean isEnabled() { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 398 | return mEnabled && (mLongList || mAlwaysShow); |
Alan Viverette | 447cdf2 | 2013-07-15 17:47:34 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 402 | * @param alwaysShow Whether the fast scroll thumb should always be shown |
| 403 | */ |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 404 | public void setAlwaysShow(boolean alwaysShow) { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 405 | if (mAlwaysShow != alwaysShow) { |
| 406 | mAlwaysShow = alwaysShow; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 407 | |
Alan Viverette | 8ac22b0 | 2013-12-12 18:35:40 -0800 | [diff] [blame] | 408 | onStateDependencyChanged(false); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 412 | /** |
| 413 | * @return Whether the fast scroll thumb will always be shown |
| 414 | * @see #setAlwaysShow(boolean) |
| 415 | */ |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 416 | public boolean isAlwaysShowEnabled() { |
| 417 | return mAlwaysShow; |
| 418 | } |
| 419 | |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 420 | /** |
| 421 | * Called when one of the variables affecting enabled state changes. |
Alan Viverette | 8ac22b0 | 2013-12-12 18:35:40 -0800 | [diff] [blame] | 422 | * |
| 423 | * @param peekIfEnabled whether the thumb should peek, if enabled |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 424 | */ |
Alan Viverette | 8ac22b0 | 2013-12-12 18:35:40 -0800 | [diff] [blame] | 425 | private void onStateDependencyChanged(boolean peekIfEnabled) { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 426 | if (isEnabled()) { |
| 427 | if (isAlwaysShowEnabled()) { |
| 428 | setState(STATE_VISIBLE); |
| 429 | } else if (mState == STATE_VISIBLE) { |
| 430 | postAutoHide(); |
Alan Viverette | 8ac22b0 | 2013-12-12 18:35:40 -0800 | [diff] [blame] | 431 | } else if (peekIfEnabled) { |
| 432 | setState(STATE_VISIBLE); |
| 433 | postAutoHide(); |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 434 | } |
| 435 | } else { |
| 436 | stop(); |
| 437 | } |
| 438 | |
| 439 | mList.resolvePadding(); |
| 440 | } |
| 441 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 442 | public void setScrollBarStyle(int style) { |
| 443 | if (mScrollBarStyle != style) { |
| 444 | mScrollBarStyle = style; |
| 445 | |
| 446 | updateLayout(); |
| 447 | } |
| 448 | } |
| 449 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 450 | /** |
| 451 | * Immediately transitions the fast scroller decorations to a hidden state. |
| 452 | */ |
| 453 | public void stop() { |
| 454 | setState(STATE_NONE); |
| 455 | } |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 456 | |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 457 | public void setScrollbarPosition(int position) { |
Fabrice Di Meglio | c23ee46 | 2012-06-22 18:46:06 -0700 | [diff] [blame] | 458 | if (position == View.SCROLLBAR_POSITION_DEFAULT) { |
| 459 | position = mList.isLayoutRtl() ? |
| 460 | View.SCROLLBAR_POSITION_LEFT : View.SCROLLBAR_POSITION_RIGHT; |
| 461 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 462 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 463 | if (mScrollbarPosition != position) { |
| 464 | mScrollbarPosition = position; |
| 465 | mLayoutFromRight = position != View.SCROLLBAR_POSITION_LEFT; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 466 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 467 | final int previewResId = mPreviewResId[mLayoutFromRight ? PREVIEW_RIGHT : PREVIEW_LEFT]; |
| 468 | mPreviewImage.setBackgroundResource(previewResId); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 469 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 470 | // Add extra padding for text. |
| 471 | final Drawable background = mPreviewImage.getBackground(); |
| 472 | if (background != null) { |
| 473 | final Rect padding = mTempBounds; |
| 474 | background.getPadding(padding); |
| 475 | padding.offset(mPreviewPadding, mPreviewPadding); |
| 476 | mPreviewImage.setPadding(padding.left, padding.top, padding.right, padding.bottom); |
| 477 | } |
| 478 | |
| 479 | // Requires re-layout. |
| 480 | updateLayout(); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | public int getWidth() { |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 485 | return mWidth; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 488 | public void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 489 | updateLayout(); |
| 490 | } |
| 491 | |
Alan Viverette | 4b95cc7 | 2014-01-14 16:54:02 -0800 | [diff] [blame] | 492 | public void onItemCountChanged(int childCount, int itemCount) { |
| 493 | if (mOldItemCount != itemCount || mOldChildCount != childCount) { |
| 494 | mOldItemCount = itemCount; |
| 495 | mOldChildCount = childCount; |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 496 | |
Alan Viverette | 4b95cc7 | 2014-01-14 16:54:02 -0800 | [diff] [blame] | 497 | final boolean hasMoreItems = itemCount - childCount > 0; |
| 498 | if (hasMoreItems && mState != STATE_DRAGGING) { |
| 499 | final int firstVisibleItem = mList.getFirstVisiblePosition(); |
| 500 | setThumbPos(getPosFromItemCount(firstVisibleItem, childCount, itemCount)); |
| 501 | } |
| 502 | |
| 503 | updateLongList(childCount, itemCount); |
| 504 | } |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Alan Viverette | 4b95cc7 | 2014-01-14 16:54:02 -0800 | [diff] [blame] | 507 | private void updateLongList(int childCount, int itemCount) { |
| 508 | final boolean longList = childCount > 0 && itemCount / childCount >= MIN_PAGES; |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 509 | if (mLongList != longList) { |
| 510 | mLongList = longList; |
| 511 | |
Alan Viverette | 8ac22b0 | 2013-12-12 18:35:40 -0800 | [diff] [blame] | 512 | onStateDependencyChanged(false); |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 513 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 515 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 516 | /** |
| 517 | * Creates a view into which preview text can be placed. |
| 518 | */ |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 519 | private TextView createPreviewTextView(Context context) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 520 | final LayoutParams params = new LayoutParams( |
| 521 | LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 522 | final TextView textView = new TextView(context); |
| 523 | textView.setLayoutParams(params); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 524 | textView.setSingleLine(true); |
| 525 | textView.setEllipsize(TruncateAt.MIDDLE); |
| 526 | textView.setGravity(Gravity.CENTER); |
| 527 | textView.setAlpha(0f); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 528 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 529 | // Manually propagate inherited layout direction. |
| 530 | textView.setLayoutDirection(mList.getLayoutDirection()); |
NoraBora | 9b38c60 | 2010-10-12 06:59:55 -0700 | [diff] [blame] | 531 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 532 | return textView; |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Measures and layouts the scrollbar and decorations. |
| 537 | */ |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 538 | public void updateLayout() { |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 539 | // Prevent re-entry when RTL properties change as a side-effect of |
| 540 | // resolving padding. |
| 541 | if (mUpdatingLayout) { |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | mUpdatingLayout = true; |
| 546 | |
| 547 | updateContainerRect(); |
| 548 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 549 | layoutThumb(); |
| 550 | layoutTrack(); |
| 551 | |
| 552 | final Rect bounds = mTempBounds; |
| 553 | measurePreview(mPrimaryText, bounds); |
| 554 | applyLayout(mPrimaryText, bounds); |
| 555 | measurePreview(mSecondaryText, bounds); |
| 556 | applyLayout(mSecondaryText, bounds); |
| 557 | |
| 558 | if (mPreviewImage != null) { |
| 559 | // Apply preview image padding. |
| 560 | bounds.left -= mPreviewImage.getPaddingLeft(); |
| 561 | bounds.top -= mPreviewImage.getPaddingTop(); |
| 562 | bounds.right += mPreviewImage.getPaddingRight(); |
| 563 | bounds.bottom += mPreviewImage.getPaddingBottom(); |
| 564 | applyLayout(mPreviewImage, bounds); |
| 565 | } |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 566 | |
| 567 | mUpdatingLayout = false; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Layouts a view within the specified bounds and pins the pivot point to |
| 572 | * the appropriate edge. |
| 573 | * |
| 574 | * @param view The view to layout. |
| 575 | * @param bounds Bounds at which to layout the view. |
| 576 | */ |
| 577 | private void applyLayout(View view, Rect bounds) { |
| 578 | view.layout(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 579 | view.setPivotX(mLayoutFromRight ? bounds.right - bounds.left : 0); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Measures the preview text bounds, taking preview image padding into |
| 584 | * account. This method should only be called after {@link #layoutThumb()} |
| 585 | * and {@link #layoutTrack()} have both been called at least once. |
| 586 | * |
| 587 | * @param v The preview text view to measure. |
| 588 | * @param out Rectangle into which measured bounds are placed. |
| 589 | */ |
| 590 | private void measurePreview(View v, Rect out) { |
| 591 | // Apply the preview image's padding as layout margins. |
| 592 | final Rect margins = mTempMargins; |
| 593 | margins.left = mPreviewImage.getPaddingLeft(); |
| 594 | margins.top = mPreviewImage.getPaddingTop(); |
| 595 | margins.right = mPreviewImage.getPaddingRight(); |
| 596 | margins.bottom = mPreviewImage.getPaddingBottom(); |
| 597 | |
| 598 | if (mOverlayPosition == OVERLAY_AT_THUMB) { |
| 599 | measureViewToSide(v, mThumbImage, margins, out); |
| 600 | } else { |
| 601 | measureFloating(v, margins, out); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Measures the bounds for a view that should be laid out against the edge |
| 607 | * of an adjacent view. If no adjacent view is provided, lays out against |
| 608 | * the list edge. |
| 609 | * |
| 610 | * @param view The view to measure for layout. |
| 611 | * @param adjacent (Optional) The adjacent view, may be null to align to the |
| 612 | * list edge. |
| 613 | * @param margins Layout margins to apply to the view. |
| 614 | * @param out Rectangle into which measured bounds are placed. |
| 615 | */ |
| 616 | private void measureViewToSide(View view, View adjacent, Rect margins, Rect out) { |
| 617 | final int marginLeft; |
| 618 | final int marginTop; |
| 619 | final int marginRight; |
| 620 | if (margins == null) { |
| 621 | marginLeft = 0; |
| 622 | marginTop = 0; |
| 623 | marginRight = 0; |
| 624 | } else { |
| 625 | marginLeft = margins.left; |
| 626 | marginTop = margins.top; |
| 627 | marginRight = margins.right; |
NoraBora | 9b38c60 | 2010-10-12 06:59:55 -0700 | [diff] [blame] | 628 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 629 | |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 630 | final Rect container = mContainerRect; |
| 631 | final int containerWidth = container.width(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 632 | final int maxWidth; |
| 633 | if (adjacent == null) { |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 634 | maxWidth = containerWidth; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 635 | } else if (mLayoutFromRight) { |
| 636 | maxWidth = adjacent.getLeft(); |
| 637 | } else { |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 638 | maxWidth = containerWidth - adjacent.getRight(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 639 | } |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 640 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 641 | final int adjMaxWidth = maxWidth - marginLeft - marginRight; |
| 642 | final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(adjMaxWidth, MeasureSpec.AT_MOST); |
| 643 | final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 644 | view.measure(widthMeasureSpec, heightMeasureSpec); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 645 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 646 | // Align to the left or right. |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 647 | final int width = Math.min(adjMaxWidth, view.getMeasuredWidth()); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 648 | final int left; |
| 649 | final int right; |
| 650 | if (mLayoutFromRight) { |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 651 | right = (adjacent == null ? container.right : adjacent.getLeft()) - marginRight; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 652 | left = right - width; |
| 653 | } else { |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 654 | left = (adjacent == null ? container.left : adjacent.getRight()) + marginLeft; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 655 | right = left + width; |
| 656 | } |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 657 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 658 | // Don't adjust the vertical position. |
| 659 | final int top = marginTop; |
| 660 | final int bottom = top + view.getMeasuredHeight(); |
| 661 | out.set(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 663 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 664 | private void measureFloating(View preview, Rect margins, Rect out) { |
| 665 | final int marginLeft; |
| 666 | final int marginTop; |
| 667 | final int marginRight; |
| 668 | if (margins == null) { |
| 669 | marginLeft = 0; |
| 670 | marginTop = 0; |
| 671 | marginRight = 0; |
| 672 | } else { |
| 673 | marginLeft = margins.left; |
| 674 | marginTop = margins.top; |
| 675 | marginRight = margins.right; |
| 676 | } |
| 677 | |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 678 | final Rect container = mContainerRect; |
| 679 | final int containerWidth = container.width(); |
| 680 | final int adjMaxWidth = containerWidth - marginLeft - marginRight; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 681 | final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(adjMaxWidth, MeasureSpec.AT_MOST); |
| 682 | final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 683 | preview.measure(widthMeasureSpec, heightMeasureSpec); |
| 684 | |
| 685 | // Align at the vertical center, 10% from the top. |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 686 | final int containerHeight = container.height(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 687 | final int width = preview.getMeasuredWidth(); |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 688 | final int top = containerHeight / 10 + marginTop + container.top; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 689 | final int bottom = top + preview.getMeasuredHeight(); |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 690 | final int left = (containerWidth - width) / 2 + container.left; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 691 | final int right = left + width; |
| 692 | out.set(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 693 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 694 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 695 | /** |
| 696 | * Updates the container rectangle used for layout. |
| 697 | */ |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 698 | private void updateContainerRect() { |
| 699 | final AbsListView list = mList; |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 700 | list.resolvePadding(); |
| 701 | |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 702 | final Rect container = mContainerRect; |
| 703 | container.left = 0; |
| 704 | container.top = 0; |
| 705 | container.right = list.getWidth(); |
| 706 | container.bottom = list.getHeight(); |
| 707 | |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 708 | final int scrollbarStyle = mScrollBarStyle; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 709 | if (scrollbarStyle == View.SCROLLBARS_INSIDE_INSET |
| 710 | || scrollbarStyle == View.SCROLLBARS_INSIDE_OVERLAY) { |
| 711 | container.left += list.getPaddingLeft(); |
| 712 | container.top += list.getPaddingTop(); |
| 713 | container.right -= list.getPaddingRight(); |
| 714 | container.bottom -= list.getPaddingBottom(); |
Alan Viverette | 26bb253 | 2013-08-09 10:40:50 -0700 | [diff] [blame] | 715 | |
| 716 | // In inset mode, we need to adjust for padded scrollbar width. |
| 717 | if (scrollbarStyle == View.SCROLLBARS_INSIDE_INSET) { |
| 718 | final int width = getWidth(); |
| 719 | if (mScrollbarPosition == View.SCROLLBAR_POSITION_RIGHT) { |
| 720 | container.right += width; |
| 721 | } else { |
| 722 | container.left -= width; |
| 723 | } |
| 724 | } |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 728 | /** |
| 729 | * Lays out the thumb according to the current scrollbar position. |
| 730 | */ |
| 731 | private void layoutThumb() { |
| 732 | final Rect bounds = mTempBounds; |
| 733 | measureViewToSide(mThumbImage, null, null, bounds); |
| 734 | applyLayout(mThumbImage, bounds); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 736 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 737 | /** |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 738 | * Lays out the track centered on the thumb. Must be called after |
| 739 | * {@link #layoutThumb}. |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 740 | */ |
| 741 | private void layoutTrack() { |
| 742 | final View track = mTrackImage; |
| 743 | final View thumb = mThumbImage; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 744 | final Rect container = mContainerRect; |
| 745 | final int containerWidth = container.width(); |
| 746 | final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(containerWidth, MeasureSpec.AT_MOST); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 747 | final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 748 | track.measure(widthMeasureSpec, heightMeasureSpec); |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 749 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 750 | final int trackWidth = track.getMeasuredWidth(); |
| 751 | final int thumbHalfHeight = thumb == null ? 0 : thumb.getHeight() / 2; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 752 | final int left = thumb.getLeft() + (thumb.getWidth() - trackWidth) / 2; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 753 | final int right = left + trackWidth; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 754 | final int top = container.top + thumbHalfHeight; |
| 755 | final int bottom = container.bottom - thumbHalfHeight; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 756 | track.layout(left, top, right, bottom); |
| 757 | } |
| 758 | |
| 759 | private void setState(int state) { |
| 760 | mList.removeCallbacks(mDeferHide); |
| 761 | |
| 762 | if (mAlwaysShow && state == STATE_NONE) { |
| 763 | state = STATE_VISIBLE; |
| 764 | } |
| 765 | |
| 766 | if (state == mState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | return; |
| 768 | } |
| 769 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 770 | switch (state) { |
| 771 | case STATE_NONE: |
| 772 | transitionToHidden(); |
| 773 | break; |
| 774 | case STATE_VISIBLE: |
| 775 | transitionToVisible(); |
| 776 | break; |
| 777 | case STATE_DRAGGING: |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 778 | if (transitionPreviewLayout(mCurrentSection)) { |
| 779 | transitionToDragging(); |
| 780 | } else { |
| 781 | transitionToVisible(); |
| 782 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 783 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 786 | mState = state; |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 787 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 788 | refreshDrawablePressedState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | } |
| 790 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 791 | private void refreshDrawablePressedState() { |
| 792 | final boolean isPressed = mState == STATE_DRAGGING; |
| 793 | mThumbImage.setPressed(isPressed); |
| 794 | mTrackImage.setPressed(isPressed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | } |
Adam Powell | 2c6196a | 2010-12-10 14:31:54 -0800 | [diff] [blame] | 796 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 797 | /** |
| 798 | * Shows nothing. |
| 799 | */ |
| 800 | private void transitionToHidden() { |
| 801 | if (mDecorAnimation != null) { |
| 802 | mDecorAnimation.cancel(); |
Adam Powell | 2c6196a | 2010-12-10 14:31:54 -0800 | [diff] [blame] | 803 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 804 | |
| 805 | final Animator fadeOut = groupAnimatorOfFloat(View.ALPHA, 0f, mThumbImage, mTrackImage, |
| 806 | mPreviewImage, mPrimaryText, mSecondaryText).setDuration(DURATION_FADE_OUT); |
| 807 | |
| 808 | // Push the thumb and track outside the list bounds. |
| 809 | final float offset = mLayoutFromRight ? mThumbImage.getWidth() : -mThumbImage.getWidth(); |
| 810 | final Animator slideOut = groupAnimatorOfFloat( |
| 811 | View.TRANSLATION_X, offset, mThumbImage, mTrackImage) |
| 812 | .setDuration(DURATION_FADE_OUT); |
| 813 | |
| 814 | mDecorAnimation = new AnimatorSet(); |
| 815 | mDecorAnimation.playTogether(fadeOut, slideOut); |
| 816 | mDecorAnimation.start(); |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 817 | |
| 818 | mShowingPreview = false; |
Adam Powell | 2c6196a | 2010-12-10 14:31:54 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 821 | /** |
| 822 | * Shows the thumb and track. |
| 823 | */ |
| 824 | private void transitionToVisible() { |
| 825 | if (mDecorAnimation != null) { |
| 826 | mDecorAnimation.cancel(); |
| 827 | } |
| 828 | |
| 829 | final Animator fadeIn = groupAnimatorOfFloat(View.ALPHA, 1f, mThumbImage, mTrackImage) |
| 830 | .setDuration(DURATION_FADE_IN); |
| 831 | final Animator fadeOut = groupAnimatorOfFloat( |
| 832 | View.ALPHA, 0f, mPreviewImage, mPrimaryText, mSecondaryText) |
| 833 | .setDuration(DURATION_FADE_OUT); |
| 834 | final Animator slideIn = groupAnimatorOfFloat( |
| 835 | View.TRANSLATION_X, 0f, mThumbImage, mTrackImage).setDuration(DURATION_FADE_IN); |
| 836 | |
| 837 | mDecorAnimation = new AnimatorSet(); |
| 838 | mDecorAnimation.playTogether(fadeIn, fadeOut, slideIn); |
| 839 | mDecorAnimation.start(); |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 840 | |
| 841 | mShowingPreview = false; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /** |
| 845 | * Shows the thumb, preview, and track. |
| 846 | */ |
| 847 | private void transitionToDragging() { |
| 848 | if (mDecorAnimation != null) { |
| 849 | mDecorAnimation.cancel(); |
| 850 | } |
| 851 | |
| 852 | final Animator fadeIn = groupAnimatorOfFloat( |
| 853 | View.ALPHA, 1f, mThumbImage, mTrackImage, mPreviewImage) |
| 854 | .setDuration(DURATION_FADE_IN); |
| 855 | final Animator slideIn = groupAnimatorOfFloat( |
| 856 | View.TRANSLATION_X, 0f, mThumbImage, mTrackImage).setDuration(DURATION_FADE_IN); |
| 857 | |
| 858 | mDecorAnimation = new AnimatorSet(); |
| 859 | mDecorAnimation.playTogether(fadeIn, slideIn); |
| 860 | mDecorAnimation.start(); |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 861 | |
| 862 | mShowingPreview = true; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Alan Viverette | a709b37 | 2013-07-25 14:43:24 -0700 | [diff] [blame] | 865 | private void postAutoHide() { |
| 866 | mList.removeCallbacks(mDeferHide); |
| 867 | mList.postDelayed(mDeferHide, FADE_TIMEOUT); |
| 868 | } |
| 869 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 870 | public void onScroll(int firstVisibleItem, int visibleItemCount, int totalItemCount) { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 871 | if (!isEnabled()) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 872 | setState(STATE_NONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | return; |
| 874 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 875 | |
| 876 | final boolean hasMoreItems = totalItemCount - visibleItemCount > 0; |
| 877 | if (hasMoreItems && mState != STATE_DRAGGING) { |
| 878 | setThumbPos(getPosFromItemCount(firstVisibleItem, visibleItemCount, totalItemCount)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 880 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | mScrollCompleted = true; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 882 | |
| 883 | if (mFirstVisibleItem != firstVisibleItem) { |
| 884 | mFirstVisibleItem = firstVisibleItem; |
| 885 | |
| 886 | // Show the thumb, if necessary, and set up auto-fade. |
| 887 | if (mState != STATE_DRAGGING) { |
| 888 | setState(STATE_VISIBLE); |
Alan Viverette | a709b37 | 2013-07-25 14:43:24 -0700 | [diff] [blame] | 889 | postAutoHide(); |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 890 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 894 | private void getSectionsFromIndexer() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | mSectionIndexer = null; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 896 | |
| 897 | Adapter adapter = mList.getAdapter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | if (adapter instanceof HeaderViewListAdapter) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 899 | mHeaderCount = ((HeaderViewListAdapter) adapter).getHeadersCount(); |
| 900 | adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 902 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | if (adapter instanceof ExpandableListConnector) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 904 | final ExpandableListAdapter expAdapter = ((ExpandableListConnector) adapter) |
| 905 | .getAdapter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | if (expAdapter instanceof SectionIndexer) { |
| 907 | mSectionIndexer = (SectionIndexer) expAdapter; |
Alan Viverette | 4f434c7 | 2013-12-13 11:01:21 -0800 | [diff] [blame] | 908 | mListAdapter = adapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | mSections = mSectionIndexer.getSections(); |
| 910 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 911 | } else if (adapter instanceof SectionIndexer) { |
Alan Viverette | 4f434c7 | 2013-12-13 11:01:21 -0800 | [diff] [blame] | 912 | mListAdapter = adapter; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 913 | mSectionIndexer = (SectionIndexer) adapter; |
| 914 | mSections = mSectionIndexer.getSections(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | } else { |
Alan Viverette | 4f434c7 | 2013-12-13 11:01:21 -0800 | [diff] [blame] | 916 | mListAdapter = adapter; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 917 | mSections = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
Adam Powell | b1f498a | 2011-01-18 20:43:23 -0800 | [diff] [blame] | 921 | public void onSectionsChanged() { |
| 922 | mListAdapter = null; |
| 923 | } |
| 924 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 925 | /** |
| 926 | * Scrolls to a specific position within the section |
| 927 | * @param position |
| 928 | */ |
| 929 | private void scrollTo(float position) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | mScrollCompleted = false; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 931 | |
| 932 | final int count = mList.getCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | final Object[] sections = mSections; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 934 | final int sectionCount = sections == null ? 0 : sections.length; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | int sectionIndex; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 936 | if (sections != null && sectionCount > 1) { |
| 937 | final int exactSection = MathUtils.constrain( |
| 938 | (int) (position * sectionCount), 0, sectionCount - 1); |
| 939 | int targetSection = exactSection; |
| 940 | int targetIndex = mSectionIndexer.getPositionForSection(targetSection); |
| 941 | sectionIndex = targetSection; |
| 942 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | // Given the expected section and index, the following code will |
| 944 | // try to account for missing sections (no names starting with..) |
| 945 | // It will compute the scroll space of surrounding empty sections |
| 946 | // and interpolate the currently visible letter's range across the |
| 947 | // available space, so that there is always some list movement while |
| 948 | // the user moves the thumb. |
| 949 | int nextIndex = count; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 950 | int prevIndex = targetIndex; |
| 951 | int prevSection = targetSection; |
| 952 | int nextSection = targetSection + 1; |
| 953 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | // Assume the next section is unique |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 955 | if (targetSection < sectionCount - 1) { |
| 956 | nextIndex = mSectionIndexer.getPositionForSection(targetSection + 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 958 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | // Find the previous index if we're slicing the previous section |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 960 | if (nextIndex == targetIndex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | // Non-existent letter |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 962 | while (targetSection > 0) { |
| 963 | targetSection--; |
| 964 | prevIndex = mSectionIndexer.getPositionForSection(targetSection); |
| 965 | if (prevIndex != targetIndex) { |
| 966 | prevSection = targetSection; |
| 967 | sectionIndex = targetSection; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 968 | break; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 969 | } else if (targetSection == 0) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 970 | // When section reaches 0 here, sectionIndex must follow it. |
| 971 | // Assuming mSectionIndexer.getPositionForSection(0) == 0. |
| 972 | sectionIndex = 0; |
| 973 | break; |
| 974 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | } |
| 976 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | // Find the next index, in case the assumed next index is not |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 979 | // unique. For instance, if there is no P, then request for P's |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | // position actually returns Q's. So we need to look ahead to make |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 981 | // sure that there is really a Q at Q's position. If not, move |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | // further down... |
| 983 | int nextNextSection = nextSection + 1; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 984 | while (nextNextSection < sectionCount && |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | mSectionIndexer.getPositionForSection(nextNextSection) == nextIndex) { |
| 986 | nextNextSection++; |
| 987 | nextSection++; |
| 988 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 989 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | // Compute the beginning and ending scroll range percentage of the |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 991 | // currently visible section. This could be equal to or greater than |
| 992 | // (1 / nSections). If the target position is near the previous |
| 993 | // position, snap to the previous position. |
| 994 | final float prevPosition = (float) prevSection / sectionCount; |
| 995 | final float nextPosition = (float) nextSection / sectionCount; |
| 996 | final float snapThreshold = (count == 0) ? Float.MAX_VALUE : .125f / count; |
| 997 | if (prevSection == exactSection && position - prevPosition < snapThreshold) { |
| 998 | targetIndex = prevIndex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | } else { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1000 | targetIndex = prevIndex + (int) ((nextIndex - prevIndex) * (position - prevPosition) |
| 1001 | / (nextPosition - prevPosition)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1003 | |
| 1004 | // Clamp to valid positions. |
| 1005 | targetIndex = MathUtils.constrain(targetIndex, 0, count - 1); |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 1006 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | if (mList instanceof ExpandableListView) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1008 | final ExpandableListView expList = (ExpandableListView) mList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | expList.setSelectionFromTop(expList.getFlatListPosition( |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1010 | ExpandableListView.getPackedPositionForGroup(targetIndex + mHeaderCount)), |
| 1011 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | } else if (mList instanceof ListView) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1013 | ((ListView) mList).setSelectionFromTop(targetIndex + mHeaderCount, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | } else { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1015 | mList.setSelection(targetIndex + mHeaderCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } else { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1018 | final int index = MathUtils.constrain((int) (position * count), 0, count - 1); |
Adam Powell | 7ee1ff1 | 2011-03-09 16:35:13 -0800 | [diff] [blame] | 1019 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | if (mList instanceof ExpandableListView) { |
| 1021 | ExpandableListView expList = (ExpandableListView) mList; |
| 1022 | expList.setSelectionFromTop(expList.getFlatListPosition( |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1023 | ExpandableListView.getPackedPositionForGroup(index + mHeaderCount)), 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | } else if (mList instanceof ListView) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1025 | ((ListView)mList).setSelectionFromTop(index + mHeaderCount, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | } else { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1027 | mList.setSelection(index + mHeaderCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1029 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | sectionIndex = -1; |
| 1031 | } |
| 1032 | |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1033 | if (mCurrentSection != sectionIndex) { |
| 1034 | mCurrentSection = sectionIndex; |
| 1035 | |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 1036 | final boolean hasPreview = transitionPreviewLayout(sectionIndex); |
| 1037 | if (!mShowingPreview && hasPreview) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1038 | transitionToDragging(); |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 1039 | } else if (mShowingPreview && !hasPreview) { |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1040 | transitionToVisible(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1041 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1045 | /** |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1046 | * Transitions the preview text to a new section. Handles animation, |
| 1047 | * measurement, and layout. If the new preview text is empty, returns false. |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1048 | * |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1049 | * @param sectionIndex The section index to which the preview should |
| 1050 | * transition. |
| 1051 | * @return False if the new preview text is empty. |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1052 | */ |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1053 | private boolean transitionPreviewLayout(int sectionIndex) { |
| 1054 | final Object[] sections = mSections; |
| 1055 | String text = null; |
| 1056 | if (sections != null && sectionIndex >= 0 && sectionIndex < sections.length) { |
| 1057 | final Object section = sections[sectionIndex]; |
| 1058 | if (section != null) { |
| 1059 | text = section.toString(); |
| 1060 | } |
| 1061 | } |
| 1062 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1063 | final Rect bounds = mTempBounds; |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 1064 | final View preview = mPreviewImage; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1065 | final TextView showing; |
| 1066 | final TextView target; |
| 1067 | if (mShowingPrimary) { |
| 1068 | showing = mPrimaryText; |
| 1069 | target = mSecondaryText; |
| 1070 | } else { |
| 1071 | showing = mSecondaryText; |
| 1072 | target = mPrimaryText; |
| 1073 | } |
| 1074 | |
| 1075 | // Set and layout target immediately. |
| 1076 | target.setText(text); |
| 1077 | measurePreview(target, bounds); |
| 1078 | applyLayout(target, bounds); |
| 1079 | |
| 1080 | if (mPreviewAnimation != null) { |
| 1081 | mPreviewAnimation.cancel(); |
| 1082 | } |
| 1083 | |
| 1084 | // Cross-fade preview text. |
| 1085 | final Animator showTarget = animateAlpha(target, 1f).setDuration(DURATION_CROSS_FADE); |
| 1086 | final Animator hideShowing = animateAlpha(showing, 0f).setDuration(DURATION_CROSS_FADE); |
| 1087 | hideShowing.addListener(mSwitchPrimaryListener); |
| 1088 | |
| 1089 | // Apply preview image padding and animate bounds, if necessary. |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 1090 | bounds.left -= preview.getPaddingLeft(); |
| 1091 | bounds.top -= preview.getPaddingTop(); |
| 1092 | bounds.right += preview.getPaddingRight(); |
| 1093 | bounds.bottom += preview.getPaddingBottom(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1094 | final Animator resizePreview = animateBounds(preview, bounds); |
| 1095 | resizePreview.setDuration(DURATION_RESIZE); |
| 1096 | |
| 1097 | mPreviewAnimation = new AnimatorSet(); |
| 1098 | final AnimatorSet.Builder builder = mPreviewAnimation.play(hideShowing).with(showTarget); |
| 1099 | builder.with(resizePreview); |
| 1100 | |
| 1101 | // The current preview size is unaffected by hidden or showing. It's |
| 1102 | // used to set starting scales for things that need to be scaled down. |
| 1103 | final int previewWidth = preview.getWidth() - preview.getPaddingLeft() |
| 1104 | - preview.getPaddingRight(); |
| 1105 | |
| 1106 | // If target is too large, shrink it immediately to fit and expand to |
| 1107 | // target size. Otherwise, start at target size. |
| 1108 | final int targetWidth = target.getWidth(); |
| 1109 | if (targetWidth > previewWidth) { |
| 1110 | target.setScaleX((float) previewWidth / targetWidth); |
| 1111 | final Animator scaleAnim = animateScaleX(target, 1f).setDuration(DURATION_RESIZE); |
| 1112 | builder.with(scaleAnim); |
| 1113 | } else { |
| 1114 | target.setScaleX(1f); |
| 1115 | } |
| 1116 | |
| 1117 | // If showing is larger than target, shrink to target size. |
| 1118 | final int showingWidth = showing.getWidth(); |
| 1119 | if (showingWidth > targetWidth) { |
| 1120 | final float scale = (float) targetWidth / showingWidth; |
| 1121 | final Animator scaleAnim = animateScaleX(showing, scale).setDuration(DURATION_RESIZE); |
| 1122 | builder.with(scaleAnim); |
| 1123 | } |
| 1124 | |
| 1125 | mPreviewAnimation.start(); |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1126 | |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 1127 | return !TextUtils.isEmpty(text); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /** |
| 1131 | * Positions the thumb and preview widgets. |
| 1132 | * |
| 1133 | * @param position The position, between 0 and 1, along the track at which |
| 1134 | * to place the thumb. |
| 1135 | */ |
| 1136 | private void setThumbPos(float position) { |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 1137 | final Rect container = mContainerRect; |
| 1138 | final int top = container.top; |
| 1139 | final int bottom = container.bottom; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1140 | |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 1141 | final View trackImage = mTrackImage; |
| 1142 | final View thumbImage = mThumbImage; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 1143 | final float min = trackImage.getTop(); |
| 1144 | final float max = trackImage.getBottom(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1145 | final float offset = min; |
| 1146 | final float range = max - min; |
| 1147 | final float thumbMiddle = position * range + offset; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 1148 | thumbImage.setTranslationY(thumbMiddle - thumbImage.getHeight() / 2); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1149 | |
Adam Powell | 5db566f | 2013-10-13 17:19:10 -0700 | [diff] [blame] | 1150 | final float previewPos = mOverlayPosition == OVERLAY_AT_THUMB ? thumbMiddle : 0; |
| 1151 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1152 | // Center the preview on the thumb, constrained to the list bounds. |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 1153 | final View previewImage = mPreviewImage; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 1154 | final float previewHalfHeight = previewImage.getHeight() / 2f; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1155 | final float minP = top + previewHalfHeight; |
| 1156 | final float maxP = bottom - previewHalfHeight; |
Adam Powell | 5db566f | 2013-10-13 17:19:10 -0700 | [diff] [blame] | 1157 | final float previewMiddle = MathUtils.constrain(previewPos, minP, maxP); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1158 | final float previewTop = previewMiddle - previewHalfHeight; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 1159 | previewImage.setTranslationY(previewTop); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1160 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1161 | mPrimaryText.setTranslationY(previewTop); |
| 1162 | mSecondaryText.setTranslationY(previewTop); |
| 1163 | } |
| 1164 | |
| 1165 | private float getPosFromMotionEvent(float y) { |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 1166 | final View trackImage = mTrackImage; |
Alan Viverette | fb66415 | 2013-08-07 17:57:51 -0700 | [diff] [blame] | 1167 | final float min = trackImage.getTop(); |
| 1168 | final float max = trackImage.getBottom(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1169 | final float offset = min; |
| 1170 | final float range = max - min; |
| 1171 | |
| 1172 | // If the list is the same height as the thumbnail or shorter, |
| 1173 | // effectively disable scrolling. |
| 1174 | if (range <= 0) { |
| 1175 | return 0f; |
| 1176 | } |
| 1177 | |
| 1178 | return MathUtils.constrain((y - offset) / range, 0f, 1f); |
| 1179 | } |
| 1180 | |
| 1181 | private float getPosFromItemCount( |
| 1182 | int firstVisibleItem, int visibleItemCount, int totalItemCount) { |
Adam Powell | 35948b7 | 2011-08-25 14:15:59 -0700 | [diff] [blame] | 1183 | if (mSectionIndexer == null || mListAdapter == null) { |
Adam Powell | 32c3a69 | 2011-01-09 21:28:43 -0800 | [diff] [blame] | 1184 | getSectionsFromIndexer(); |
| 1185 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1186 | |
| 1187 | final boolean hasSections = mSectionIndexer != null && mSections != null |
| 1188 | && mSections.length > 0; |
| 1189 | if (!hasSections || !mMatchDragPosition) { |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1190 | return (float) firstVisibleItem / (totalItemCount - visibleItemCount); |
Adam Powell | 32c3a69 | 2011-01-09 21:28:43 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1193 | // Ignore headers. |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1194 | firstVisibleItem -= mHeaderCount; |
Adam Powell | 32c3a69 | 2011-01-09 21:28:43 -0800 | [diff] [blame] | 1195 | if (firstVisibleItem < 0) { |
| 1196 | return 0; |
| 1197 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1198 | totalItemCount -= mHeaderCount; |
Adam Powell | 32c3a69 | 2011-01-09 21:28:43 -0800 | [diff] [blame] | 1199 | |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1200 | // Hidden portion of the first visible row. |
| 1201 | final View child = mList.getChildAt(0); |
| 1202 | final float incrementalPos; |
| 1203 | if (child == null || child.getHeight() == 0) { |
| 1204 | incrementalPos = 0; |
| 1205 | } else { |
| 1206 | incrementalPos = (float) (mList.getPaddingTop() - child.getTop()) / child.getHeight(); |
| 1207 | } |
| 1208 | |
| 1209 | // Number of rows in this section. |
Adam Powell | 32c3a69 | 2011-01-09 21:28:43 -0800 | [diff] [blame] | 1210 | final int section = mSectionIndexer.getSectionForPosition(firstVisibleItem); |
| 1211 | final int sectionPos = mSectionIndexer.getPositionForSection(section); |
Adam Powell | f49971e | 2011-06-14 22:00:01 -0700 | [diff] [blame] | 1212 | final int sectionCount = mSections.length; |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1213 | final int positionsInSection; |
| 1214 | if (section < sectionCount - 1) { |
Jean-Baptiste Queru | 414b023 | 2013-07-08 15:02:41 -0700 | [diff] [blame] | 1215 | final int nextSectionPos; |
| 1216 | if (section + 1 < sectionCount) { |
| 1217 | nextSectionPos = mSectionIndexer.getPositionForSection(section + 1); |
| 1218 | } else { |
| 1219 | nextSectionPos = totalItemCount - 1; |
| 1220 | } |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1221 | positionsInSection = nextSectionPos - sectionPos; |
| 1222 | } else { |
| 1223 | positionsInSection = totalItemCount - sectionPos; |
| 1224 | } |
Adam Powell | 32c3a69 | 2011-01-09 21:28:43 -0800 | [diff] [blame] | 1225 | |
Alan Viverette | 6b40cc7 | 2013-06-25 16:41:52 -0700 | [diff] [blame] | 1226 | // Position within this section. |
| 1227 | final float posWithinSection; |
| 1228 | if (positionsInSection == 0) { |
| 1229 | posWithinSection = 0; |
| 1230 | } else { |
| 1231 | posWithinSection = (firstVisibleItem + incrementalPos - sectionPos) |
| 1232 | / positionsInSection; |
| 1233 | } |
| 1234 | |
Alan Viverette | f9af7b2 | 2013-10-15 17:09:35 -0700 | [diff] [blame] | 1235 | float result = (section + posWithinSection) / sectionCount; |
| 1236 | |
| 1237 | // Fake out the scroll bar for the last item. Since the section indexer |
| 1238 | // won't ever actually move the list in this end space, make scrolling |
| 1239 | // across the last item account for whatever space is remaining. |
| 1240 | if (firstVisibleItem > 0 && firstVisibleItem + visibleItemCount == totalItemCount) { |
| 1241 | final View lastChild = mList.getChildAt(visibleItemCount - 1); |
| 1242 | final float lastItemVisible = (float) (mList.getHeight() - mList.getPaddingBottom() |
| 1243 | - lastChild.getTop()) / lastChild.getHeight(); |
| 1244 | result += (1 - result) * lastItemVisible; |
| 1245 | } |
| 1246 | |
| 1247 | return result; |
Adam Powell | 32c3a69 | 2011-01-09 21:28:43 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1250 | /** |
| 1251 | * Cancels an ongoing fling event by injecting a |
| 1252 | * {@link MotionEvent#ACTION_CANCEL} into the host view. |
| 1253 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | private void cancelFling() { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1255 | final MotionEvent cancelFling = MotionEvent.obtain( |
| 1256 | 0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | mList.onTouchEvent(cancelFling); |
| 1258 | cancelFling.recycle(); |
| 1259 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 1260 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1261 | /** |
| 1262 | * Cancels a pending drag. |
| 1263 | * |
| 1264 | * @see #startPendingDrag() |
| 1265 | */ |
| 1266 | private void cancelPendingDrag() { |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 1267 | mPendingDrag = -1; |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1270 | /** |
| 1271 | * Delays dragging until after the framework has determined that the user is |
| 1272 | * scrolling, rather than tapping. |
| 1273 | */ |
| 1274 | private void startPendingDrag() { |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 1275 | mPendingDrag = SystemClock.uptimeMillis() + TAP_TIMEOUT; |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1278 | private void beginDrag() { |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 1279 | mPendingDrag = -1; |
| 1280 | |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1281 | setState(STATE_DRAGGING); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1282 | |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1283 | if (mListAdapter == null && mList != null) { |
| 1284 | getSectionsFromIndexer(); |
| 1285 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1286 | |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1287 | if (mList != null) { |
| 1288 | mList.requestDisallowInterceptTouchEvent(true); |
| 1289 | mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL); |
| 1290 | } |
| 1291 | |
| 1292 | cancelFling(); |
| 1293 | } |
| 1294 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1295 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 1296 | if (!isEnabled()) { |
Alan Viverette | 447cdf2 | 2013-07-15 17:47:34 -0700 | [diff] [blame] | 1297 | return false; |
| 1298 | } |
| 1299 | |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1300 | switch (ev.getActionMasked()) { |
| 1301 | case MotionEvent.ACTION_DOWN: |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1302 | if (isPointInside(ev.getX(), ev.getY())) { |
| 1303 | // If the parent has requested that its children delay |
| 1304 | // pressed state (e.g. is a scrolling container) then we |
| 1305 | // need to allow the parent time to decide whether it wants |
| 1306 | // to intercept events. If it does, we will receive a CANCEL |
| 1307 | // event. |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 1308 | if (!mList.isInScrollingContainer()) { |
| 1309 | beginDrag(); |
| 1310 | return true; |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1311 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1312 | |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 1313 | mInitialTouchY = ev.getY(); |
| 1314 | startPendingDrag(); |
| 1315 | } |
| 1316 | break; |
| 1317 | case MotionEvent.ACTION_MOVE: |
| 1318 | if (!isPointInside(ev.getX(), ev.getY())) { |
| 1319 | cancelPendingDrag(); |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 1320 | } else if (mPendingDrag >= 0 && mPendingDrag <= SystemClock.uptimeMillis()) { |
| 1321 | beginDrag(); |
| 1322 | |
| 1323 | final float pos = getPosFromMotionEvent(mInitialTouchY); |
| 1324 | scrollTo(pos); |
| 1325 | |
| 1326 | return onTouchEvent(ev); |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1327 | } |
| 1328 | break; |
| 1329 | case MotionEvent.ACTION_UP: |
| 1330 | case MotionEvent.ACTION_CANCEL: |
| 1331 | cancelPendingDrag(); |
| 1332 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1333 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | return false; |
| 1336 | } |
| 1337 | |
Alan Viverette | a709b37 | 2013-07-25 14:43:24 -0700 | [diff] [blame] | 1338 | public boolean onInterceptHoverEvent(MotionEvent ev) { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 1339 | if (!isEnabled()) { |
Alan Viverette | a709b37 | 2013-07-25 14:43:24 -0700 | [diff] [blame] | 1340 | return false; |
| 1341 | } |
| 1342 | |
| 1343 | final int actionMasked = ev.getActionMasked(); |
| 1344 | if ((actionMasked == MotionEvent.ACTION_HOVER_ENTER |
| 1345 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) && mState == STATE_NONE |
| 1346 | && isPointInside(ev.getX(), ev.getY())) { |
| 1347 | setState(STATE_VISIBLE); |
| 1348 | postAutoHide(); |
| 1349 | } |
| 1350 | |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1354 | public boolean onTouchEvent(MotionEvent me) { |
Alan Viverette | b9f2722 | 2013-09-06 19:39:47 -0700 | [diff] [blame] | 1355 | if (!isEnabled()) { |
Alan Viverette | 447cdf2 | 2013-07-15 17:47:34 -0700 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
| 1358 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1359 | switch (me.getActionMasked()) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1360 | case MotionEvent.ACTION_UP: { |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 1361 | if (mPendingDrag >= 0) { |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1362 | // Allow a tap to scroll. |
| 1363 | beginDrag(); |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1364 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1365 | final float pos = getPosFromMotionEvent(me.getY()); |
| 1366 | setThumbPos(pos); |
| 1367 | scrollTo(pos); |
| 1368 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1369 | // Will hit the STATE_DRAGGING check below |
Adam Powell | 20232d0 | 2010-12-08 21:08:53 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1372 | if (mState == STATE_DRAGGING) { |
| 1373 | if (mList != null) { |
| 1374 | // ViewGroup does the right thing already, but there might |
| 1375 | // be other classes that don't properly reset on touch-up, |
| 1376 | // so do this explicitly just in case. |
| 1377 | mList.requestDisallowInterceptTouchEvent(false); |
| 1378 | mList.reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); |
| 1379 | } |
| 1380 | |
| 1381 | setState(STATE_VISIBLE); |
Alan Viverette | a709b37 | 2013-07-25 14:43:24 -0700 | [diff] [blame] | 1382 | postAutoHide(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1383 | |
| 1384 | return true; |
| 1385 | } |
| 1386 | } break; |
| 1387 | |
| 1388 | case MotionEvent.ACTION_MOVE: { |
Alan Viverette | 4cfeedb | 2013-12-03 14:03:24 -0800 | [diff] [blame] | 1389 | if (mPendingDrag >= 0 && Math.abs(me.getY() - mInitialTouchY) > mScaledTouchSlop) { |
| 1390 | beginDrag(); |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1391 | |
Adam Powell | af5280c | 2011-10-11 18:36:34 -0700 | [diff] [blame] | 1392 | // Will hit the STATE_DRAGGING check below |
| 1393 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1394 | |
| 1395 | if (mState == STATE_DRAGGING) { |
| 1396 | // TODO: Ignore jitter. |
| 1397 | final float pos = getPosFromMotionEvent(me.getY()); |
| 1398 | setThumbPos(pos); |
| 1399 | |
| 1400 | // If the previous scrollTo is still pending |
| 1401 | if (mScrollCompleted) { |
| 1402 | scrollTo(pos); |
| 1403 | } |
| 1404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | return true; |
| 1406 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1407 | } break; |
| 1408 | |
| 1409 | case MotionEvent.ACTION_CANCEL: { |
| 1410 | cancelPendingDrag(); |
| 1411 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1413 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1414 | return false; |
| 1415 | } |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 1416 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1417 | /** |
| 1418 | * Returns whether a coordinate is inside the scroller's activation area. If |
| 1419 | * there is a track image, touching anywhere within the thumb-width of the |
| 1420 | * track activates scrolling. Otherwise, the user has to touch inside thumb |
| 1421 | * itself. |
| 1422 | * |
| 1423 | * @param x The x-coordinate. |
| 1424 | * @param y The y-coordinate. |
| 1425 | * @return Whether the coordinate is inside the scroller's activation area. |
| 1426 | */ |
| 1427 | private boolean isPointInside(float x, float y) { |
Alan Viverette | 8636ace | 2013-10-31 15:41:31 -0700 | [diff] [blame] | 1428 | return isPointInsideX(x) && (mTrackDrawable != null || isPointInsideY(y)); |
Romain Guy | 82f3495 | 2009-05-24 18:40:45 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1431 | private boolean isPointInsideX(float x) { |
| 1432 | if (mLayoutFromRight) { |
| 1433 | return x >= mThumbImage.getLeft(); |
| 1434 | } else { |
| 1435 | return x <= mThumbImage.getRight(); |
| 1436 | } |
| 1437 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 1438 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1439 | private boolean isPointInsideY(float y) { |
Adam Powell | d827389 | 2013-10-13 13:35:27 -0700 | [diff] [blame] | 1440 | final float offset = mThumbImage.getTranslationY(); |
| 1441 | final float top = mThumbImage.getTop() + offset; |
| 1442 | final float bottom = mThumbImage.getBottom() + offset; |
| 1443 | return y >= top && y <= bottom; |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1444 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 1445 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1446 | /** |
| 1447 | * Constructs an animator for the specified property on a group of views. |
| 1448 | * See {@link ObjectAnimator#ofFloat(Object, String, float...)} for |
| 1449 | * implementation details. |
| 1450 | * |
| 1451 | * @param property The property being animated. |
| 1452 | * @param value The value to which that property should animate. |
| 1453 | * @param views The target views to animate. |
| 1454 | * @return An animator for all the specified views. |
| 1455 | */ |
| 1456 | private static Animator groupAnimatorOfFloat( |
| 1457 | Property<View, Float> property, float value, View... views) { |
| 1458 | AnimatorSet animSet = new AnimatorSet(); |
| 1459 | AnimatorSet.Builder builder = null; |
| 1460 | |
| 1461 | for (int i = views.length - 1; i >= 0; i--) { |
| 1462 | final Animator anim = ObjectAnimator.ofFloat(views[i], property, value); |
| 1463 | if (builder == null) { |
| 1464 | builder = animSet.play(anim); |
| 1465 | } else { |
| 1466 | builder.with(anim); |
| 1467 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 1469 | |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1470 | return animSet; |
| 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * Returns an animator for the view's scaleX value. |
| 1475 | */ |
| 1476 | private static Animator animateScaleX(View v, float target) { |
| 1477 | return ObjectAnimator.ofFloat(v, View.SCALE_X, target); |
| 1478 | } |
| 1479 | |
| 1480 | /** |
| 1481 | * Returns an animator for the view's alpha value. |
| 1482 | */ |
| 1483 | private static Animator animateAlpha(View v, float alpha) { |
| 1484 | return ObjectAnimator.ofFloat(v, View.ALPHA, alpha); |
| 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * A Property wrapper around the <code>left</code> functionality handled by the |
| 1489 | * {@link View#setLeft(int)} and {@link View#getLeft()} methods. |
| 1490 | */ |
| 1491 | private static Property<View, Integer> LEFT = new IntProperty<View>("left") { |
| 1492 | @Override |
| 1493 | public void setValue(View object, int value) { |
| 1494 | object.setLeft(value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | } |
Alan Viverette | e918a48 | 2013-06-07 11:43:06 -0700 | [diff] [blame] | 1496 | |
| 1497 | @Override |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1498 | public Integer get(View object) { |
| 1499 | return object.getLeft(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | } |
Alan Viverette | 0ebe81e | 2013-06-21 17:01:36 -0700 | [diff] [blame] | 1501 | }; |
| 1502 | |
| 1503 | /** |
| 1504 | * A Property wrapper around the <code>top</code> functionality handled by the |
| 1505 | * {@link View#setTop(int)} and {@link View#getTop()} methods. |
| 1506 | */ |
| 1507 | private static Property<View, Integer> TOP = new IntProperty<View>("top") { |
| 1508 | @Override |
| 1509 | public void setValue(View object, int value) { |
| 1510 | object.setTop(value); |
| 1511 | } |
| 1512 | |
| 1513 | @Override |
| 1514 | public Integer get(View object) { |
| 1515 | return object.getTop(); |
| 1516 | } |
| 1517 | }; |
| 1518 | |
| 1519 | /** |
| 1520 | * A Property wrapper around the <code>right</code> functionality handled by the |
| 1521 | * {@link View#setRight(int)} and {@link View#getRight()} methods. |
| 1522 | */ |
| 1523 | private static Property<View, Integer> RIGHT = new IntProperty<View>("right") { |
| 1524 | @Override |
| 1525 | public void setValue(View object, int value) { |
| 1526 | object.setRight(value); |
| 1527 | } |
| 1528 | |
| 1529 | @Override |
| 1530 | public Integer get(View object) { |
| 1531 | return object.getRight(); |
| 1532 | } |
| 1533 | }; |
| 1534 | |
| 1535 | /** |
| 1536 | * A Property wrapper around the <code>bottom</code> functionality handled by the |
| 1537 | * {@link View#setBottom(int)} and {@link View#getBottom()} methods. |
| 1538 | */ |
| 1539 | private static Property<View, Integer> BOTTOM = new IntProperty<View>("bottom") { |
| 1540 | @Override |
| 1541 | public void setValue(View object, int value) { |
| 1542 | object.setBottom(value); |
| 1543 | } |
| 1544 | |
| 1545 | @Override |
| 1546 | public Integer get(View object) { |
| 1547 | return object.getBottom(); |
| 1548 | } |
| 1549 | }; |
| 1550 | |
| 1551 | /** |
| 1552 | * Returns an animator for the view's bounds. |
| 1553 | */ |
| 1554 | private static Animator animateBounds(View v, Rect bounds) { |
| 1555 | final PropertyValuesHolder left = PropertyValuesHolder.ofInt(LEFT, bounds.left); |
| 1556 | final PropertyValuesHolder top = PropertyValuesHolder.ofInt(TOP, bounds.top); |
| 1557 | final PropertyValuesHolder right = PropertyValuesHolder.ofInt(RIGHT, bounds.right); |
| 1558 | final PropertyValuesHolder bottom = PropertyValuesHolder.ofInt(BOTTOM, bounds.bottom); |
| 1559 | return ObjectAnimator.ofPropertyValuesHolder(v, left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | } |
| 1561 | } |