Skip to content

Commit f7daf42

Browse files
author
Iain Connor
committed
Updated tools and allowed smooth scrolling
1 parent f4168e6 commit f7daf42

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.0"
5+
buildToolsVersion "23.0.2"
66

77
defaultConfig {
88
applicationId "com.tippingcanoe.dewey.app"
@@ -21,6 +21,6 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:appcompat-v7:23.0.1'
24+
compile 'com.android.support:appcompat-v7:23.1.1'
2525
compile project(':library')
2626
}

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.0"
5+
buildToolsVersion "23.0.2"
66

77
defaultConfig {
88
minSdkVersion 10
@@ -21,6 +21,6 @@ android {
2121
dependencies {
2222
compile fileTree(dir: 'libs', include: ['*.jar'])
2323

24-
compile 'com.android.support:appcompat-v7:23.0.1'
25-
compile 'com.android.support:recyclerview-v7:23.0.1'
24+
compile 'com.android.support:appcompat-v7:23.1.1'
25+
compile 'com.android.support:recyclerview-v7:23.1.1'
2626
}

library/src/main/java/com/tippingcanoe/dewey/Dewey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected void setup ( Context context, @Nullable AttributeSet attrs ) {
7474
}
7575
}
7676

77-
layoutManager = new DeweyLayoutManager(context, LinearLayoutManager.HORIZONTAL, false, uniformCellWidth);
77+
layoutManager = new DeweyLayoutManager(context, LinearLayoutManager.HORIZONTAL, false, uniformCellWidth, animationDurationMs);
7878
setLayoutManager(layoutManager);
7979
setHasFixedSize(true);
8080
setHorizontalScrollBarEnabled(false);

library/src/main/java/com/tippingcanoe/dewey/DeweyLayoutManager.java

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
package com.tippingcanoe.dewey;
22

33
import android.content.Context;
4+
import android.graphics.PointF;
5+
import android.support.v4.view.ViewCompat;
46
import android.support.v7.widget.LinearLayoutManager;
7+
import android.support.v7.widget.LinearSmoothScroller;
58
import android.support.v7.widget.RecyclerView;
69
import android.util.AttributeSet;
7-
import android.util.Log;
810
import android.view.View;
911
import android.view.ViewGroup;
1012

1113
class DeweyLayoutManager extends LinearLayoutManager {
1214
int uniformCellWidth = 0;
1315
int forcedCellWidth = 0;
16+
int animationDuration = 0;
1417

15-
public DeweyLayoutManager(Context context, int uniformCellWidth) {
18+
public DeweyLayoutManager(Context context, int uniformCellWidth, int animationDuration) {
1619
super(context);
1720
this.uniformCellWidth = uniformCellWidth;
21+
this.animationDuration = animationDuration;
1822
updateUniformCellWidth();
1923
}
2024

21-
public DeweyLayoutManager(Context context, int orientation, boolean reverseLayout, int uniformCellWidth) {
25+
public DeweyLayoutManager(Context context, int orientation, boolean reverseLayout, int uniformCellWidth, int animationDuration) {
2226
super(context, orientation, reverseLayout);
2327
this.uniformCellWidth = uniformCellWidth;
28+
this.animationDuration = animationDuration;
2429
updateUniformCellWidth();
2530
}
2631

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) {
2833
super(context, attrs, defStyleAttr, defStyleRes);
2934
this.uniformCellWidth = uniformCellWidth;
35+
this.animationDuration = animationDuration;
3036
updateUniformCellWidth();
3137
}
3238

@@ -116,4 +122,55 @@ public void setUniformCellWidth(int uniformCellWidth) {
116122
public int getForcedCellWidth() {
117123
return forcedCellWidth;
118124
}
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+
}
119176
}

0 commit comments

Comments
 (0)