Skip to content

Commit

Permalink
bugfix: The text flashes while scrolling in QMUITabSegment #141
Browse files Browse the repository at this point in the history
  • Loading branch information
cgspine committed Jan 12, 2018
1 parent 7005f40 commit d7e0f58
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions qmui/src/main/java/com/qmuiteam/qmui/widget/QMUITabSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.QMUIInterpolatorStaticHolder;
import com.qmuiteam.qmui.R;
import com.qmuiteam.qmui.util.QMUIColorHelper;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
Expand Down Expand Up @@ -159,6 +161,12 @@ public class QMUITabSegment extends HorizontalScrollView {
* typeface
*/
private TypefaceProvider mTypefaceProvider;

/**
* 记录 ViewPager 的 scrollState
*/
private int mViewPagerScrollState = ViewPager.SCROLL_STATE_IDLE;

private boolean mIsAnimating;
private OnTabClickListener mOnTabClickListener;
private boolean mForceIndicatorNotDoLayoutWhenParentLayout = false;
Expand Down Expand Up @@ -518,8 +526,15 @@ private void preventLayoutToChangeTabColor(TextView textView, int color, Tab mod
mForceIndicatorNotDoLayoutWhenParentLayout = false;
}

private void changeTabColor(TextView textView, int color, Tab model, int status) {
textView.setTextColor(color);
private void changeTabColor(TextView textView, int color, Tab model, int status){
changeTabColor(textView, color, model, status, false);
}

private void changeTabColor(TextView textView, int color, Tab model, int status, boolean preventColorChange) {
if(!preventColorChange){
textView.setTextColor(color);
}

if (!model.isDynamicChangeIconColor()) {
if (status == STATUS_NORMAL || model.getSelectedIcon() == null) {
setDrawable(textView, model.getNormalIcon(), getTabIconPosition(model));
Expand All @@ -528,13 +543,17 @@ private void changeTabColor(TextView textView, int color, Tab model, int status)
}
return;
}
Drawable drawable = textView.getCompoundDrawables()[getTabIconPosition(model)];
if (drawable == null) {
return;

if(!preventColorChange){
Drawable drawable = textView.getCompoundDrawables()[getTabIconPosition(model)];
if (drawable == null) {
return;
}
// 这里要拿textView已经set并mutate的drawable
QMUIDrawableHelper.setDrawableTintColor(drawable, color);
setDrawable(textView, model.getNormalIcon(), getTabIconPosition(model));
}
// 这里要拿textView已经set并mutate的drawable
QMUIDrawableHelper.setDrawableTintColor(drawable, color);
setDrawable(textView, model.getNormalIcon(), getTabIconPosition(model));

}

public void selectTab(int index) {
Expand Down Expand Up @@ -587,11 +606,8 @@ private void selectTab(final int index, boolean preventAnim) {
if (preventAnim) {
setTextViewTypeface(prevView.getTextView(), false);
setTextViewTypeface(nowView.getTextView(), true);
changeTabColor(prevView.getTextView(), getTabNormalColor(prevModel), prevModel, STATUS_NORMAL);
changeTabColor(nowView.getTextView(), getTabSelectedColor(nowModel), nowModel, STATUS_SELECTED);
dispatchTabUnselected(prev);
dispatchTabSelected(index);
mSelectedIndex = index;
changeTabColor(prevView.getTextView(), getTabNormalColor(prevModel), prevModel, STATUS_NORMAL, true);
changeTabColor(nowView.getTextView(), getTabSelectedColor(nowModel), nowModel, STATUS_SELECTED, true);
if (getScrollX() > nowView.getLeft()) {
smoothScrollTo(nowView.getLeft(), 0);
} else {
Expand All @@ -600,12 +616,17 @@ private void selectTab(final int index, boolean preventAnim) {
smoothScrollBy(nowView.getRight() - realWidth - getScrollX(), 0);
}
}

dispatchTabUnselected(prev);
dispatchTabSelected(index);
mSelectedIndex = index;
return;
}

final int leftDistance = nowModel.getContentLeft() - prevModel.getContentLeft();
final int widthDistance = nowModel.getContentWidth() - prevModel.getContentWidth();
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
animator.setInterpolator(QMUIInterpolatorStaticHolder.LINEAR_INTERPOLATOR);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Expand Down Expand Up @@ -941,6 +962,10 @@ public TabLayoutOnPageChangeListener(QMUITabSegment tabSegment) {

@Override
public void onPageScrollStateChanged(final int state) {
final QMUITabSegment tabSegment = mTabSegmentRef.get();
if (tabSegment != null) {
tabSegment.mViewPagerScrollState = state;
}

}

Expand Down

0 comments on commit d7e0f58

Please sign in to comment.