@@ -206,10 +206,10 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
206
206
if (dragHelper?.viewDragState == ViewDragHelper .STATE_SETTLING ) {
207
207
dragHelper?.abort()
208
208
}
209
- require(state != = PanelState .DRAGGING ) { " Panel state cannot be DRAGGING during state set" }
209
+ require(state != = PanelState .DRAGGING ) { " Panel state can't be DRAGGING during state set" }
210
210
if (! isEnabled
211
- || ! firstLayout && slideableView == null
212
- || state == = slideState || slideState == = PanelState .DRAGGING
211
+ || ( ! firstLayout && ( slideableView == null ))
212
+ || ( state == = slideState) || ( slideState == = PanelState .DRAGGING )
213
213
) return
214
214
if (firstLayout) {
215
215
setPanelStateInternal(state)
@@ -314,74 +314,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
314
314
} else {
315
315
var scrollerInterpolator: Interpolator ? = null
316
316
if (attrs != null ) {
317
- val defAttrs = context.obtainStyledAttributes(attrs, DEFAULT_ATTRS )
318
- try {
319
- val gravity = defAttrs.getInt(0 , Gravity .NO_GRAVITY )
320
- setGravity(gravity)
321
- } finally {
322
- defAttrs.recycle()
323
- }
324
- val ta = context.obtainStyledAttributes(attrs, R .styleable.SlidingUpPanelLayout )
325
- try {
326
- panelHeight = ta.getDimensionPixelSize(
327
- R .styleable.SlidingUpPanelLayout_umanoPanelHeight ,
328
- - 1
329
- )
330
- shadowHeight = ta.getDimensionPixelSize(
331
- R .styleable.SlidingUpPanelLayout_umanoShadowHeight ,
332
- - 1
333
- )
334
- parallaxOffset = ta.getDimensionPixelSize(
335
- R .styleable.SlidingUpPanelLayout_umanoParallaxOffset ,
336
- - 1
337
- )
338
- minFlingVelocity = ta.getInt(
339
- R .styleable.SlidingUpPanelLayout_umanoFlingVelocity ,
340
- DEFAULT_MIN_FLING_VELOCITY
341
- )
342
- coveredFadeColor = ta.getColor(
343
- R .styleable.SlidingUpPanelLayout_umanoFadeColor ,
344
- DEFAULT_FADE_COLOR
345
- )
346
- dragViewResId =
347
- ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoDragView , - 1 )
348
- scrollableViewResId =
349
- ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoScrollableView , - 1 )
350
- aboveShadowResId =
351
- ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoAboveShadowStyle , - 1 )
352
- belowShadowResId =
353
- ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoBelowShadowStyle , - 1 )
354
- overlayContent = ta.getBoolean(
355
- R .styleable.SlidingUpPanelLayout_umanoOverlay ,
356
- DEFAULT_OVERLAY_FLAG
357
- )
358
- clipPanel = ta.getBoolean(
359
- R .styleable.SlidingUpPanelLayout_umanoClipPanel ,
360
- DEFAULT_CLIP_PANEL_FLAG
361
- )
362
- anchorPoint = ta.getFloat(
363
- R .styleable.SlidingUpPanelLayout_umanoAnchorPoint ,
364
- DEFAULT_ANCHOR_POINT
365
- )
366
- maxSlideOffset = ta.getFloat(
367
- R .styleable.SlidingUpPanelLayout_umanoMaxSlidingOffset ,
368
- DEFAULT_MAX_SLIDING_OFFSET
369
- )
370
- slideState = PanelState .values()[ta.getInt(
371
- R .styleable.SlidingUpPanelLayout_umanoInitialState ,
372
- DEFAULT_SLIDE_STATE .ordinal
373
- )]
374
- val interpolatorResId = ta.getResourceId(
375
- R .styleable.SlidingUpPanelLayout_umanoScrollInterpolator ,
376
- - 1
377
- )
378
- if (interpolatorResId != - 1 ) {
379
- scrollerInterpolator =
380
- AnimationUtils .loadInterpolator(context, interpolatorResId)
381
- }
382
- } finally {
383
- ta.recycle()
384
- }
317
+ scrollerInterpolator = initAttributes(context, attrs)
385
318
}
386
319
val density = context.resources.displayMetrics.density
387
320
if (panelHeight == - 1 ) {
@@ -394,30 +327,105 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
394
327
parallaxOffset = (DEFAULT_PARALLAX_OFFSET * density).toInt()
395
328
}
396
329
// If the shadow height is zero, don't show the shadow
397
- shadowDrawable = if (shadowHeight > 0 ) {
398
- if (isSlidingUp) {
399
- if (aboveShadowResId == - 1 ) {
400
- ContextCompat .getDrawable(context, R .drawable.above_shadow)
401
- } else {
402
- ContextCompat .getDrawable(context, aboveShadowResId)
403
- }
404
- } else {
405
- if (belowShadowResId == - 1 ) {
406
- ContextCompat .getDrawable(context, R .drawable.below_shadow)
407
- } else {
408
- ContextCompat .getDrawable(context, belowShadowResId)
409
- }
410
- }
411
- } else {
412
- null
413
- }
330
+ shadowDrawable = makeShadowDrawable(context)
414
331
setWillNotDraw(false )
415
332
dragHelper = create(this , 0.5f , scrollerInterpolator, DragHelperCallback ())
416
333
dragHelper?.minVelocity = minFlingVelocity * density
417
334
touchEnabled = true
418
335
}
419
336
}
420
337
338
+ private fun initAttributes (context : Context , attrs : AttributeSet ? ): Interpolator ? {
339
+ val defAttrs = context.obtainStyledAttributes(attrs, DEFAULT_ATTRS )
340
+ try {
341
+ val gravity = defAttrs.getInt(0 , Gravity .NO_GRAVITY )
342
+ setGravity(gravity)
343
+ } finally {
344
+ defAttrs.recycle()
345
+ }
346
+ val ta = context.obtainStyledAttributes(attrs, R .styleable.SlidingUpPanelLayout )
347
+ var scrollerInterpolator: Interpolator ? = null
348
+ try {
349
+ panelHeight = ta.getDimensionPixelSize(
350
+ R .styleable.SlidingUpPanelLayout_umanoPanelHeight ,
351
+ - 1
352
+ )
353
+ shadowHeight = ta.getDimensionPixelSize(
354
+ R .styleable.SlidingUpPanelLayout_umanoShadowHeight ,
355
+ - 1
356
+ )
357
+ parallaxOffset = ta.getDimensionPixelSize(
358
+ R .styleable.SlidingUpPanelLayout_umanoParallaxOffset ,
359
+ - 1
360
+ )
361
+ minFlingVelocity = ta.getInt(
362
+ R .styleable.SlidingUpPanelLayout_umanoFlingVelocity ,
363
+ DEFAULT_MIN_FLING_VELOCITY
364
+ )
365
+ coveredFadeColor = ta.getColor(
366
+ R .styleable.SlidingUpPanelLayout_umanoFadeColor ,
367
+ DEFAULT_FADE_COLOR
368
+ )
369
+ dragViewResId =
370
+ ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoDragView , - 1 )
371
+ scrollableViewResId =
372
+ ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoScrollableView , - 1 )
373
+ aboveShadowResId =
374
+ ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoAboveShadowStyle , - 1 )
375
+ belowShadowResId =
376
+ ta.getResourceId(R .styleable.SlidingUpPanelLayout_umanoBelowShadowStyle , - 1 )
377
+ overlayContent = ta.getBoolean(
378
+ R .styleable.SlidingUpPanelLayout_umanoOverlay ,
379
+ DEFAULT_OVERLAY_FLAG
380
+ )
381
+ clipPanel = ta.getBoolean(
382
+ R .styleable.SlidingUpPanelLayout_umanoClipPanel ,
383
+ DEFAULT_CLIP_PANEL_FLAG
384
+ )
385
+ anchorPoint = ta.getFloat(
386
+ R .styleable.SlidingUpPanelLayout_umanoAnchorPoint ,
387
+ DEFAULT_ANCHOR_POINT
388
+ )
389
+ maxSlideOffset = ta.getFloat(
390
+ R .styleable.SlidingUpPanelLayout_umanoMaxSlidingOffset ,
391
+ DEFAULT_MAX_SLIDING_OFFSET
392
+ )
393
+ slideState = PanelState .values()[ta.getInt(
394
+ R .styleable.SlidingUpPanelLayout_umanoInitialState ,
395
+ DEFAULT_SLIDE_STATE .ordinal
396
+ )]
397
+ val interpolatorResId = ta.getResourceId(
398
+ R .styleable.SlidingUpPanelLayout_umanoScrollInterpolator ,
399
+ - 1
400
+ )
401
+ if (interpolatorResId != - 1 ) {
402
+ scrollerInterpolator =
403
+ AnimationUtils .loadInterpolator(context, interpolatorResId)
404
+ }
405
+ } finally {
406
+ ta.recycle()
407
+ }
408
+ return scrollerInterpolator
409
+ }
410
+
411
+ private fun makeShadowDrawable (context : Context ) = if (shadowHeight > 0 ) {
412
+ if (isSlidingUp) {
413
+ if (aboveShadowResId == - 1 ) {
414
+ ContextCompat .getDrawable(context, R .drawable.above_shadow)
415
+ } else {
416
+ ContextCompat .getDrawable(context, aboveShadowResId)
417
+ }
418
+ } else {
419
+ if (belowShadowResId == - 1 ) {
420
+ ContextCompat .getDrawable(context, R .drawable.below_shadow)
421
+ } else {
422
+ ContextCompat .getDrawable(context, belowShadowResId)
423
+ }
424
+ }
425
+ } else {
426
+ null
427
+ }
428
+
421
429
/* *
422
430
* Set the Drag View after the view is inflated
423
431
*/
@@ -493,6 +501,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
493
501
}
494
502
495
503
fun addPanelSlideListener (listener : PanelSlideListener ) {
504
+ // TODO why do we synchronize again, this is already a costly CopyOnWriteArrayList? Maybe should use Kotlin Collections.synchronizedList() wrapper instead?
496
505
synchronized(panelSlideListeners) { panelSlideListeners.add(listener) }
497
506
}
498
507
@@ -608,11 +617,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
608
617
* previous state and the new state. Careful: this process is synchronized to avoid out-of-order
609
618
* callbacks. Call this method sparingly.
610
619
*/
611
- fun dispatchOnPanelStateChanged (
612
- panel : View ,
613
- previousState : PanelState ,
614
- newState : PanelState ,
615
- ) {
620
+ fun dispatchOnPanelStateChanged (panel : View , previousState : PanelState , newState : PanelState ) {
616
621
synchronized(panelSlideListeners) {
617
622
for (l in panelSlideListeners) {
618
623
l.onPanelStateChanged(panel, previousState, newState)
@@ -622,13 +627,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
622
627
}
623
628
624
629
fun updateObscuredViewVisibility () {
625
- if (childCount == 0 ) {
626
- return
627
- }
628
- val leftBound = paddingLeft
629
- val rightBound = width - paddingRight
630
- val topBound = paddingTop
631
- val bottomBound = height - paddingBottom
630
+ if (childCount == 0 ) return
632
631
val left: Int
633
632
val right: Int
634
633
val top: Int
@@ -645,10 +644,10 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
645
644
left = 0
646
645
}
647
646
val child = getChildAt(0 )
648
- val clampedChildLeft = max(leftBound , child.left)
649
- val clampedChildTop = max(topBound , child.top)
650
- val clampedChildRight = min(rightBound , child.right)
651
- val clampedChildBottom = min(bottomBound , child.bottom)
647
+ val clampedChildLeft = max(paddingLeft , child.left)
648
+ val clampedChildTop = max(paddingTop , child.top)
649
+ val clampedChildRight = min(width - paddingRight , child.right)
650
+ val clampedChildBottom = min(height - paddingBottom , child.bottom)
652
651
val vis: Int =
653
652
if (clampedChildLeft >= left && clampedChildTop >= top && clampedChildRight <= right && clampedChildBottom <= bottom) {
654
653
INVISIBLE
@@ -728,13 +727,16 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
728
727
// See https://github.com/umano/AndroidSlidingUpPanel/issues/412.
729
728
height - = lp.topMargin
730
729
}
731
- var childWidthSpec: Int
732
- childWidthSpec = if (lp.width == MarginLayoutParams .WRAP_CONTENT ) {
733
- MeasureSpec .makeMeasureSpec(width, MeasureSpec .AT_MOST )
734
- } else if (lp.width == MarginLayoutParams .MATCH_PARENT ) {
735
- MeasureSpec .makeMeasureSpec(width, MeasureSpec .EXACTLY )
736
- } else {
737
- MeasureSpec .makeMeasureSpec(lp.width, MeasureSpec .EXACTLY )
730
+ val childWidthSpec: Int = when (lp.width) {
731
+ MarginLayoutParams .WRAP_CONTENT -> {
732
+ MeasureSpec .makeMeasureSpec(width, MeasureSpec .AT_MOST )
733
+ }
734
+ MarginLayoutParams .MATCH_PARENT -> {
735
+ MeasureSpec .makeMeasureSpec(width, MeasureSpec .EXACTLY )
736
+ }
737
+ else -> {
738
+ MeasureSpec .makeMeasureSpec(lp.width, MeasureSpec .EXACTLY )
739
+ }
738
740
}
739
741
var childHeightSpec: Int
740
742
if (lp.height == MarginLayoutParams .WRAP_CONTENT ) {
@@ -815,7 +817,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
815
817
override fun onInterceptTouchEvent (ev : MotionEvent ): Boolean {
816
818
// If the scrollable view is handling touch, never intercept
817
819
if (isScrollableViewHandlingTouch || ! isTouchEnabled) {
818
- dragHelper!! .abort()
820
+ dragHelper? .abort()
819
821
return false
820
822
}
821
823
val action = ev.action
@@ -883,7 +885,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
883
885
override fun dispatchTouchEvent (ev : MotionEvent ): Boolean {
884
886
val action = ev.action
885
887
if (! isEnabled || ! isTouchEnabled || isUnableToDrag && action != MotionEvent .ACTION_DOWN ) {
886
- dragHelper!! .abort()
888
+ dragHelper? .abort()
887
889
return super .dispatchTouchEvent(ev)
888
890
}
889
891
val x = ev.x
@@ -1138,23 +1140,22 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
1138
1140
/* *
1139
1141
* Tests scrollability within child views of v given a delta of dx.
1140
1142
*
1141
- * @param v View to test for horizontal scrollability
1143
+ * @param view View to test for horizontal scrollability
1142
1144
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
1143
1145
* or just its children (false).
1144
1146
* @param dx Delta scrolled in pixels
1145
1147
* @param x X coordinate of the active touch point
1146
1148
* @param y Y coordinate of the active touch point
1147
1149
* @return true if child views of v can be scrolled by delta of dx.
1148
1150
*/
1149
- protected fun canScroll (v : View , checkV : Boolean , dx : Int , x : Int , y : Int ): Boolean {
1150
- if (v is ViewGroup ) {
1151
- val group = v
1152
- val scrollX = v.getScrollX()
1153
- val scrollY = v.getScrollY()
1154
- val count = group.childCount
1151
+ protected fun canScroll (view : View , checkV : Boolean , dx : Int , x : Int , y : Int ): Boolean {
1152
+ if (view is ViewGroup ) {
1153
+ val scrollX = view.getScrollX()
1154
+ val scrollY = view.getScrollY()
1155
+ val count = view.childCount
1155
1156
// Count backwards - let topmost views consume scroll distance first.
1156
1157
for (i in count - 1 downTo 0 ) {
1157
- val child = group .getChildAt(i)
1158
+ val child = view .getChildAt(i)
1158
1159
if (x + scrollX >= child.left && x + scrollX < child.right && y + scrollY >= child.top && y + scrollY < child.bottom &&
1159
1160
canScroll(
1160
1161
child, true , dx, x + scrollX - child.left,
@@ -1165,7 +1166,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
1165
1166
}
1166
1167
}
1167
1168
}
1168
- return checkV && v .canScrollHorizontally(- dx)
1169
+ return checkV && view .canScrollHorizontally(- dx)
1169
1170
}
1170
1171
1171
1172
override fun generateDefaultLayoutParams (): LayoutParams {
@@ -1234,13 +1235,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
1234
1235
setAllChildrenVisible()
1235
1236
}
1236
1237
1237
- override fun onViewPositionChanged (
1238
- changedView : View ? ,
1239
- left : Int ,
1240
- top : Int ,
1241
- dx : Int ,
1242
- dy : Int ,
1243
- ) {
1238
+ override fun onViewPositionChanged (changedView : View ? , left : Int , top : Int , dx : Int , dy : Int ) {
1244
1239
onPanelDragged(top)
1245
1240
invalidate()
1246
1241
}
@@ -1304,5 +1299,4 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
1304
1299
}
1305
1300
}
1306
1301
}
1307
-
1308
1302
}
0 commit comments