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