| 1 | /* |
| 2 | * This is the source code of Telegram for Android v. 5.x.x. |
| 3 | * It is licensed under GNU GPL v. 2 or later. |
| 4 | * You should have received a copy of the license in this archive (see LICENSE). |
| 5 | * |
| 6 | * Copyright Nikolai Kudashov, 2013-2018. |
| 7 | */ |
| 8 | |
| 9 | package androidx.recyclerview.widget; |
| 10 | |
| 11 | import android.content.Context; |
| 12 | import android.graphics.PointF; |
| 13 | import androidx.annotation.Nullable; |
| 14 | import android.view.View; |
| 15 | import android.view.animation.DecelerateInterpolator; |
| 16 | import android.view.animation.LinearInterpolator; |
| 17 | |
| 18 | import org.telegram.messenger.AndroidUtilities; |
| 19 | |
| 20 | public class LinearSmoothScrollerCustom extends RecyclerView.SmoothScroller { |
| 21 | |
| 22 | private static final float MILLISECONDS_PER_INCH = 25f; |
| 23 | |
| 24 | private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000; |
| 25 | |
| 26 | private static final float TARGET_SEEK_EXTRA_SCROLL_RATIO = 1.2f; |
| 27 | |
| 28 | protected final LinearInterpolator mLinearInterpolator = new LinearInterpolator(); |
| 29 | |
| 30 | protected final DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator(1.5f); |
| 31 | |
| 32 | protected PointF mTargetVector; |
| 33 | |
| 34 | private final float MILLISECONDS_PER_PX; |
| 35 | |
| 36 | protected int mInterimTargetDx = 0, mInterimTargetDy = 0; |
| 37 | private int scrollPosition; |
| 38 | |
| 39 | public static final int POSITION_MIDDLE = 0; |
| 40 | public static final int POSITION_END = 1; |
| 41 | public static final int POSITION_TOP = 2; |
| 42 | |
| 43 | public LinearSmoothScrollerCustom(Context context, int position) { |
| 44 | MILLISECONDS_PER_PX = MILLISECONDS_PER_INCH / context.getResources().getDisplayMetrics().densityDpi; |
| 45 | scrollPosition = position; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | protected void onStart() { |
| 50 | |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void onTargetFound(View targetView, RecyclerView.State state, Action action) { |
| 55 | final int dy = calculateDyToMakeVisible(targetView); |
| 56 | final int time = calculateTimeForDeceleration(dy); |
| 57 | if (time > 0) { |
| 58 | action.update(0, -dy, Math.max(400, time), mDecelerateInterpolator); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | protected void onSeekTargetStep(int dx, int dy, RecyclerView.State state, Action action) { |
| 64 | if (getChildCount() == 0) { |
| 65 | stop(); |
| 66 | return; |
| 67 | } |
| 68 | mInterimTargetDx = clampApplyScroll(mInterimTargetDx, dx); |
| 69 | mInterimTargetDy = clampApplyScroll(mInterimTargetDy, dy); |
| 70 | |
| 71 | if (mInterimTargetDx == 0 && mInterimTargetDy == 0) { |
| 72 | updateActionForInterimTarget(action); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | protected void onStop() { |
| 78 | mInterimTargetDx = mInterimTargetDy = 0; |
| 79 | mTargetVector = null; |
| 80 | } |
| 81 | |
| 82 | protected int calculateTimeForDeceleration(int dx) { |
| 83 | return (int) Math.ceil(calculateTimeForScrolling(dx) / .3356); |
| 84 | } |
| 85 | |
| 86 | protected int calculateTimeForScrolling(int dx) { |
| 87 | return (int) Math.ceil(Math.abs(dx) * MILLISECONDS_PER_PX); |
| 88 | } |
| 89 | |
| 90 | protected void updateActionForInterimTarget(Action action) { |
| 91 | PointF scrollVector = computeScrollVectorForPosition(getTargetPosition()); |
| 92 | if (scrollVector == null || (scrollVector.x == 0 && scrollVector.y == 0)) { |
| 93 | final int target = getTargetPosition(); |
| 94 | action.jumpTo(target); |
| 95 | stop(); |
| 96 | return; |
| 97 | } |
| 98 | normalize(scrollVector); |
| 99 | mTargetVector = scrollVector; |
| 100 | |
| 101 | mInterimTargetDx = (int) (TARGET_SEEK_SCROLL_DISTANCE_PX * scrollVector.x); |
| 102 | mInterimTargetDy = (int) (TARGET_SEEK_SCROLL_DISTANCE_PX * scrollVector.y); |
| 103 | final int time = calculateTimeForScrolling(TARGET_SEEK_SCROLL_DISTANCE_PX); |
| 104 | action.update((int) (mInterimTargetDx * TARGET_SEEK_EXTRA_SCROLL_RATIO), (int) (mInterimTargetDy * TARGET_SEEK_EXTRA_SCROLL_RATIO), (int) (time * TARGET_SEEK_EXTRA_SCROLL_RATIO), mLinearInterpolator); |
| 105 | } |
| 106 | |
| 107 | private int clampApplyScroll(int tmpDt, int dt) { |
| 108 | final int before = tmpDt; |
| 109 | tmpDt -= dt; |
| 110 | if (before * tmpDt <= 0) { |
| 111 | return 0; |
| 112 | } |
| 113 | return tmpDt; |
| 114 | } |
| 115 | |
| 116 | public int calculateDyToMakeVisible(View view) { |
| 117 | final RecyclerView.LayoutManager layoutManager = getLayoutManager(); |
| 118 | if (layoutManager == null || !layoutManager.canScrollVertically()) { |
| 119 | return 0; |
| 120 | } |
| 121 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams(); |
| 122 | int top = layoutManager.getDecoratedTop(view) - params.topMargin; |
| 123 | int bottom = layoutManager.getDecoratedBottom(view) + params.bottomMargin; |
| 124 | int start = layoutManager.getPaddingTop(); |
| 125 | int end = layoutManager.getHeight() - layoutManager.getPaddingBottom(); |
| 126 | |
| 127 | int boxSize = end - start; |
| 128 | int viewSize = bottom - top; |
| 129 | if (scrollPosition == POSITION_TOP) { |
| 130 | start = layoutManager.getPaddingTop(); |
| 131 | } else if (viewSize > boxSize) { |
| 132 | start = 0; |
| 133 | } else if (scrollPosition == POSITION_MIDDLE) { |
| 134 | start = (boxSize - viewSize) / 2; |
| 135 | } else { |
| 136 | start = (layoutManager.getPaddingTop() - AndroidUtilities.dp(88)); |
| 137 | } |
| 138 | end = start + viewSize; |
| 139 | final int dtStart = start - top; |
| 140 | if (dtStart > 0) { |
| 141 | return dtStart; |
| 142 | } |
| 143 | final int dtEnd = end - bottom; |
| 144 | if (dtEnd < 0) { |
| 145 | return dtEnd; |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | @Nullable |
| 151 | public PointF computeScrollVectorForPosition(int targetPosition) { |
| 152 | RecyclerView.LayoutManager layoutManager = getLayoutManager(); |
| 153 | if (layoutManager instanceof ScrollVectorProvider) { |
| 154 | return ((ScrollVectorProvider) layoutManager).computeScrollVectorForPosition(targetPosition); |
| 155 | } |
| 156 | return null; |
| 157 | } |
| 158 | } |
| 159 | |