| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.widget; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.content.Context; |
| 21 |
import android.graphics.PointF; |
| 22 |
import android.util.DisplayMetrics; |
| 23 |
import android.util.Log; |
| 24 |
import android.view.View; |
| 25 |
import android.view.animation.DecelerateInterpolator; |
| 26 |
import android.view.animation.LinearInterpolator; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class LinearSmoothScroller extends RecyclerView.SmoothScroller { |
| 39 |
|
| 40 |
private static final String TAG = "LinearSmoothScroller"; |
| 41 |
|
| 42 |
private static final boolean DEBUG = false; |
| 43 |
|
| 44 |
private static final float MILLISECONDS_PER_INCH = 25f; |
| 45 |
|
| 46 |
private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final int SNAP_TO_START = -1; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static final int SNAP_TO_END = 1; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static final int SNAP_TO_ANY = 0; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private static final float TARGET_SEEK_EXTRA_SCROLL_RATIO = 1.2f; |
| 82 |
|
| 83 |
protected final LinearInterpolator mLinearInterpolator = new LinearInterpolator(); |
| 84 |
|
| 85 |
protected final DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator(); |
| 86 |
|
| 87 |
protected PointF mTargetVector; |
| 88 |
|
| 89 |
private final float MILLISECONDS_PER_PX; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
protected int mInterimTargetDx = 0, mInterimTargetDy = 0; |
| 94 |
|
| 95 |
public LinearSmoothScroller(Context context) { |
| 96 |
MILLISECONDS_PER_PX = calculateSpeedPerPixel(context.getResources().getDisplayMetrics()); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@Override |
| 103 |
protected void onStart() { |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
@Override |
| 111 |
protected void onTargetFound(View targetView, RecyclerView.State state, Action action) { |
| 112 |
final int dx = calculateDxToMakeVisible(targetView, getHorizontalSnapPreference()); |
| 113 |
final int dy = calculateDyToMakeVisible(targetView, getVerticalSnapPreference()); |
| 114 |
final int distance = (int) Math.sqrt(dx * dx + dy * dy); |
| 115 |
final int time = calculateTimeForDeceleration(distance); |
| 116 |
if (time > 0) { |
| 117 |
action.update(-dx, -dy, time, mDecelerateInterpolator); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@Override |
| 125 |
protected void onSeekTargetStep(int dx, int dy, RecyclerView.State state, Action action) { |
| 126 |
if (getChildCount() == 0) { |
| 127 |
stop(); |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
if (DEBUG && mTargetVector != null |
| 132 |
&& ((mTargetVector.x * dx < 0 || mTargetVector.y * dy < 0))) { |
| 133 |
throw new IllegalStateException("Scroll happened in the opposite direction" |
| 134 |
+ " of the target. Some calculations are wrong"); |
| 135 |
} |
| 136 |
mInterimTargetDx = clampApplyScroll(mInterimTargetDx, dx); |
| 137 |
mInterimTargetDy = clampApplyScroll(mInterimTargetDy, dy); |
| 138 |
|
| 139 |
if (mInterimTargetDx == 0 && mInterimTargetDy == 0) { |
| 140 |
updateActionForInterimTarget(action); |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
@Override |
| 149 |
protected void onStop() { |
| 150 |
mInterimTargetDx = mInterimTargetDy = 0; |
| 151 |
mTargetVector = null; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { |
| 162 |
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
protected int calculateTimeForDeceleration(int dx) { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
return (int) Math.ceil(calculateTimeForScrolling(dx) / .3356); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
protected int calculateTimeForScrolling(int dx) { |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
return (int) Math.ceil(Math.abs(dx) * MILLISECONDS_PER_PX); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
protected int getHorizontalSnapPreference() { |
| 206 |
return mTargetVector == null || mTargetVector.x == 0 ? SNAP_TO_ANY : |
| 207 |
mTargetVector.x > 0 ? SNAP_TO_END : SNAP_TO_START; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
protected int getVerticalSnapPreference() { |
| 220 |
return mTargetVector == null || mTargetVector.y == 0 ? SNAP_TO_ANY : |
| 221 |
mTargetVector.y > 0 ? SNAP_TO_END : SNAP_TO_START; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
protected void updateActionForInterimTarget(Action action) { |
| 231 |
|
| 232 |
PointF scrollVector = computeScrollVectorForPosition(getTargetPosition()); |
| 233 |
if (scrollVector == null || (scrollVector.x == 0 && scrollVector.y == 0)) { |
| 234 |
final int target = getTargetPosition(); |
| 235 |
action.jumpTo(target); |
| 236 |
stop(); |
| 237 |
return; |
| 238 |
} |
| 239 |
normalize(scrollVector); |
| 240 |
mTargetVector = scrollVector; |
| 241 |
|
| 242 |
mInterimTargetDx = (int) (TARGET_SEEK_SCROLL_DISTANCE_PX * scrollVector.x); |
| 243 |
mInterimTargetDy = (int) (TARGET_SEEK_SCROLL_DISTANCE_PX * scrollVector.y); |
| 244 |
final int time = calculateTimeForScrolling(TARGET_SEEK_SCROLL_DISTANCE_PX); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
action.update((int) (mInterimTargetDx * TARGET_SEEK_EXTRA_SCROLL_RATIO), |
| 249 |
(int) (mInterimTargetDy * TARGET_SEEK_EXTRA_SCROLL_RATIO), |
| 250 |
(int) (time * TARGET_SEEK_EXTRA_SCROLL_RATIO), mLinearInterpolator); |
| 251 |
} |
| 252 |
|
| 253 |
private int clampApplyScroll(int tmpDt, int dt) { |
| 254 |
final int before = tmpDt; |
| 255 |
tmpDt -= dt; |
| 256 |
if (before * tmpDt <= 0) { |
| 257 |
return 0; |
| 258 |
} |
| 259 |
return tmpDt; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int |
| 267 |
snapPreference) { |
| 268 |
switch (snapPreference) { |
| 269 |
case SNAP_TO_START: |
| 270 |
return boxStart - viewStart; |
| 271 |
case SNAP_TO_END: |
| 272 |
return boxEnd - viewEnd; |
| 273 |
case SNAP_TO_ANY: |
| 274 |
final int dtStart = boxStart - viewStart; |
| 275 |
if (dtStart > 0) { |
| 276 |
return dtStart; |
| 277 |
} |
| 278 |
final int dtEnd = boxEnd - viewEnd; |
| 279 |
if (dtEnd < 0) { |
| 280 |
return dtEnd; |
| 281 |
} |
| 282 |
break; |
| 283 |
default: |
| 284 |
throw new IllegalArgumentException("snap preference should be one of the" |
| 285 |
+ " constants defined in SmoothScroller, starting with SNAP_"); |
| 286 |
} |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public int calculateDyToMakeVisible(View view, int snapPreference) { |
| 302 |
final RecyclerView.LayoutManager layoutManager = getLayoutManager(); |
| 303 |
if (layoutManager == null || !layoutManager.canScrollVertically()) { |
| 304 |
return 0; |
| 305 |
} |
| 306 |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) |
| 307 |
view.getLayoutParams(); |
| 308 |
final int top = layoutManager.getDecoratedTop(view) - params.topMargin; |
| 309 |
final int bottom = layoutManager.getDecoratedBottom(view) + params.bottomMargin; |
| 310 |
final int start = layoutManager.getPaddingTop(); |
| 311 |
final int end = layoutManager.getHeight() - layoutManager.getPaddingBottom(); |
| 312 |
return calculateDtToFit(top, bottom, start, end, snapPreference); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public int calculateDxToMakeVisible(View view, int snapPreference) { |
| 327 |
final RecyclerView.LayoutManager layoutManager = getLayoutManager(); |
| 328 |
if (layoutManager == null || !layoutManager.canScrollHorizontally()) { |
| 329 |
return 0; |
| 330 |
} |
| 331 |
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) |
| 332 |
view.getLayoutParams(); |
| 333 |
final int left = layoutManager.getDecoratedLeft(view) - params.leftMargin; |
| 334 |
final int right = layoutManager.getDecoratedRight(view) + params.rightMargin; |
| 335 |
final int start = layoutManager.getPaddingLeft(); |
| 336 |
final int end = layoutManager.getWidth() - layoutManager.getPaddingRight(); |
| 337 |
return calculateDtToFit(left, right, start, end, snapPreference); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
@Nullable |
| 351 |
public PointF computeScrollVectorForPosition(int targetPosition) { |
| 352 |
RecyclerView.LayoutManager layoutManager = getLayoutManager(); |
| 353 |
if (layoutManager instanceof ScrollVectorProvider) { |
| 354 |
return ((ScrollVectorProvider) layoutManager) |
| 355 |
.computeScrollVectorForPosition(targetPosition); |
| 356 |
} |
| 357 |
Log.w(TAG, "You should override computeScrollVectorForPosition when the LayoutManager" |
| 358 |
+ " does not implement " + ScrollVectorProvider.class.getCanonicalName()); |
| 359 |
return null; |
| 360 |
} |
| 361 |
} |