Skip to content

Commit e92cfd2

Browse files
committed
Improve the motion of indicator
1 parent b728f50 commit e92cfd2

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Change Log
22
==========
33

4+
## Version 1.3.0
5+
* Improve indicator motion
6+
47
## Version 1.2.0
58
* Change minSdkVersion from 9 to 14
69
* Update Android SDK version to 26

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ repositories {
3535
}
3636
3737
dependencies {
38-
compile 'com.nshmura:recyclertablayout:1.2.0'
38+
compile 'com.nshmura:recyclertablayout:1.3.0'
3939
}
4040
```
4141

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# suppress inspection "UnusedProperty" for whole file
2-
VERSION_NAME=1.2.0
3-
VERSION_CODE=13
2+
VERSION_NAME=1.3.0
3+
VERSION_CODE=14
44
GROUP=com.nshmura
55
ARTIFACT_ID=recyclertablayout
66
ARTIFACT_NAME=RecyclerTabLayout

library/src/main/java/com/nshmura/recyclertablayout/RecyclerTabLayout.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ public class RecyclerTabLayout extends RecyclerView {
6161
protected Adapter<?> mAdapter;
6262

6363
protected int mIndicatorPosition;
64-
protected int mIndicatorOffset;
65-
protected int mScrollOffset;
64+
protected int mIndicatorGap;
65+
protected int mIndicatorScroll;
66+
private int mOldPosition;
67+
private int mOldScrollOffset;
6668
protected float mOldPositionOffset;
6769
protected float mPositionThreshold;
6870
protected boolean mRequestScrollToTab;
@@ -248,35 +250,36 @@ protected void scrollToTab(int position, float positionOffset, boolean fitIndica
248250

249251
if (selectedView != null) {
250252
int width = getMeasuredWidth();
251-
float scroll1 = width / 2.f - selectedView.getMeasuredWidth() / 2.f;
253+
float sLeft = (position == 0) ? 0 : width / 2.f - selectedView.getMeasuredWidth() / 2.f; // left edge of selected tab
254+
float sRight = sLeft + selectedView.getMeasuredWidth(); // right edge of selected tab
252255

253256
if (nextView != null) {
254-
float scroll2 = width / 2.f - nextView.getMeasuredWidth() / 2.f;
255-
256-
float scroll = scroll1 + (selectedView.getMeasuredWidth() - scroll2);
257-
float dx = scroll * positionOffset;
258-
scrollOffset = (int) (scroll1 - dx);
259-
260-
mScrollOffset = (int) dx;
261-
mIndicatorOffset = (int) ((scroll1 - scroll2) * positionOffset);
257+
float nLeft = width / 2.f - nextView.getMeasuredWidth() / 2.f; // left edge of next tab
258+
float distance = sRight - nLeft; // total distance that is needed to distance to next tab
259+
float dx = distance * positionOffset;
260+
scrollOffset = (int) (sLeft - dx);
261+
262+
if (position == 0) {
263+
float indicatorGap = (nextView.getMeasuredWidth() - selectedView.getMeasuredWidth()) / 2;
264+
mIndicatorGap = (int) (indicatorGap * positionOffset);
265+
mIndicatorScroll = (int)((selectedView.getMeasuredWidth() + indicatorGap) * positionOffset);
266+
267+
} else {
268+
float indicatorGap = (nextView.getMeasuredWidth() - selectedView.getMeasuredWidth()) / 2;
269+
mIndicatorGap = (int) (indicatorGap * positionOffset);
270+
mIndicatorScroll = (int) dx;
271+
}
262272

263273
} else {
264-
scrollOffset = (int) scroll1;
265-
mScrollOffset = 0;
266-
mIndicatorOffset = 0;
274+
scrollOffset = (int) sLeft;
275+
mIndicatorScroll = 0;
276+
mIndicatorGap = 0;
267277
}
268278
if (fitIndicator) {
269-
mScrollOffset = 0;
270-
mIndicatorOffset = 0;
271-
}
272-
273-
if (mAdapter != null && mIndicatorPosition == position) {
274-
updateCurrentIndicatorPosition(position, positionOffset - mOldPositionOffset,
275-
positionOffset);
279+
mIndicatorScroll = 0;
280+
mIndicatorGap = 0;
276281
}
277282

278-
mIndicatorPosition = position;
279-
280283
} else {
281284
if (getMeasuredWidth() > 0 && mTabMaxWidth > 0 && mTabMinWidth == mTabMaxWidth) { //fixed size
282285
int width = mTabMinWidth;
@@ -287,13 +290,23 @@ protected void scrollToTab(int position, float positionOffset, boolean fitIndica
287290
mRequestScrollToTab = true;
288291
}
289292

293+
if (mAdapter != null && mIndicatorPosition != position) {
294+
updateCurrentIndicatorPosition(position, positionOffset - mOldPositionOffset,
295+
positionOffset);
296+
}
297+
mIndicatorPosition = position;
298+
290299
stopScroll();
291-
mLinearLayoutManager.scrollToPositionWithOffset(position, scrollOffset);
292300

301+
if (position != mOldPosition || scrollOffset != mOldScrollOffset) {
302+
mLinearLayoutManager.scrollToPositionWithOffset(position, scrollOffset);
303+
}
293304
if (mIndicatorHeight > 0) {
294305
invalidate();
295306
}
296307

308+
mOldPosition = position;
309+
mOldScrollOffset = scrollOffset;
297310
mOldPositionOffset = positionOffset;
298311
}
299312

@@ -326,11 +339,11 @@ public void onDraw(Canvas canvas) {
326339
int left;
327340
int right;
328341
if (isLayoutRtl()) {
329-
left = view.getLeft() - mScrollOffset - mIndicatorOffset;
330-
right = view.getRight() - mScrollOffset + mIndicatorOffset;
342+
left = view.getLeft() - mIndicatorScroll - mIndicatorGap;
343+
right = view.getRight() - mIndicatorScroll + mIndicatorGap;
331344
} else {
332-
left = view.getLeft() + mScrollOffset - mIndicatorOffset;
333-
right = view.getRight() + mScrollOffset + mIndicatorOffset;
345+
left = view.getLeft() + mIndicatorScroll - mIndicatorGap;
346+
right = view.getRight() + mIndicatorScroll + mIndicatorGap;
334347
}
335348

336349
int top = getHeight() - mIndicatorHeight;

0 commit comments

Comments
 (0)