-
Notifications
You must be signed in to change notification settings - Fork 3
/
WrappingSlidingPaneLayout.java
1408 lines (1199 loc) · 49.9 KB
/
WrappingSlidingPaneLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.newviewsplayground;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.AccessibilityDelegateCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.*;
import android.view.accessibility.AccessibilityEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* WrappingSlidingPaneLayout is a fork from SlidingPaneLayout which uses
* the pane size to determine the scrollable area. This allows you to have
* panes that always appear by setting the first child to match_parent and
* the second to a fixed width or wrap_content.
*
* The pane will only scroll enough to display its view and won't overlay
* the entire layout.
*/
public class WrappingSlidingPaneLayout extends ViewGroup {
private static final String TAG = "WrappingSlidingPaneLayout";
/**
* Default size of the overhang for a pane in the open state.
* At least this much of a sliding pane will remain visible.
* This indicates that there is more content available and provides
* a "physical" edge to grab to pull it closed.
*/
private static final int DEFAULT_OVERHANG_SIZE = 32; // dp;
/**
* If no fade color is given by default it will fade to 80% gray.
*/
private static final int DEFAULT_FADE_COLOR = 0xcccccccc;
/**
* The fade color used for the sliding panel. 0 = no fading.
*/
private int mSliderFadeColor = DEFAULT_FADE_COLOR;
/**
* Minimum velocity that will be detected as a fling
*/
private static final int MIN_FLING_VELOCITY = 400; // dips per second
/**
* The fade color used for the panel covered by the slider. 0 = no fading.
*/
private int mCoveredFadeColor;
/**
* Drawable used to draw the shadow between panes.
*/
private Drawable mShadowDrawable;
/**
* The size of the overhang in pixels.
* This is the minimum section of the sliding panel that will
* be visible in the open state to allow for a closing drag.
*/
private final int mOverhangSize;
/**
* True if a panel can slide with the current measurements
*/
private boolean mCanSlide;
/**
* The child view that can slide, if any.
*/
private View mSlideableView;
/**
* How far the panel is offset from its closed position.
* range [0, 1] where 0 = closed, 1 = open.
*/
private float mSlideOffset;
/**
* How far the non-sliding panel is parallaxed from its usual position when open.
* range [0, 1]
*/
private float mParallaxOffset;
/**
* How far in pixels the slideable panel may move.
*/
private int mSlideRange;
/**
* A panel view is locked into internal scrolling or another condition that
* is preventing a drag.
*/
private boolean mIsUnableToDrag;
/**
* Distance in pixels to parallax the fixed pane by when fully closed
*/
private int mParallaxBy;
private float mInitialMotionX;
private float mInitialMotionY;
private PanelSlideListener mPanelSlideListener;
private final ViewDragHelper mDragHelper;
/**
* Stores whether or not the pane was open the last time it was slideable.
* If open/close operations are invoked this state is modified. Used by
* instance state save/restore.
*/
private boolean mPreservedOpenState;
private boolean mFirstLayout = true;
private final Rect mTmpRect = new Rect();
private final ArrayList<DisableLayerRunnable> mPostedRunnables =
new ArrayList<DisableLayerRunnable>();
static final SlidingPanelLayoutImpl IMPL;
static {
final int deviceVersion = Build.VERSION.SDK_INT;
if (deviceVersion >= 17) {
IMPL = new SlidingPanelLayoutImplJBMR1();
} else if (deviceVersion >= 16) {
IMPL = new SlidingPanelLayoutImplJB();
} else {
IMPL = new SlidingPanelLayoutImplBase();
}
}
/**
* Listener for monitoring events about sliding panes.
*/
public interface PanelSlideListener {
/**
* Called when a sliding pane's position changes.
* @param panel The child view that was moved
* @param slideOffset The new offset of this sliding pane within its range, from 0-1
*/
public void onPanelSlide(View panel, float slideOffset);
/**
* Called when a sliding pane becomes slid completely open. The pane may or may not
* be interactive at this point depending on how much of the pane is visible.
* @param panel The child view that was slid to an open position, revealing other panes
*/
public void onPanelOpened(View panel);
/**
* Called when a sliding pane becomes slid completely closed. The pane is now guaranteed
* to be interactive. It may now obscure other views in the layout.
* @param panel The child view that was slid to a closed position
*/
public void onPanelClosed(View panel);
}
/**
* No-op stubs for {@link PanelSlideListener}. If you only want to implement a subset
* of the listener methods you can extend this instead of implement the full interface.
*/
public static class SimplePanelSlideListener implements PanelSlideListener {
@Override
public void onPanelSlide(View panel, float slideOffset) {
}
@Override
public void onPanelOpened(View panel) {
}
@Override
public void onPanelClosed(View panel) {
}
}
public WrappingSlidingPaneLayout(Context context) {
this(context, null);
}
public WrappingSlidingPaneLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WrappingSlidingPaneLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final float density = context.getResources().getDisplayMetrics().density;
mOverhangSize = (int) (DEFAULT_OVERHANG_SIZE * density + 0.5f);
final ViewConfiguration viewConfig = ViewConfiguration.get(context);
setWillNotDraw(false);
ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegate());
ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
mDragHelper = ViewDragHelper.create(this, 0.5f, new DragHelperCallback());
mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
mDragHelper.setMinVelocity(MIN_FLING_VELOCITY * density);
}
/**
* Set a distance to parallax the lower pane by when the upper pane is in its
* fully closed state. The lower pane will scroll between this position and
* its fully open state.
*
* @param parallaxBy Distance to parallax by in pixels
*/
public void setParallaxDistance(int parallaxBy) {
mParallaxBy = parallaxBy;
requestLayout();
}
/**
* @return The distance the lower pane will parallax by when the upper pane is fully closed.
*
* @see #setParallaxDistance(int)
*/
public int getParallaxDistance() {
return mParallaxBy;
}
/**
* Set the color used to fade the sliding pane out when it is slid most of the way offscreen.
*
* @param color An ARGB-packed color value
*/
public void setSliderFadeColor(int color) {
mSliderFadeColor = color;
}
/**
* @return The ARGB-packed color value used to fade the sliding pane
*/
public int getSliderFadeColor() {
return mSliderFadeColor;
}
/**
* Set the color used to fade the pane covered by the sliding pane out when the pane
* will become fully covered in the closed state.
*
* @param color An ARGB-packed color value
*/
public void setCoveredFadeColor(int color) {
mCoveredFadeColor = color;
}
/**
* @return The ARGB-packed color value used to fade the fixed pane
*/
public int getCoveredFadeColor() {
return mCoveredFadeColor;
}
public void setPanelSlideListener(PanelSlideListener listener) {
mPanelSlideListener = listener;
}
void dispatchOnPanelSlide(View panel) {
if (mPanelSlideListener != null) {
mPanelSlideListener.onPanelSlide(panel, mSlideOffset);
}
}
void dispatchOnPanelOpened(View panel) {
if (mPanelSlideListener != null) {
mPanelSlideListener.onPanelOpened(panel);
}
sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
}
void dispatchOnPanelClosed(View panel) {
if (mPanelSlideListener != null) {
mPanelSlideListener.onPanelClosed(panel);
}
sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
}
void updateObscuredViewsVisibility(View panel) {
final int leftBound = getPaddingLeft();
final int rightBound = getWidth() - getPaddingRight();
final int topBound = getPaddingTop();
final int bottomBound = getHeight() - getPaddingBottom();
final int left;
final int right;
final int top;
final int bottom;
if (panel != null && hasOpaqueBackground(panel)) {
left = panel.getLeft();
right = panel.getRight();
top = panel.getTop();
bottom = panel.getBottom();
} else {
left = right = top = bottom = 0;
}
for (int i = 0, childCount = getChildCount(); i < childCount; i++) {
final View child = getChildAt(i);
if (child == panel) {
// There are still more children above the panel but they won't be affected.
break;
}
final int clampedChildLeft = Math.max(leftBound, child.getLeft());
final int clampedChildTop = Math.max(topBound, child.getTop());
final int clampedChildRight = Math.min(rightBound, child.getRight());
final int clampedChildBottom = Math.min(bottomBound, child.getBottom());
final int vis;
if (clampedChildLeft >= left && clampedChildTop >= top &&
clampedChildRight <= right && clampedChildBottom <= bottom) {
vis = INVISIBLE;
} else {
vis = VISIBLE;
}
child.setVisibility(vis);
}
}
void setAllChildrenVisible() {
for (int i = 0, childCount = getChildCount(); i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == INVISIBLE) {
child.setVisibility(VISIBLE);
}
}
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
if (bg != null) {
return bg.getOpacity() == PixelFormat.OPAQUE;
}
return false;
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mFirstLayout = true;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mFirstLayout = true;
for (int i = 0, count = mPostedRunnables.size(); i < count; i++) {
final DisableLayerRunnable dlr = mPostedRunnables.get(i);
dlr.run();
}
mPostedRunnables.clear();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("Width must have an exact value or MATCH_PARENT");
} else if (heightMode == MeasureSpec.UNSPECIFIED) {
throw new IllegalStateException("Height must not be UNSPECIFIED");
}
int layoutHeight = 0;
int maxLayoutHeight = -1;
switch (heightMode) {
case MeasureSpec.EXACTLY:
layoutHeight = maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom();
break;
case MeasureSpec.AT_MOST:
maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom();
break;
}
float weightSum = 0;
boolean canSlide = false;
int widthRemaining = widthSize - getPaddingLeft() - getPaddingRight();
final int childCount = getChildCount();
if (childCount > 2) {
Log.e(TAG, "onMeasure: More than two child views are not supported.");
}
// We'll find the current one below.
mSlideableView = null;
// First pass. Measure based on child LayoutParams width/height.
// Weight will incur a second pass.
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (child.getVisibility() == GONE) {
lp.dimWhenOffset = false;
continue;
}
if (lp.weight > 0) {
weightSum += lp.weight;
// If we have no width, weight is the only contributor to the final size.
// Measure this view on the weight pass only.
if (lp.width == 0) continue;
}
int childWidthSpec;
final int horizontalMargin = lp.leftMargin + lp.rightMargin;
if (lp.width == LayoutParams.WRAP_CONTENT) {
childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin,
MeasureSpec.AT_MOST);
} else if (lp.width == LayoutParams.FILL_PARENT) {
childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin,
MeasureSpec.EXACTLY);
} else {
childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
}
int childHeightSpec;
if (lp.height == LayoutParams.WRAP_CONTENT) {
childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST);
} else if (lp.height == LayoutParams.FILL_PARENT) {
childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
}
child.measure(childWidthSpec, childHeightSpec);
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
if (heightMode == MeasureSpec.AT_MOST && childHeight > layoutHeight) {
layoutHeight = Math.min(childHeight, maxLayoutHeight);
}
widthRemaining -= childWidth;
canSlide |= lp.slideable = widthRemaining < 0;
if (lp.slideable) {
mSlideableView = child;
}
}
// Resolve weight and make sure non-sliding panels are smaller than the full screen.
if (canSlide || weightSum > 0) {
final int fixedPanelWidthLimit = widthSize - mOverhangSize;
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final boolean skippedFirstPass = lp.width == 0 && lp.weight > 0;
final int measuredWidth = skippedFirstPass ? 0 : child.getMeasuredWidth();
if (canSlide && child != mSlideableView) {
if (lp.width < 0 && (measuredWidth > fixedPanelWidthLimit || lp.weight > 0)) {
// Fixed panels in a sliding configuration should
// be clamped to the fixed panel limit.
final int childHeightSpec;
if (skippedFirstPass) {
// Do initial height measurement if we skipped measuring this view
// the first time around.
if (lp.height == LayoutParams.WRAP_CONTENT) {
childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight,
MeasureSpec.AT_MOST);
} else if (lp.height == LayoutParams.FILL_PARENT) {
childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height,
MeasureSpec.EXACTLY);
}
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(
child.getMeasuredHeight(), MeasureSpec.EXACTLY);
}
final int childWidthSpec = MeasureSpec.makeMeasureSpec(
fixedPanelWidthLimit, MeasureSpec.EXACTLY);
child.measure(childWidthSpec, childHeightSpec);
}
} else if (lp.weight > 0) {
int childHeightSpec;
if (lp.width == 0) {
// This was skipped the first time; figure out a real height spec.
if (lp.height == LayoutParams.WRAP_CONTENT) {
childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight,
MeasureSpec.AT_MOST);
} else if (lp.height == LayoutParams.FILL_PARENT) {
childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height,
MeasureSpec.EXACTLY);
}
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(
child.getMeasuredHeight(), MeasureSpec.EXACTLY);
}
if (canSlide) {
// Consume available space
final int horizontalMargin = lp.leftMargin + lp.rightMargin;
final int newWidth = widthSize - horizontalMargin;
final int childWidthSpec = MeasureSpec.makeMeasureSpec(
newWidth, MeasureSpec.EXACTLY);
if (measuredWidth != newWidth) {
child.measure(childWidthSpec, childHeightSpec);
}
} else {
// Distribute the extra width proportionally similar to LinearLayout
final int widthToDistribute = Math.max(0, widthRemaining);
final int addedWidth = (int) (lp.weight * widthToDistribute / weightSum);
final int childWidthSpec = MeasureSpec.makeMeasureSpec(
measuredWidth + addedWidth, MeasureSpec.EXACTLY);
child.measure(childWidthSpec, childHeightSpec);
}
}
}
}
setMeasuredDimension(widthSize, layoutHeight);
mCanSlide = canSlide;
if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && !canSlide) {
// Cancel scrolling in progress, it's no longer relevant.
mDragHelper.abort();
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int width = r - l;
final int paddingLeft = getPaddingLeft();
final int paddingRight = getPaddingRight();
final int paddingTop = getPaddingTop();
final int childCount = getChildCount();
int xStart = paddingLeft;
int nextXStart = xStart;
if (mFirstLayout) {
mSlideOffset = mCanSlide && mPreservedOpenState ? 1.f : 0.f;
}
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int childWidth = child.getMeasuredWidth();
int offset = 0;
if (lp.slideable) {
final int margin = lp.leftMargin + lp.rightMargin;
final int range = Math.min(nextXStart,
childWidth - paddingRight - mOverhangSize) - xStart - margin;
mSlideRange = range;
lp.dimWhenOffset = xStart + (width - childWidth) + lp.leftMargin + range + childWidth / 2 >
width - paddingRight;
xStart += (width - childWidth) + (int) (range * mSlideOffset) + lp.leftMargin;
} else if (mCanSlide && mParallaxBy != 0) {
offset = (int) ((1 - mSlideOffset) * mParallaxBy);
xStart = nextXStart;
} else {
xStart = nextXStart;
}
final int childLeft = xStart - offset;
final int childRight = childLeft + childWidth;
final int childTop = paddingTop;
final int childBottom = childTop + child.getMeasuredHeight();
child.layout(childLeft, paddingTop, childRight, childBottom);
nextXStart += child.getWidth();
}
if (mFirstLayout) {
if (mCanSlide) {
if (mParallaxBy != 0) {
parallaxOtherViews(mSlideOffset);
}
if (((LayoutParams) mSlideableView.getLayoutParams()).dimWhenOffset) {
dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor);
}
} else {
// Reset the dim level of all children; it's irrelevant when nothing moves.
for (int i = 0; i < childCount; i++) {
dimChildView(getChildAt(i), 0, mSliderFadeColor);
}
}
updateObscuredViewsVisibility(mSlideableView);
}
mFirstLayout = false;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Recalculate sliding panes and their details
if (w != oldw) {
mFirstLayout = true;
}
}
@Override
public void requestChildFocus(View child, View focused) {
super.requestChildFocus(child, focused);
if (!isInTouchMode() && !mCanSlide) {
mPreservedOpenState = child == mSlideableView;
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
// Preserve the open state based on the last view that was touched.
if (!mCanSlide && action == MotionEvent.ACTION_DOWN && getChildCount() > 1) {
// After the first things will be slideable.
final View secondChild = getChildAt(1);
if (secondChild != null) {
mPreservedOpenState = !mDragHelper.isViewUnder(secondChild,
(int) ev.getX(), (int) ev.getY());
}
}
if (!mCanSlide || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {
mDragHelper.cancel();
return super.onInterceptTouchEvent(ev);
}
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
mDragHelper.cancel();
return false;
}
boolean interceptTap = false;
switch (action) {
case MotionEvent.ACTION_DOWN: {
mIsUnableToDrag = false;
final float x = ev.getX();
final float y = ev.getY();
mInitialMotionX = x;
mInitialMotionY = y;
if (mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y) &&
isDimmed(mSlideableView)) {
interceptTap = true;
}
break;
}
case MotionEvent.ACTION_MOVE: {
final float x = ev.getX();
final float y = ev.getY();
final float adx = Math.abs(x - mInitialMotionX);
final float ady = Math.abs(y - mInitialMotionY);
final int slop = mDragHelper.getTouchSlop();
if (adx > slop && ady > adx) {
mDragHelper.cancel();
mIsUnableToDrag = true;
return false;
}
}
}
final boolean interceptForDrag = mDragHelper.shouldInterceptTouchEvent(ev);
return interceptForDrag || interceptTap;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!mCanSlide) {
return super.onTouchEvent(ev);
}
mDragHelper.processTouchEvent(ev);
final int action = ev.getAction();
boolean wantTouchEvents = true;
switch (action & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mInitialMotionX = x;
mInitialMotionY = y;
break;
}
case MotionEvent.ACTION_UP: {
if (isDimmed(mSlideableView)) {
final float x = ev.getX();
final float y = ev.getY();
final float dx = x - mInitialMotionX;
final float dy = y - mInitialMotionY;
final int slop = mDragHelper.getTouchSlop();
if (dx * dx + dy * dy < slop * slop &&
mDragHelper.isViewUnder(mSlideableView, (int) x, (int) y)) {
// Taps close a dimmed open pane.
closePane(mSlideableView, 0);
break;
}
}
break;
}
}
return wantTouchEvents;
}
private boolean closePane(View pane, int initialVelocity) {
if (mFirstLayout || smoothSlideTo(0.f, initialVelocity)) {
mPreservedOpenState = false;
return true;
}
return false;
}
private boolean openPane(View pane, int initialVelocity) {
if (mFirstLayout || smoothSlideTo(1.f, initialVelocity)) {
mPreservedOpenState = true;
return true;
}
return false;
}
/**
* @deprecated Renamed to {@link #openPane()} - this method is going away soon!
*/
@Deprecated
public void smoothSlideOpen() {
openPane();
}
/**
* Open the sliding pane if it is currently slideable. If first layout
* has already completed this will animate.
*
* @return true if the pane was slideable and is now open/in the process of opening
*/
public boolean openPane() {
return openPane(mSlideableView, 0);
}
/**
* @deprecated Renamed to {@link #closePane()} - this method is going away soon!
*/
@Deprecated
public void smoothSlideClosed() {
closePane();
}
/**
* Close the sliding pane if it is currently slideable. If first layout
* has already completed this will animate.
*
* @return true if the pane was slideable and is now closed/in the process of closing
*/
public boolean closePane() {
return closePane(mSlideableView, 0);
}
/**
* Check if the layout is completely open. It can be open either because the slider
* itself is open revealing the left pane, or if all content fits without sliding.
*
* @return true if sliding panels are completely open
*/
public boolean isOpen() {
return !mCanSlide || mSlideOffset == 1;
}
/**
* @return true if content in this layout can be slid open and closed
* @deprecated Renamed to {@link #isSlideable()} - this method is going away soon!
*/
@Deprecated
public boolean canSlide() {
return mCanSlide;
}
/**
* Check if the content in this layout cannot fully fit side by side and therefore
* the content pane can be slid back and forth.
*
* @return true if content in this layout can be slid open and closed
*/
public boolean isSlideable() {
return mCanSlide;
}
private void onPanelDragged(int newLeft) {
final LayoutParams lp = (LayoutParams) mSlideableView.getLayoutParams();
final int leftBound = getMeasuredWidth() - mSlideableView.getMeasuredWidth() + getPaddingLeft() + lp.leftMargin;
mSlideOffset = (float) (newLeft - leftBound) / mSlideRange;
if (mParallaxBy != 0) {
parallaxOtherViews(mSlideOffset);
}
if (lp.dimWhenOffset) {
dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor);
}
dispatchOnPanelSlide(mSlideableView);
}
private void dimChildView(View v, float mag, int fadeColor) {
final LayoutParams lp = (LayoutParams) v.getLayoutParams();
if (mag > 0 && fadeColor != 0) {
final int baseAlpha = (fadeColor & 0xff000000) >>> 24;
int imag = (int) (baseAlpha * mag);
int color = imag << 24 | (fadeColor & 0xffffff);
if (lp.dimPaint == null) {
lp.dimPaint = new Paint();
}
lp.dimPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_OVER));
if (ViewCompat.getLayerType(v) != ViewCompat.LAYER_TYPE_HARDWARE) {
ViewCompat.setLayerType(v, ViewCompat.LAYER_TYPE_HARDWARE, lp.dimPaint);
}
invalidateChildRegion(v);
} else if (ViewCompat.getLayerType(v) != ViewCompat.LAYER_TYPE_NONE) {
if (lp.dimPaint != null) {
lp.dimPaint.setColorFilter(null);
}
final DisableLayerRunnable dlr = new DisableLayerRunnable(v);
mPostedRunnables.add(dlr);
ViewCompat.postOnAnimation(this, dlr);
}
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
boolean result;
final int save = canvas.save(Canvas.CLIP_SAVE_FLAG);
if (mCanSlide && !lp.slideable && mSlideableView != null) {
// Clip against the slider; no sense drawing what will immediately be covered.
canvas.getClipBounds(mTmpRect);
mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
canvas.clipRect(mTmpRect);
}
if (Build.VERSION.SDK_INT >= 11) { // HC
result = super.drawChild(canvas, child, drawingTime);
} else {
if (lp.dimWhenOffset && mSlideOffset > 0) {
if (!child.isDrawingCacheEnabled()) {
child.setDrawingCacheEnabled(true);
}
final Bitmap cache = child.getDrawingCache();
if (cache != null) {
canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
result = false;
} else {
Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
result = super.drawChild(canvas, child, drawingTime);
}
} else {
if (child.isDrawingCacheEnabled()) {
child.setDrawingCacheEnabled(false);
}
result = super.drawChild(canvas, child, drawingTime);
}
}
canvas.restoreToCount(save);
return result;
}
private void invalidateChildRegion(View v) {
IMPL.invalidateChildRegion(this, v);
}
/**
* Smoothly animate mDraggingPane to the target X position within its range.
*
* @param slideOffset position to animate to
* @param velocity initial velocity in case of fling, or 0.
*/
boolean smoothSlideTo(float slideOffset, int velocity) {
if (!mCanSlide) {
// Nothing to do.
return false;
}
final LayoutParams lp = (LayoutParams) mSlideableView.getLayoutParams();
final int leftBound = getMeasuredWidth() - mSlideableView.getMeasuredWidth() + getPaddingLeft() + lp.leftMargin;
int x = (int) (leftBound + slideOffset * mSlideRange);
if (mDragHelper.smoothSlideViewTo(mSlideableView, x, mSlideableView.getTop())) {
setAllChildrenVisible();
ViewCompat.postInvalidateOnAnimation(this);
return true;
}
return false;
}
@Override
public void computeScroll() {
if (mDragHelper.continueSettling(true)) {
if (!mCanSlide) {
mDragHelper.abort();
return;
}
ViewCompat.postInvalidateOnAnimation(this);
}
}
/**
* Set a drawable to use as a shadow cast by the right pane onto the left pane
* during opening/closing.
*
* @param d drawable to use as a shadow
*/
public void setShadowDrawable(Drawable d) {
mShadowDrawable = d;
}
/**
* Set a drawable to use as a shadow cast by the right pane onto the left pane
* during opening/closing.
*
* @param resId Resource ID of a drawable to use
*/
public void setShadowResource(int resId) {
setShadowDrawable(getResources().getDrawable(resId));
}
@Override
public void draw(Canvas c) {
super.draw(c);
final View shadowView = getChildCount() > 1 ? getChildAt(1) : null;
if (shadowView == null || mShadowDrawable == null) {
// No need to draw a shadow if we don't have one.
return;
}
final int shadowWidth = mShadowDrawable.getIntrinsicWidth();
final int right = shadowView.getLeft();
final int top = shadowView.getTop();
final int bottom = shadowView.getBottom();
final int left = right - shadowWidth;
mShadowDrawable.setBounds(left, top, right, bottom);
mShadowDrawable.draw(c);
}
private void parallaxOtherViews(float slideOffset) {
final LayoutParams slideLp = (LayoutParams) mSlideableView.getLayoutParams();
final boolean dimViews = slideLp.dimWhenOffset && slideLp.leftMargin <= 0;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {