Skip to content

Commit 79e1f7d

Browse files
author
Iain Connor
committed
Centering cells working
1 parent 77e8024 commit 79e1f7d

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.support.v7.widget.LinearLayoutManager;
1111
import android.support.v7.widget.RecyclerView;
1212
import android.util.AttributeSet;
13+
import android.util.Log;
1314
import android.view.animation.AccelerateDecelerateInterpolator;
1415
import android.view.animation.AccelerateInterpolator;
1516
import android.view.animation.Interpolator;
@@ -77,7 +78,23 @@ protected void setup ( Context context, @Nullable AttributeSet attrs ) {
7778
setHasFixedSize(true);
7879
setHorizontalScrollBarEnabled(false);
7980

80-
setFocusedPosition(0, false);
81+
setFocusedPosition(0, false, true);
82+
}
83+
84+
protected int adjustPositionToCenterIfNeeded(int inputPosition) {
85+
if ( inputPosition != focusedPosition && layoutManager != null && layoutManager.areCellsUniform() && layoutManager.getUniformCellWidth() != 0 ) {
86+
boolean scrollingRight = inputPosition > focusedPosition;
87+
float cellsInView = ((float) getMeasuredWidth() / (float) layoutManager.getUniformCellWidth());
88+
int cellsToCenter = (int) Math.floor((cellsInView / 2.0f));
89+
int offset = (scrollingRight ? 1 : -1) * cellsToCenter;
90+
int adjustedPosition = Math.min(inputPosition + offset, getAdapter().getItemCount() - 1);
91+
92+
if ( adjustedPosition >= 0 && inputPosition >= offset ) {
93+
return adjustedPosition;
94+
}
95+
}
96+
97+
return inputPosition;
8198
}
8299

83100
public void setFocusedPosition ( int position, boolean animated ) {
@@ -89,20 +106,23 @@ public void setFocusedPosition ( int position, boolean animated, boolean silentl
89106
onFocusedPositionChangedListener.onFocusedPositionChanged(focusedPosition, position);
90107
}
91108

92-
boolean requestedPositionIsVisible = position >= getChildAdapterPosition(getChildAt(0)) && position <= getChildAdapterPosition(getChildAt(getChildCount() - 1));
109+
int adjustedScrollTarget = adjustPositionToCenterIfNeeded(position);
93110

94-
// @TODO, enhance this by attempting to center the focused position.
95-
if (requestedPositionIsVisible && animated) {
111+
if (requestedPositionIsVisible(position) && requestedPositionIsVisible(adjustedScrollTarget) && animated) {
96112
deweyDecorator.startAnimation(focusedPosition, position);
97113
} else if (animated) {
98-
smoothScrollToPosition(position);
114+
smoothScrollToPosition(adjustedScrollTarget);
99115
} else {
100-
scrollToPosition(position);
116+
scrollToPosition(adjustedScrollTarget);
101117
}
102118

103119
focusedPosition = position;
104120
}
105121

122+
public boolean requestedPositionIsVisible ( int position ) {
123+
return position >= getChildAdapterPosition(getChildAt(0)) && position <= getChildAdapterPosition(getChildAt(getChildCount() - 1));
124+
}
125+
106126
@Override
107127
public void setAdapter ( Adapter adapter ) {
108128
super.setAdapter(adapter);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public void startAnimation ( int previouslyFocusedPosition, int newFocusedPositi
324324
int maxVisiblePos = 0;
325325

326326
for (int i = 0; i < dewey.getChildCount(); i++) {
327-
int childPosition = dewey.getChildPosition(dewey.getChildAt(i));
327+
int childPosition = dewey.getChildLayoutPosition(dewey.getChildAt(i));
328328

329329
if (childPosition != RecyclerView.NO_POSITION) {
330330
if (childPosition == previouslyFocusedPosition) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public boolean areCellsUniform() {
128128
return uniformCells;
129129
}
130130

131+
public int getUniformCellWidth() {
132+
return uniformCellWidth;
133+
}
134+
131135
public void setUniformCells(boolean uniformCells) {
132136
this.uniformCells = uniformCells;
133137
}

0 commit comments

Comments
 (0)