Skip to content

Commit 2213637

Browse files
committed
fix bug
1 parent c61fd4f commit 2213637

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
android:layout_height="match_parent"
2424
app:expandMode="slide_up"
2525
app:expandTime="700"
26-
app:switchSpeed="6"
26+
app:switchTime="1200"
2727
app:preloadPageNum="3"
2828
app:maxRotateY="5">
2929

ecardflow/src/main/java/moe/codeest/ecardflow/ECardFlow.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,27 @@ public class ECardFlow extends ViewPager {
3131
public static final int SLIDE_UP_TO_EXPAND = 10;
3232
public static final int CLICK_TO_EXPAND = 11;
3333

34+
private static final int DEFAULT_BASE_SWITCH_TIME = 200;
3435
private static final int DEFAULT_EXPAND_TIME = 700;
3536
private static final int DEFAULT_EXPAND_MODE = SLIDE_UP_TO_EXPAND;
36-
private static final int DEFAULT_SWITCH_SPEED = 6;
37+
private static final int DEFAULT_SWITCH_TIME = DEFAULT_BASE_SWITCH_TIME * 6;
3738
private static final int DEFAULT_PRELOAD_PAGE_NUM = 3;
3839

3940
private float mLastX, mLastY, mInterLastX, mInterLastY;
41+
private float mPageScaleX, mPageScaleY, mScaleX, mScaleY, mScrollY;
4042
private float mRate;
4143
private boolean hasReset = true;
4244
private boolean isExpanding = false;
45+
private boolean isSwitching = false;
4346

4447
//Custom Attrs
4548
private int mSlop;
4649
private int mExpandTime;
4750
private int mExpandMode;
48-
private int mScrollFactor;
51+
private int mSwitchTime;
4952
private int mPreloadPageNum;
5053
private int mMaxRotateY;
5154

52-
private float mPageScaleX, mPageScaleY, mScaleX, mScaleY, mScrollY;
53-
5455
public ECardFlow(Context context) {
5556
super(context);
5657
}
@@ -59,7 +60,7 @@ public ECardFlow(Context context, AttributeSet attrs) {
5960
super(context, attrs);
6061

6162
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.attr);
62-
mScrollFactor = ta.getInt(R.styleable.attr_switchSpeed, DEFAULT_SWITCH_SPEED);
63+
mSwitchTime = ta.getInt(R.styleable.attr_switchTime, DEFAULT_SWITCH_TIME);
6364
mExpandMode = ta.getInt(R.styleable.attr_expandMode, DEFAULT_EXPAND_MODE);
6465
mExpandTime = ta.getInteger(R.styleable.attr_expandTime, DEFAULT_EXPAND_TIME);
6566
mPreloadPageNum = ta.getInteger(R.styleable.attr_preloadPageNum, DEFAULT_PRELOAD_PAGE_NUM);
@@ -70,7 +71,7 @@ public ECardFlow(Context context, AttributeSet attrs) {
7071
mTransformer = new CardFlowTransformer();
7172
mTransformer.setMaxRotateY(mMaxRotateY);
7273
setPageTransformer(true, mTransformer);
73-
initSwitchSpeed(mScrollFactor);
74+
initSwitchSpeed(mSwitchTime * 1.00f / DEFAULT_BASE_SWITCH_TIME);
7475
initExpandRate();
7576
mSlop = ViewConfiguration.get(this.getContext()).getScaledTouchSlop();
7677
addOnPageChangeListener(new OnDirectionListener());
@@ -110,15 +111,15 @@ public boolean onTouchEvent(MotionEvent event) {
110111
} else {
111112
gotoNext();
112113
}
113-
} else if(mExpandMode == SLIDE_UP_TO_EXPAND && mLastY - mCurY > mSlop && hasReset) {
114+
} else if(mExpandMode == SLIDE_UP_TO_EXPAND && mLastY - mCurY > mSlop && hasReset && !isSwitching) {
114115
hasReset = false;
115116
expand();
116117
}
117118
break;
118119
case MotionEvent.ACTION_UP:
119120
int mUpX = (int) event.getRawX();
120121
int mUpY = (int) event.getRawY();
121-
if (mExpandMode == CLICK_TO_EXPAND && Math.abs(mUpX - mInterLastX) <= mSlop && Math.abs(mUpY - mInterLastY) <= mSlop) {
122+
if (mExpandMode == CLICK_TO_EXPAND && Math.abs(mUpX - mInterLastX) <= mSlop && Math.abs(mUpY - mInterLastY) <= mSlop && !isSwitching) {
122123
expand();
123124
}
124125
mLastX = 0;
@@ -134,6 +135,9 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
134135
int action = event.getAction();
135136
switch (action) {
136137
case MotionEvent.ACTION_DOWN:
138+
if (isSwitching) {
139+
return false;
140+
}
137141
mInterLastX = (int) event.getRawX();
138142
mInterLastY = (int) event.getRawY();
139143
break;
@@ -161,12 +165,6 @@ public void setExpandTime(int time) {
161165
mExpandTime = time;
162166
}
163167

164-
public void setSwitchSpeed(int rate) {
165-
if (mScroller != null) {
166-
mScroller.setScrollFactor(rate);
167-
}
168-
}
169-
170168
public void setTouchSlop(int slop) {
171169
mSlop = slop;
172170
}
@@ -276,7 +274,7 @@ public void onPageSelected(int position) {
276274

277275
@Override
278276
public void onPageScrollStateChanged(int state) {
279-
277+
isSwitching = state != ViewPager.SCROLL_STATE_IDLE;
280278
}
281279
}
282280

@@ -289,7 +287,7 @@ public void run() {
289287
});
290288
}
291289

292-
private void initSwitchSpeed(int scrollFactor) {
290+
private void initSwitchSpeed(float scrollFactor) {
293291
try {
294292
Class<?> viewpager = ViewPager.class;
295293
Field scroller = viewpager.getDeclaredField("mScroller");
@@ -308,7 +306,7 @@ private void initSwitchSpeed(int scrollFactor) {
308306

309307
public static class ScrollerCustomDuration extends Scroller {
310308

311-
private double mScrollFactor = DEFAULT_SWITCH_SPEED;
309+
private double mScrollFactor = DEFAULT_SWITCH_TIME / DEFAULT_BASE_SWITCH_TIME;
312310

313311
public ScrollerCustomDuration(Context context, Interpolator interpolator) {
314312
super(context, interpolator);

ecardflow/src/main/res/values/attrs.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<declare-styleable name="attr">
4-
<!-- Page switching speed, more big,more slowly, default 6 -->
5-
<attr name="switchSpeed" format="integer" />
4+
<!-- Page switch time, default 1200ms -->
5+
<attr name="switchTime" format="integer" />
66

77
<!-- Page expand time, default 700ms -->
88
<attr name="expandTime" format="integer" />

0 commit comments

Comments
 (0)