Skip to content

Commit

Permalink
bugfix: start dragging and then quickly click on the tab will bring a…
Browse files Browse the repository at this point in the history
…n error #198
  • Loading branch information
cgspine committed Mar 23, 2018
1 parent d48b609 commit e5f112b
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions qmui/src/main/java/com/qmuiteam/qmui/widget/QMUITabSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.Gravity;
Expand Down Expand Up @@ -172,7 +175,7 @@ public class QMUITabSegment extends HorizontalScrollView {
protected OnClickListener mTabOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (mIsAnimating) {
if (mIsAnimating || mViewPagerScrollState != ViewPager.SCROLL_STATE_IDLE) {
return;
}
int index = (int) v.getTag();
Expand All @@ -194,6 +197,7 @@ public void onClick(View v) {
private ViewPager.OnPageChangeListener mOnPageChangeListener;
private OnTabSelectedListener mViewPagerSelectedListener;
// private AdapterChangeListener mAdapterChangeListener;
private boolean mIsInSelectTab = false;

public QMUITabSegment(Context context) {
this(context, null);
Expand Down Expand Up @@ -563,16 +567,23 @@ public void selectTab(int index) {
* 只有点击 tab 才会自己产生动画变化,其它需要使用 updateIndicatorPosition 做驱动
*/
private void selectTab(final int index, boolean preventAnim) {
if(mIsInSelectTab){
return;
}
mIsInSelectTab = true;
if (mContentLayout.getTabAdapter().getSize() == 0 || mContentLayout.getTabAdapter().getSize() <= index) {
mIsInSelectTab = false;
return;
}
if (mSelectedIndex == index) {
dispatchTabReselected(index);
mIsInSelectTab = false;
return;
}

if (mIsAnimating) {
mPendingSelectedIndex = index;
mIsInSelectTab = false;
return;
}

Expand All @@ -594,6 +605,7 @@ private void selectTab(final int index, boolean preventAnim) {
changeTabColor(selectedTv, getTabSelectedColor(model), model, STATUS_SELECTED);
dispatchTabSelected(index);
mSelectedIndex = index;
mIsInSelectTab = false;
return;
}
final int prev = mSelectedIndex;
Expand All @@ -603,6 +615,8 @@ private void selectTab(final int index, boolean preventAnim) {
final TabItemView nowView = listViews.get(index);

if (preventAnim) {
dispatchTabUnselected(prev);
dispatchTabSelected(index);
setTextViewTypeface(prevView.getTextView(), false);
setTextViewTypeface(nowView.getTextView(), true);
changeTabColor(prevView.getTextView(), getTabNormalColor(prevModel), prevModel, STATUS_NORMAL, mViewPagerScrollState != ViewPager.SCROLL_STATE_IDLE);
Expand All @@ -615,10 +629,8 @@ private void selectTab(final int index, boolean preventAnim) {
smoothScrollBy(nowView.getRight() - realWidth - getScrollX(), 0);
}
}

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

Expand Down Expand Up @@ -677,6 +689,7 @@ public void onAnimationRepeat(Animator animation) {
});
animator.setDuration(200);
animator.start();
mIsInSelectTab = false;
}

private void setTextViewTypeface(TextView tv, boolean selected) {
Expand All @@ -687,13 +700,11 @@ private void setTextViewTypeface(TextView tv, boolean selected) {
tv.setTypeface(null, isBold ? Typeface.BOLD : Typeface.NORMAL);
}

public void updateIndicatorPosition(int index, float offsetPercent) {
if (mIsAnimating) {
return;
}
if (offsetPercent == 0) {
public void updateIndicatorPosition(final int index, float offsetPercent) {
if (mIsAnimating || mIsInSelectTab || offsetPercent == 0) {
return;
}

int targetIndex;
if (offsetPercent < 0) {
targetIndex = index - 1;
Expand Down Expand Up @@ -1334,7 +1345,7 @@ protected void bind(Tab item, TabItemView view, int position) {
}
}

public class InnerTextView extends TextView {
public class InnerTextView extends AppCompatTextView {

public InnerTextView(Context context) {
super(context);
Expand Down

0 comments on commit e5f112b

Please sign in to comment.