|
1 | 1 | package com.tippingcanoe.dewey; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.graphics.PointF; |
| 5 | +import android.support.v4.view.ViewCompat; |
4 | 6 | import android.support.v7.widget.LinearLayoutManager; |
| 7 | +import android.support.v7.widget.LinearSmoothScroller; |
5 | 8 | import android.support.v7.widget.RecyclerView; |
6 | 9 | import android.util.AttributeSet; |
7 | | -import android.util.Log; |
8 | 10 | import android.view.View; |
9 | 11 | import android.view.ViewGroup; |
10 | 12 |
|
11 | 13 | class DeweyLayoutManager extends LinearLayoutManager { |
12 | 14 | int uniformCellWidth = 0; |
13 | 15 | int forcedCellWidth = 0; |
| 16 | + int animationDuration = 0; |
14 | 17 |
|
15 | | - public DeweyLayoutManager(Context context, int uniformCellWidth) { |
| 18 | + public DeweyLayoutManager(Context context, int uniformCellWidth, int animationDuration) { |
16 | 19 | super(context); |
17 | 20 | this.uniformCellWidth = uniformCellWidth; |
| 21 | + this.animationDuration = animationDuration; |
18 | 22 | updateUniformCellWidth(); |
19 | 23 | } |
20 | 24 |
|
21 | | - public DeweyLayoutManager(Context context, int orientation, boolean reverseLayout, int uniformCellWidth) { |
| 25 | + public DeweyLayoutManager(Context context, int orientation, boolean reverseLayout, int uniformCellWidth, int animationDuration) { |
22 | 26 | super(context, orientation, reverseLayout); |
23 | 27 | this.uniformCellWidth = uniformCellWidth; |
| 28 | + this.animationDuration = animationDuration; |
24 | 29 | updateUniformCellWidth(); |
25 | 30 | } |
26 | 31 |
|
27 | | - public DeweyLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int uniformCellWidth) { |
| 32 | + public DeweyLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, int uniformCellWidth, int animationDuration) { |
28 | 33 | super(context, attrs, defStyleAttr, defStyleRes); |
29 | 34 | this.uniformCellWidth = uniformCellWidth; |
| 35 | + this.animationDuration = animationDuration; |
30 | 36 | updateUniformCellWidth(); |
31 | 37 | } |
32 | 38 |
|
@@ -116,4 +122,55 @@ public void setUniformCellWidth(int uniformCellWidth) { |
116 | 122 | public int getForcedCellWidth() { |
117 | 123 | return forcedCellWidth; |
118 | 124 | } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { |
| 128 | + if ( areCellsUniform() ) { |
| 129 | + View firstVisibleChild = recyclerView.getChildAt(0); |
| 130 | + int distanceInPixels = 0; |
| 131 | + |
| 132 | + if ( firstVisibleChild != null ) { |
| 133 | + int currentPosition = recyclerView.getChildLayoutPosition(firstVisibleChild); |
| 134 | + distanceInPixels = Math.abs((currentPosition - position) * getUniformCellWidth()); |
| 135 | + if (distanceInPixels == 0) { |
| 136 | + distanceInPixels = (int) Math.abs(ViewCompat.getY(firstVisibleChild)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if ( distanceInPixels == 0 ) { |
| 141 | + super.smoothScrollToPosition(recyclerView, state, position); |
| 142 | + } else { |
| 143 | + DurationSmoothScroller smoothScroller = new DurationSmoothScroller(recyclerView.getContext(), distanceInPixels, animationDuration); |
| 144 | + smoothScroller.setTargetPosition(position); |
| 145 | + startSmoothScroll(smoothScroller); |
| 146 | + } |
| 147 | + } else { |
| 148 | + super.smoothScrollToPosition(recyclerView, state, position); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + class DurationSmoothScroller extends LinearSmoothScroller { |
| 153 | + private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000; |
| 154 | + private final float distanceInPixels; |
| 155 | + private final float duration; |
| 156 | + |
| 157 | + public DurationSmoothScroller(Context context, int distanceInPixels, int duration) { |
| 158 | + super(context); |
| 159 | + this.distanceInPixels = distanceInPixels; |
| 160 | + float millisecondsPerPx = calculateSpeedPerPixel(context.getResources().getDisplayMetrics()); |
| 161 | + this.duration = distanceInPixels < TARGET_SEEK_SCROLL_DISTANCE_PX ? |
| 162 | + (int) (Math.abs(distanceInPixels) * millisecondsPerPx) : duration; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public PointF computeScrollVectorForPosition(int targetPosition) { |
| 167 | + return DeweyLayoutManager.this.computeScrollVectorForPosition(targetPosition); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + protected int calculateTimeForScrolling(int dx) { |
| 172 | + float proportion = (float) dx / distanceInPixels; |
| 173 | + return (int) (duration * proportion); |
| 174 | + } |
| 175 | + } |
119 | 176 | } |
0 commit comments