Skip to content

Commit 2597064

Browse files
committed
Merge pull request #509 from cody-young-move/fixed_scroll_issues
Fixed two nested scroll issues
2 parents 44d8409 + bc24980 commit 2597064

File tree

1 file changed

+68
-50
lines changed

1 file changed

+68
-50
lines changed

library/src/com/sothree/slidinguppanel/SlidingUpPanelLayout.java

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import android.os.Build;
1212
import android.os.Parcel;
1313
import android.os.Parcelable;
14+
import android.support.annotation.NonNull;
1415
import android.support.v4.view.MotionEventCompat;
1516
import android.support.v4.view.ViewCompat;
1617
import android.util.AttributeSet;
17-
import android.util.Log;
1818
import android.view.Gravity;
1919
import android.view.MotionEvent;
2020
import android.view.View;
@@ -70,8 +70,8 @@ public class SlidingUpPanelLayout extends ViewGroup {
7070
/**
7171
* Default attributes for layout
7272
*/
73-
private static final int[] DEFAULT_ATTRS = new int[] {
74-
android.R.attr.gravity
73+
private static final int[] DEFAULT_ATTRS = new int[]{
74+
android.R.attr.gravity
7575
};
7676

7777
/**
@@ -167,6 +167,7 @@ public enum PanelState {
167167
HIDDEN,
168168
DRAGGING
169169
}
170+
170171
private PanelState mSlideState = DEFAULT_SLIDE_STATE;
171172

172173
/**
@@ -225,30 +226,36 @@ public enum PanelState {
225226
public interface PanelSlideListener {
226227
/**
227228
* Called when a sliding pane's position changes.
228-
* @param panel The child view that was moved
229+
*
230+
* @param panel The child view that was moved
229231
* @param slideOffset The new offset of this sliding pane within its range, from 0-1
230232
*/
231233
public void onPanelSlide(View panel, float slideOffset);
234+
232235
/**
233236
* Called when a sliding panel becomes slid completely collapsed.
237+
*
234238
* @param panel The child view that was slid to an collapsed position
235239
*/
236240
public void onPanelCollapsed(View panel);
237241

238242
/**
239243
* Called when a sliding panel becomes slid completely expanded.
244+
*
240245
* @param panel The child view that was slid to a expanded position
241246
*/
242247
public void onPanelExpanded(View panel);
243248

244249
/**
245250
* Called when a sliding panel becomes anchored.
251+
*
246252
* @param panel The child view that was slid to a anchored position
247253
*/
248254
public void onPanelAnchored(View panel);
249255

250256
/**
251257
* Called when a sliding panel becomes completely hidden.
258+
*
252259
* @param panel The child view that was slid to a hidden position
253260
*/
254261
public void onPanelHidden(View panel);
@@ -262,15 +269,19 @@ public static class SimplePanelSlideListener implements PanelSlideListener {
262269
@Override
263270
public void onPanelSlide(View panel, float slideOffset) {
264271
}
272+
265273
@Override
266274
public void onPanelCollapsed(View panel) {
267275
}
276+
268277
@Override
269278
public void onPanelExpanded(View panel) {
270279
}
280+
271281
@Override
272282
public void onPanelAnchored(View panel) {
273283
}
284+
274285
@Override
275286
public void onPanelHidden(View panel) {
276287
}
@@ -287,7 +298,7 @@ public SlidingUpPanelLayout(Context context, AttributeSet attrs) {
287298
public SlidingUpPanelLayout(Context context, AttributeSet attrs, int defStyle) {
288299
super(context, attrs, defStyle);
289300

290-
if(isInEditMode()) {
301+
if (isInEditMode()) {
291302
mShadowDrawable = null;
292303
mDragHelper = null;
293304
return;
@@ -344,7 +355,6 @@ public SlidingUpPanelLayout(Context context, AttributeSet attrs, int defStyle) {
344355
} else {
345356
mShadowDrawable = getResources().getDrawable(R.drawable.below_shadow);
346357
}
347-
348358
} else {
349359
mShadowDrawable = null;
350360
}
@@ -401,6 +411,7 @@ public int getCoveredFadeColor() {
401411

402412
/**
403413
* Set sliding enabled flag
414+
*
404415
* @param enabled flag value
405416
*/
406417
public void setTouchEnabled(boolean enabled) {
@@ -433,7 +444,7 @@ public void setPanelHeight(int val) {
433444
}
434445
}
435446

436-
protected void smoothToBottom(){
447+
protected void smoothToBottom() {
437448
smoothSlideTo(0, 0);
438449
}
439450

@@ -468,7 +479,7 @@ public int getPanelHeight() {
468479
*/
469480
public int getCurrentParalaxOffset() {
470481
// Clamp slide offset at zero for parallax computation;
471-
int offset = (int)(mParallaxOffset * Math.max(mSlideOffset, 0));
482+
int offset = (int) (mParallaxOffset * Math.max(mSlideOffset, 0));
472483
return mIsSlidingUp ? -offset : offset;
473484
}
474485

@@ -502,6 +513,7 @@ public void setMinFlingVelocity(int val) {
502513

503514
/**
504515
* Sets the panel slide listener
516+
*
505517
* @param listener
506518
*/
507519
public void setPanelSlideListener(PanelSlideListener listener) {
@@ -526,7 +538,7 @@ public void setDragView(View dragView) {
526538
@Override
527539
public void onClick(View v) {
528540
if (!isEnabled() || !isTouchEnabled()) return;
529-
if (mSlideState != PanelState.EXPANDED && mSlideState != PanelState.ANCHORED) {
541+
if (mSlideState != PanelState.EXPANDED && mSlideState != PanelState.ANCHORED) {
530542
if (mAnchorPoint < 1.0f) {
531543
setPanelState(PanelState.ANCHORED);
532544
} else {
@@ -536,7 +548,8 @@ public void onClick(View v) {
536548
setPanelState(PanelState.COLLAPSED);
537549
}
538550
}
539-
});;
551+
});
552+
;
540553
}
541554
}
542555

@@ -583,6 +596,7 @@ public float getAnchorPoint() {
583596

584597
/**
585598
* Sets whether or not the panel overlays the content
599+
*
586600
* @param overlayed
587601
*/
588602
public void setOverlayed(boolean overlayed) {
@@ -598,6 +612,7 @@ public boolean isOverlayed() {
598612

599613
/**
600614
* Sets whether or not the main content is clipped to the top of the panel
615+
*
601616
* @param overlayed
602617
*/
603618
public void setClipPanel(boolean clip) {
@@ -800,19 +815,19 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
800815

801816
if (mFirstLayout) {
802817
switch (mSlideState) {
803-
case EXPANDED:
804-
mSlideOffset = 1.0f;
805-
break;
806-
case ANCHORED:
807-
mSlideOffset = mAnchorPoint;
808-
break;
809-
case HIDDEN:
810-
int newTop = computePanelTopPosition(0.0f) + (mIsSlidingUp ? +mPanelHeight : -mPanelHeight);
811-
mSlideOffset = computeSlideOffset(newTop);
812-
break;
813-
default:
814-
mSlideOffset = 0.f;
815-
break;
818+
case EXPANDED:
819+
mSlideOffset = 1.0f;
820+
break;
821+
case ANCHORED:
822+
mSlideOffset = mAnchorPoint;
823+
break;
824+
case HIDDEN:
825+
int newTop = computePanelTopPosition(0.0f) + (mIsSlidingUp ? +mPanelHeight : -mPanelHeight);
826+
mSlideOffset = computeSlideOffset(newTop);
827+
break;
828+
default:
829+
mSlideOffset = 0.f;
830+
break;
816831
}
817832
}
818833

@@ -886,7 +901,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
886901
final float ady = Math.abs(y - mInitialMotionY);
887902
final int dragSlop = mDragHelper.getTouchSlop();
888903

889-
if ((ady > dragSlop && adx > ady) || !isViewUnder(mDragView, (int)mInitialMotionX, (int)mInitialMotionY)) {
904+
if ((ady > dragSlop && adx > ady) || !isViewUnder(mDragView, (int) mInitialMotionX, (int) mInitialMotionY)) {
890905
mDragHelper.cancel();
891906
mIsUnableToDrag = true;
892907
return false;
@@ -909,11 +924,10 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
909924
}
910925

911926
@Override
912-
public boolean onTouchEvent(MotionEvent ev) {
927+
public boolean onTouchEvent(@NonNull MotionEvent ev) {
913928
if (!isEnabled() || !isTouchEnabled()) {
914929
return super.onTouchEvent(ev);
915930
}
916-
final int action = MotionEventCompat.getActionMasked(ev);
917931
try {
918932
mDragHelper.processTouchEvent(ev);
919933
return true;
@@ -924,15 +938,14 @@ public boolean onTouchEvent(MotionEvent ev) {
924938
}
925939

926940
@Override
927-
public boolean dispatchTouchEvent(MotionEvent ev) {
941+
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
928942
final int action = MotionEventCompat.getActionMasked(ev);
929943

930944
if (!isEnabled() || !isTouchEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
931945
mDragHelper.cancel();
932946
return super.dispatchTouchEvent(ev);
933947
}
934948

935-
final float x = ev.getX();
936949
final float y = ev.getY();
937950

938951
if (action == MotionEvent.ACTION_DOWN) {
@@ -985,14 +998,18 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
985998
// Was the panel handling the touch previously?
986999
// Then we need to rejigger things so that the
9871000
// child gets a proper down event.
988-
if (!mIsScrollableViewHandlingTouch) {
1001+
if (!mIsScrollableViewHandlingTouch && mDragHelper.isDragging()) {
9891002
mDragHelper.cancel();
9901003
ev.setAction(MotionEvent.ACTION_DOWN);
9911004
}
9921005

9931006
mIsScrollableViewHandlingTouch = true;
9941007
return super.dispatchTouchEvent(ev);
9951008
}
1009+
} else if (action == MotionEvent.ACTION_UP && mIsScrollableViewHandlingTouch) {
1010+
// If the scrollable view was handling the touch and we receive an up
1011+
// we want to clear any previous dragging state so we don't intercept a touch stream accidentally
1012+
mDragHelper.setDragState(ViewDragHelper.STATE_IDLE);
9961013
}
9971014

9981015
// In all other cases, just let the default behavior take over.
@@ -1017,12 +1034,12 @@ private int getScrollableViewScrollPosition() {
10171034
if (mIsSlidingUp) {
10181035
return mScrollableView.getScrollY();
10191036
} else {
1020-
ScrollView sv = ((ScrollView)mScrollableView);
1037+
ScrollView sv = ((ScrollView) mScrollableView);
10211038
View child = sv.getChildAt(0);
10221039
return (child.getBottom() - (sv.getHeight() + sv.getScrollY()));
10231040
}
1024-
} else if (mScrollableView instanceof ListView && ((ListView)mScrollableView).getChildCount() > 0) {
1025-
ListView lv = ((ListView)mScrollableView);
1041+
} else if (mScrollableView instanceof ListView && ((ListView) mScrollableView).getChildCount() > 0) {
1042+
ListView lv = ((ListView) mScrollableView);
10261043
if (lv.getAdapter() == null) return 0;
10271044
if (mIsSlidingUp) {
10281045
View firstChild = lv.getChildAt(0);
@@ -1066,6 +1083,7 @@ private float computeSlideOffset(int topPosition) {
10661083

10671084
/**
10681085
* Returns the current state of the panel as an enum.
1086+
*
10691087
* @return the current panel state
10701088
*/
10711089
public PanelState getPanelState() {
@@ -1074,6 +1092,7 @@ public PanelState getPanelState() {
10741092

10751093
/**
10761094
* Change panel state to the given state with
1095+
*
10771096
* @param state - new panel state
10781097
*/
10791098
public void setPanelState(PanelState state) {
@@ -1135,7 +1154,7 @@ private void onPanelDragged(int newTop) {
11351154
dispatchOnPanelSlide(mSlideableView);
11361155
// If the slide offset is negative, and overlay is not on, we need to increase the
11371156
// height of the main content
1138-
LayoutParams lp = (LayoutParams)mMainView.getLayoutParams();
1157+
LayoutParams lp = (LayoutParams) mMainView.getLayoutParams();
11391158
int defaultHeight = getHeight() - getPaddingBottom() - getPaddingTop() - mPanelHeight;
11401159

11411160
if (mSlideOffset <= 0 && !mOverlayContent) {
@@ -1190,7 +1209,7 @@ protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
11901209
* Smoothly animate mDraggingPane to the target X position within its range.
11911210
*
11921211
* @param slideOffset position to animate to
1193-
* @param velocity initial velocity in case of fling, or 0.
1212+
* @param velocity initial velocity in case of fling, or 0.
11941213
*/
11951214
boolean smoothSlideTo(float slideOffset, int velocity) {
11961215
if (!isEnabled()) {
@@ -1244,12 +1263,12 @@ public void draw(Canvas c) {
12441263
/**
12451264
* Tests scrollability within child views of v given a delta of dx.
12461265
*
1247-
* @param v View to test for horizontal scrollability
1266+
* @param v View to test for horizontal scrollability
12481267
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
12491268
* or just its children (false).
1250-
* @param dx Delta scrolled in pixels
1251-
* @param x X coordinate of the active touch point
1252-
* @param y Y coordinate of the active touch point
1269+
* @param dx Delta scrolled in pixels
1270+
* @param x X coordinate of the active touch point
1271+
* @param y Y coordinate of the active touch point
12531272
* @return true if child views of v can be scrolled by delta of dx.
12541273
*/
12551274
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
@@ -1416,8 +1435,8 @@ public int clampViewPositionVertical(View child, int top, int dy) {
14161435
}
14171436

14181437
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
1419-
private static final int[] ATTRS = new int[] {
1420-
android.R.attr.layout_weight
1438+
private static final int[] ATTRS = new int[]{
1439+
android.R.attr.layout_weight
14211440
};
14221441

14231442
public LayoutParams() {
@@ -1446,7 +1465,6 @@ public LayoutParams(Context c, AttributeSet attrs) {
14461465
final TypedArray a = c.obtainStyledAttributes(attrs, ATTRS);
14471466
a.recycle();
14481467
}
1449-
14501468
}
14511469

14521470
static class SavedState extends BaseSavedState {
@@ -1473,15 +1491,15 @@ public void writeToParcel(Parcel out, int flags) {
14731491

14741492
public static final Parcelable.Creator<SavedState> CREATOR =
14751493
new Parcelable.Creator<SavedState>() {
1476-
@Override
1477-
public SavedState createFromParcel(Parcel in) {
1478-
return new SavedState(in);
1479-
}
1494+
@Override
1495+
public SavedState createFromParcel(Parcel in) {
1496+
return new SavedState(in);
1497+
}
14801498

1481-
@Override
1482-
public SavedState[] newArray(int size) {
1483-
return new SavedState[size];
1484-
}
1485-
};
1499+
@Override
1500+
public SavedState[] newArray(int size) {
1501+
return new SavedState[size];
1502+
}
1503+
};
14861504
}
14871505
}

0 commit comments

Comments
 (0)