滑动冲突解决方案:BottomSheetBehavior 与 ViewPager 和多 RecyclerView 结合使用

滑动冲突解决方案:BottomSheetBehavior 与 ViewPager 和多 RecyclerView 结合使用

七月 30, 2024

1. 问题背景

BottomSheetBehavior 是 Jetpack Compose 库中的一个行为类,它为 CoordinatorLayout 中的子视图提供了展开和折叠的交互功能。这种交互在 Android 应用中非常流行,尤其是在需要展示额外信息或选项时。BottomSheetBehavior 允许开发者自定义底部 Sheet 的行为,例如设置不同的展开状态、滑动灵敏度以及与用户交互时的动画效果。

2. 冲突原因分析

  • BottomSheetBehavior 默认只处理第一个可滑动子视图。
  • 滑动事件被 RecyclerView 完全消费,导致 BottomSheetBehavior 的滑动弹出和关闭功能失效。

3. 自定义 MyViewPagerBottomSheetBehavior

重写 findScrollingChild 方法以支持 ViewPager

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
    @Nullable  
@VisibleForTesting View findScrollingChild(View view) {
if (view == null) {
return null;
}
globalView = view;
boolean b = isScrollViewOnTopMap.getOrDefault(currentPagePosition, false);
if (b) {
return null;
}

if (ViewCompat.isNestedScrollingEnabled(view)) {
return view;
}
if (view instanceof ViewPager) {
ViewPager viewPager = (ViewPager) view;
if (isFirstFind) {
for (int i = 0; i < Objects.requireNonNull(viewPager.getAdapter()).getCount(); i++) {
isScrollViewOnTopMap.put(i, true);
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//注意这里, 再切换时重新更新nestedScrollingChildRef
//防止page切换 状态不同步滑动值可能不对的问题
if (currentPagePosition != position) {
currentPagePosition = position;
notifyScrollView();
}
}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}
});
isFirstFind = false;
}
// 修改此处直接使用原生方法来代替原文的通过反射获取的方法
View currentViewPagerChild = viewPager.getChildAt(viewPager.getCurrentItem());
// View currentViewPagerChild = ViewPagerUtils.getCurrentView(viewPager);
View scrollingChild = findScrollingChild(currentViewPagerChild);
if (scrollingChild != null) {
return scrollingChild;
}
return currentViewPagerChild;
}

if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}

4. 处理 ViewPager 联动

  • 添加 isScrollViewOnTopMap 来跟踪每个页面的滑动状态。
  • ViewPager 添加页面变化监听,动态更新 isScrollViewOnTopMap
1
2
3
4
// 针对viewpager联动
boolean isFirstFind = true;
private HashMap<Integer, Boolean> isScrollViewOnTopMap = new HashMap<>();
private int currentPagePosition = 0;

5. 刷新滑动控件方法

创建 notifyScrollView 方法以刷新找到的滑动控件。

1
2
3
private void notifyScrollView() {
nestedScrollingChildRef = new WeakReference<>(findScrollingChild(globalView));
}

6. 暴露设置滑动顶部状态的方法

提供 setCurrentScrollViewOnTop 公开方法,允许外部修改滑动顶部状态。

1
2
3
4
5
6
7
8
public void setCurrentScrollViewOnTop(boolean scrollViewOnTop) {
isScrollViewOnTopMap.put(currentPagePosition, scrollViewOnTop);
notifyScrollView();
}
//修改nestedScrollingChildRef用于让滑动事件可以在behavior与rv/lv中进行切换
private void notifyScrollView() {
nestedScrollingChildRef = new WeakReference<>(findScrollingChild(globalView));
}

7. 监听 RecyclerView 滑动

RecyclerViewOnScrollListener 中,根据滑动状态更新 Behavior 的滑动顶部状态。
RV实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (behavior != null) {
Log.d("!!!!!!!!!", String.valueOf(!recyclerView.canScrollVertically(-1)) + dy);
if (dy == 0) {
//指尖未真实上下滑动(针对从viewpager其他页面切换过来时),不做任何操作
return;
}
if (!recyclerView.canScrollVertically(-1) && dy < 0) {
//不可以下拉,并且手势是下拉,通知behavior已经列表已经在顶部了
Log.d("~~~~~", "划不动了");
behavior.setCurrentScrollViewOnTop(true);
} else {
//可以下拉或者手势不是下拉,通知behavior已经列表不在顶部
behavior.setCurrentScrollViewOnTop(false);
}
}
}
});

LV实现:

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
private var previousFirstVisibleItem = -1

override fun onScroll(
view: AbsListView?,
firstVisibleItem: Int,
visibleItemCount: Int,
totalItemCount: Int
) {
super.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount)
// 判断滚动方向
if (view != null) {
if (previousFirstVisibleItem == -1) {
previousFirstVisibleItem = firstVisibleItem
} else if (firstVisibleItem < previousFirstVisibleItem) {
// 向下滚动
if (firstVisibleItem == 0) {
// 列表已经滚动到顶部
behavior.setCurrentScrollViewOnTop(true)
} else {
behavior.setCurrentScrollViewOnTop(false)
}
} else if (firstVisibleItem > previousFirstVisibleItem) {
// 向上滚动
if (firstVisibleItem + visibleItemCount == totalItemCount) {
// 列表已经滚动到底部
behavior.setCurrentScrollViewOnTop(false)
} else {
behavior.setCurrentScrollViewOnTop(false)
}
}
previousFirstVisibleItem = firstVisibleItem
}
}

8. 实现细节

  • 使用 ViewPager 的原生方法获取,原文参考使用了反射没有必要同时还增加了风险。
  • 除了自定义 behavior 外,还需要自定义 CustomBottomSheetDialog 这样才能让 Dialog 的 set 的 behavior 是自定义类的 behavior,否则 behavior 不会生效。
  • 原文中监听Viewpager滚动方式可能存在遗漏, 切换page时可能造成nestedScrollingChildRef没第一时间更新,只能下次生效 所以需要在onPageScroll进行nestedScrollingChildRef的更新

9. 参考资料

10. 完整代码

design_minibar_bottom_sheet_dialog.xml

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
<?xml version="1.0" encoding="utf-8"?>  
<!--
~ Copyright (C) 2015 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.-->
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<View android:id="@+id/touch_outside"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:importantForAccessibility="no"
android:soundEffectsEnabled="false"
tools:ignore="UnusedAttribute"/>

<FrameLayout android:id="@+id/design_bottom_sheet"
style="?attr/bottomSheetStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
app:layout_behavior="com.base.custom.MinibarViewPagerBottomSheetBehavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</FrameLayout>

MinibarViewPagerBottomSheetBehavior.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663

package com.base.custom;


import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowInsets;

import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams;
import androidx.core.math.MathUtils;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat;
import androidx.core.view.accessibility.AccessibilityViewCommand;
import androidx.customview.view.AbsSavedState;
import androidx.customview.widget.ViewDragHelper;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.R;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


public class MinibarViewPagerBottomSheetBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {

/**
* Callback for monitoring events about bottom sheets. */ public abstract static class BottomSheetCallback {

/**
* Called when the bottom sheet changes its state. * * @param bottomSheet The bottom sheet view.
* @param newState The new state. This will be one of {@link #STATE_DRAGGING}, {@link
* #STATE_SETTLING}, {@link #STATE_EXPANDED}, {@link #STATE_COLLAPSED}, {@link
* #STATE_HIDDEN}, or {@link #STATE_HALF_EXPANDED}.
*/ public abstract void onStateChanged(@NonNull View bottomSheet, @State int newState);

/**
* Called when the bottom sheet is being dragged. * * @param bottomSheet The bottom sheet view.
* @param slideOffset The new offset of this bottom sheet within [-1,1] range. Offset increases
* as this bottom sheet is moving upward. From 0 to 1 the sheet is between collapsed and * expanded states and from -1 to 0 it is between hidden and collapsed states. */ public abstract void onSlide(@NonNull View bottomSheet, float slideOffset);
}

/**
* The bottom sheet is dragging. */ public static final int STATE_DRAGGING = 1;

/**
* The bottom sheet is settling. */ public static final int STATE_SETTLING = 2;

/**
* The bottom sheet is expanded. */ public static final int STATE_EXPANDED = 3;

/**
* The bottom sheet is collapsed. */ public static final int STATE_COLLAPSED = 4;

/**
* The bottom sheet is hidden. */ public static final int STATE_HIDDEN = 5;

/**
* The bottom sheet is half-expanded (used when mFitToContents is false). */ public static final int STATE_HALF_EXPANDED = 6;

/**
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@IntDef({
STATE_EXPANDED,
STATE_COLLAPSED,
STATE_DRAGGING,
STATE_SETTLING,
STATE_HIDDEN,
STATE_HALF_EXPANDED
})
@Retention(RetentionPolicy.SOURCE)
public @interface State {
}

/**
* Peek at the 16:9 ratio keyline of its parent. * * <p>This can be used as a parameter for {@link #setPeekHeight(int)}. {@link #getPeekHeight()}
* will return this when the value is set. */ public static final int PEEK_HEIGHT_AUTO = -1;

/**
* This flag will preserve the peekHeight int value on configuration change. */ public static final int SAVE_PEEK_HEIGHT = 0x1;

/**
* This flag will preserve the fitToContents boolean value on configuration change. */ public static final int SAVE_FIT_TO_CONTENTS = 1 << 1;

/**
* This flag will preserve the hideable boolean value on configuration change. */ public static final int SAVE_HIDEABLE = 1 << 2;

/**
* This flag will preserve the skipCollapsed boolean value on configuration change. */ public static final int SAVE_SKIP_COLLAPSED = 1 << 3;

/**
* This flag will preserve all aforementioned values on configuration change. */ public static final int SAVE_ALL = -1;

/**
* This flag will not preserve the aforementioned values set at runtime if the view is destroyed * and recreated. The only value preserved will be the positional state, e.g. collapsed, hidden, * expanded, etc. This is the default behavior. */ public static final int SAVE_NONE = 0;

/**
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@IntDef(
flag = true,
value = {
SAVE_PEEK_HEIGHT,
SAVE_FIT_TO_CONTENTS,
SAVE_HIDEABLE,
SAVE_SKIP_COLLAPSED,
SAVE_ALL,
SAVE_NONE,
})
@Retention(RetentionPolicy.SOURCE)
public @interface SaveFlags {
}

private static final String TAG = "BottomSheetBehavior";

@SaveFlags
private int saveFlags = SAVE_NONE;

private static final int SIGNIFICANT_VEL_THRESHOLD = 500;

private static final float HIDE_THRESHOLD = 0.5f;

private static final float HIDE_FRICTION = 0.1f;

private static final int CORNER_ANIMATION_DURATION = 500;

private boolean fitToContents = true;

private boolean updateImportantForAccessibilityOnSiblings = false;

private float maximumVelocity;

/**
* Peek height set by the user. */ private int peekHeight;

/**
* Whether or not to use automatic peek height. */ private boolean peekHeightAuto;

/**
* Minimum peek height permitted. */ private int peekHeightMin;

/**
* True if Behavior has a non-null value for the @shapeAppearance attribute */ private boolean shapeThemingEnabled;

private MaterialShapeDrawable materialShapeDrawable;

private boolean gestureInsetBottomIgnored;

/**
* Default Shape Appearance to be used in bottomsheet */ private ShapeAppearanceModel shapeAppearanceModelDefault;

private boolean isShapeExpanded;

private SettleRunnable settleRunnable = null;

@Nullable
private ValueAnimator interpolatorAnimator;

private static final int DEF_STYLE_RES = R.style.Widget_Design_BottomSheet_Modal;

int expandedOffset;

int fitToContentsOffset;

int halfExpandedOffset;

float halfExpandedRatio = 0.5f;

int collapsedOffset;

float elevation = -1;

boolean hideable;

private boolean skipCollapsed;

private boolean draggable = true;

@State
int state = STATE_COLLAPSED;

@Nullable
ViewDragHelper viewDragHelper;

private boolean ignoreEvents;

private int lastNestedScrollDy;

private boolean nestedScrolled;

int parentWidth;
int parentHeight;

@Nullable
WeakReference<V> viewRef;

@Nullable
WeakReference<View> nestedScrollingChildRef;

@NonNull
private final ArrayList<BottomSheetCallback> callbacks = new ArrayList<>();

@Nullable
private VelocityTracker velocityTracker;

int activePointerId;

private int initialY;

boolean touchingScrollingChild;

@Nullable
private Map<View, Integer> importantForAccessibilityMap;

public MinibarViewPagerBottomSheetBehavior() {
}

public MinibarViewPagerBottomSheetBehavior(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomSheetBehavior_Layout);
this.shapeThemingEnabled = a.hasValue(R.styleable.BottomSheetBehavior_Layout_shapeAppearance);
boolean hasBackgroundTint = a.hasValue(R.styleable.BottomSheetBehavior_Layout_backgroundTint);
if (hasBackgroundTint) {
@SuppressLint("RestrictedApi") ColorStateList bottomSheetColor =
MaterialResources.getColorStateList(
context, a, R.styleable.BottomSheetBehavior_Layout_backgroundTint);
createMaterialShapeDrawable(context, attrs, hasBackgroundTint, bottomSheetColor);
} else {
createMaterialShapeDrawable(context, attrs, hasBackgroundTint);
}
createShapeValueAnimator();

if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
this.elevation = a.getDimension(R.styleable.BottomSheetBehavior_Layout_android_elevation, -1);
}

TypedValue value = a.peekValue(R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight);
if (value != null && value.data == PEEK_HEIGHT_AUTO) {
setPeekHeight(value.data);
} else {
setPeekHeight(
a.getDimensionPixelSize(
R.styleable.BottomSheetBehavior_Layout_behavior_peekHeight, PEEK_HEIGHT_AUTO));
}
setHideable(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_hideable, false));
setGestureInsetBottomIgnored(
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_gestureInsetBottomIgnored, false));
setFitToContents(
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_fitToContents, true));
setSkipCollapsed(
a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_skipCollapsed, false));
setDraggable(a.getBoolean(R.styleable.BottomSheetBehavior_Layout_behavior_draggable, true));
setSaveFlags(a.getInt(R.styleable.BottomSheetBehavior_Layout_behavior_saveFlags, SAVE_NONE));
setHalfExpandedRatio(
a.getFloat(R.styleable.BottomSheetBehavior_Layout_behavior_halfExpandedRatio, 0.5f));

value = a.peekValue(R.styleable.BottomSheetBehavior_Layout_behavior_expandedOffset);
if (value != null && value.type == TypedValue.TYPE_FIRST_INT) {
setExpandedOffset(value.data);
} else {
setExpandedOffset(
a.getDimensionPixelOffset(
R.styleable.BottomSheetBehavior_Layout_behavior_expandedOffset, 0));
}
a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
maximumVelocity = configuration.getScaledMaximumFlingVelocity();
}

@NonNull
@Override public Parcelable onSaveInstanceState(@NonNull CoordinatorLayout parent, @NonNull V child) {
return new SavedState(super.onSaveInstanceState(parent, child), this);
}

@Override
public void onRestoreInstanceState(
@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(parent, child, ss.getSuperState());
// Restore Optional State values designated by saveFlags
restoreOptionalState(ss);
// Intermediate states are restored as collapsed state
if (ss.state == STATE_DRAGGING || ss.state == STATE_SETTLING) {
this.state = STATE_COLLAPSED;
} else {
this.state = ss.state;
}
}

@Override
public void onAttachedToLayoutParams(@NonNull LayoutParams layoutParams) {
super.onAttachedToLayoutParams(layoutParams);
// These may already be null, but just be safe, explicitly assign them. This lets us know the
// first time we layout with this behavior by checking (viewRef == null). viewRef = null;
viewDragHelper = null;
}

@Override
public void onDetachedFromLayoutParams() {
super.onDetachedFromLayoutParams();
// Release references so we don't run unnecessary codepaths while not attached to a view.
viewRef = null;
viewDragHelper = null;
}

@Override
public boolean onLayoutChild(
@NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {
if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
child.setFitsSystemWindows(true);
}

if (viewRef == null) {
// First layout with this behavior.
peekHeightMin =
parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min);
setSystemGestureInsets(parent);
viewRef = new WeakReference<>(child);
// Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
// default to android:background declared in styles or layout. if (shapeThemingEnabled && materialShapeDrawable != null) {
ViewCompat.setBackground(child, materialShapeDrawable);
}
// Set elevation on MaterialShapeDrawable
if (materialShapeDrawable != null) {
// Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
materialShapeDrawable.setElevation(
elevation == -1 ? ViewCompat.getElevation(child) : elevation);
// Update the material shape based on initial state.
isShapeExpanded = state == STATE_EXPANDED;
materialShapeDrawable.setInterpolation(isShapeExpanded ? 0f : 1f);
}
updateAccessibilityActions();
if (ViewCompat.getImportantForAccessibility(child)
== ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
ViewCompat.setImportantForAccessibility(child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
if (viewDragHelper == null) {
viewDragHelper = ViewDragHelper.create(parent, dragCallback);
}

int savedTop = child.getTop();
// First let the parent lay it out
parent.onLayoutChild(child, layoutDirection);
// Offset the bottom sheet
parentWidth = parent.getWidth();
parentHeight = parent.getHeight();
fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
calculateHalfExpandedOffset();
calculateCollapsedOffset();

if (state == STATE_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, getExpandedOffset());
} else if (state == STATE_HALF_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, halfExpandedOffset);
} else if (hideable && state == STATE_HIDDEN) {
ViewCompat.offsetTopAndBottom(child, parentHeight);
} else if (state == STATE_COLLAPSED) {
ViewCompat.offsetTopAndBottom(child, collapsedOffset);
} else if (state == STATE_DRAGGING || state == STATE_SETTLING) {
ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
}

nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
return true;
}

@Override
public boolean onInterceptTouchEvent(
@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull MotionEvent event) {
if (!child.isShown() || !draggable) {
ignoreEvents = true;
return false;
}
int action = event.getActionMasked();
// Record the velocity
if (action == MotionEvent.ACTION_DOWN) {
reset();
}
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
}
velocityTracker.addMovement(event);
switch (action) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
touchingScrollingChild = false;
activePointerId = MotionEvent.INVALID_POINTER_ID;
// Reset the ignore flag
if (ignoreEvents) {
ignoreEvents = false;
return false;
}
break;
case MotionEvent.ACTION_DOWN:
int initialX = (int) event.getX();
initialY = (int) event.getY();
// Only intercept nested scrolling events here if the view not being moved by the
// ViewDragHelper. if (state != STATE_SETTLING) {
View scroll = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
if (scroll != null && parent.isPointInChildBounds(scroll, initialX, initialY)) {
activePointerId = event.getPointerId(event.getActionIndex());
touchingScrollingChild = true;
}
}
ignoreEvents =
activePointerId == MotionEvent.INVALID_POINTER_ID
&& !parent.isPointInChildBounds(child, initialX, initialY);
break;
default: // fall out
}
if (!ignoreEvents
&& viewDragHelper != null
&& viewDragHelper.shouldInterceptTouchEvent(event)) {
return true;
}
// We have to handle cases that the ViewDragHelper does not capture the bottom sheet because
// it is not the top most view of its parent. This is not necessary when the touch event is // happening over the scrolling content as nested scrolling logic handles that case. View scroll = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
return action == MotionEvent.ACTION_MOVE
&& scroll != null
&& !ignoreEvents
&& state != STATE_DRAGGING
&& !parent.isPointInChildBounds(scroll, (int) event.getX(), (int) event.getY())
&& viewDragHelper != null
&& Math.abs(initialY - event.getY()) > viewDragHelper.getTouchSlop();
}

@Override
public boolean onTouchEvent(
@NonNull CoordinatorLayout parent, @NonNull V child, @NonNull MotionEvent event) {
if (!child.isShown()) {
return false;
}
int action = event.getActionMasked();
if (state == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN) {
return true;
}
if (viewDragHelper != null) {
viewDragHelper.processTouchEvent(event);
}
// Record the velocity
if (action == MotionEvent.ACTION_DOWN) {
reset();
}
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
}
velocityTracker.addMovement(event);
// The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
// to capture the bottom sheet in case it is not captured and the touch slop is passed. if (action == MotionEvent.ACTION_MOVE && !ignoreEvents) {
if (Math.abs(initialY - event.getY()) > viewDragHelper.getTouchSlop()) {
viewDragHelper.captureChildView(child, event.getPointerId(event.getActionIndex()));
}
}
return !ignoreEvents;
}

@Override
public boolean onStartNestedScroll(
@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child,
@NonNull View directTargetChild,
@NonNull View target,
int axes,
int type) {
lastNestedScrollDy = 0;
nestedScrolled = false;
return (axes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}

@Override
public void onNestedPreScroll(
@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child,
@NonNull View target,
int dx,
int dy,
@NonNull int[] consumed,
int type) {
if (type == ViewCompat.TYPE_NON_TOUCH) {
// Ignore fling here. The ViewDragHelper handles it.
return;
}
View scrollingChild = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
if (target != scrollingChild) {
return;
}
int currentTop = child.getTop();
int newTop = currentTop - dy;
if (dy > 0) { // Upward
if (newTop < getExpandedOffset()) {
consumed[1] = currentTop - getExpandedOffset();
ViewCompat.offsetTopAndBottom(child, -consumed[1]);
setStateInternal(STATE_EXPANDED);
} else {
if (!draggable) {
// Prevent dragging
return;
}

consumed[1] = dy;
ViewCompat.offsetTopAndBottom(child, -dy);
setStateInternal(STATE_DRAGGING);
}
} else if (dy < 0) { // Downward
if (!target.canScrollVertically(-1)) {
if (newTop <= collapsedOffset || hideable) {
if (!draggable) {
// Prevent dragging
return;
}

consumed[1] = dy;
ViewCompat.offsetTopAndBottom(child, -dy);
setStateInternal(STATE_DRAGGING);
} else {
consumed[1] = currentTop - collapsedOffset;
ViewCompat.offsetTopAndBottom(child, -consumed[1]);
setStateInternal(STATE_COLLAPSED);
}
}
}
dispatchOnSlide(child.getTop());
lastNestedScrollDy = dy;
nestedScrolled = true;
}

@Override
public void onStopNestedScroll(
@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child,
@NonNull View target,
int type) {
if (child.getTop() == getExpandedOffset()) {
setStateInternal(STATE_EXPANDED);
return;
}
if (nestedScrollingChildRef == null
|| target != nestedScrollingChildRef.get()
|| !nestedScrolled) {
return;
}
int top;
int targetState;
if (lastNestedScrollDy > 0) {
if (fitToContents) {
top = fitToContentsOffset;
targetState = STATE_EXPANDED;
} else {
int currentTop = child.getTop();
if (currentTop > halfExpandedOffset) {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
} else {
top = expandedOffset;
targetState = STATE_EXPANDED;
}
}
} else if (hideable && shouldHide(child, getYVelocity())) {
top = parentHeight;
targetState = STATE_HIDDEN;
} else if (lastNestedScrollDy == 0) {
int currentTop = child.getTop();
if (fitToContents) {
if (Math.abs(currentTop - fitToContentsOffset) < Math.abs(currentTop - collapsedOffset)) {
top = fitToContentsOffset;
targetState = STATE_EXPANDED;
} else {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
}
} else {
if (currentTop < halfExpandedOffset) {
if (currentTop < Math.abs(currentTop - collapsedOffset)) {
top = expandedOffset;
targetState = STATE_EXPANDED;
} else {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
}
} else {
if (Math.abs(currentTop - halfExpandedOffset) < Math.abs(currentTop - collapsedOffset)) {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
} else {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
}
}
}
} else {
if (fitToContents) {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
} else {
// Settle to nearest height.
int currentTop = child.getTop();
if (Math.abs(currentTop - halfExpandedOffset) < Math.abs(currentTop - collapsedOffset)) {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
} else {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
}
}
}
startSettlingAnimation(child, targetState, top, false);
nestedScrolled = false;
}

@Override
public void onNestedScroll(
@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child,
@NonNull View target,
int dxConsumed,
int dyConsumed,
int dxUnconsumed,
int dyUnconsumed,
int type,
@NonNull int[] consumed) {
// Overridden to prevent the default consumption of the entire scroll distance.
}

@Override
public boolean onNestedPreFling(
@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child,
@NonNull View target,
float velocityX,
float velocityY) {
if (nestedScrollingChildRef != null) {
return target == nestedScrollingChildRef.get()
&& (state != STATE_EXPANDED
|| super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY));
} else {
return false;
}
}

/**
* @return whether the height of the expanded sheet is determined by the height of its contents,
* or if it is expanded in two stages (half the height of the parent container, full height of * parent container). */ public boolean isFitToContents() {
return fitToContents;
}

/**
* Sets whether the height of the expanded sheet is determined by the height of its contents, or * if it is expanded in two stages (half the height of the parent container, full height of parent * container). Default value is true. * * @param fitToContents whether or not to fit the expanded sheet to its contents.
*/ public void setFitToContents(boolean fitToContents) {
if (this.fitToContents == fitToContents) {
return;
}
this.fitToContents = fitToContents;

// If sheet is already laid out, recalculate the collapsed offset based on new setting.
// Otherwise, let onLayoutChild handle this later. if (viewRef != null) {
calculateCollapsedOffset();
}
// Fix incorrect expanded settings depending on whether or not we are fitting sheet to contents.
setStateInternal((this.fitToContents && state == STATE_HALF_EXPANDED) ? STATE_EXPANDED : state);

updateAccessibilityActions();
}

/**
* Sets the height of the bottom sheet when it is collapsed. * * @param peekHeight The height of the collapsed bottom sheet in pixels, or {@link
* #PEEK_HEIGHT_AUTO} to configure the sheet to peek automatically at 16:9 ratio keyline.
* @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_peekHeight */ public void setPeekHeight(int peekHeight) {
setPeekHeight(peekHeight, false);
}

/**
* Sets the height of the bottom sheet when it is collapsed while optionally animating between the * old height and the new height. * * @param peekHeight The height of the collapsed bottom sheet in pixels, or {@link
* #PEEK_HEIGHT_AUTO} to configure the sheet to peek automatically at 16:9 ratio keyline.
* @param animate Whether to animate between the old height and the new height.
* @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_peekHeight */ public final void setPeekHeight(int peekHeight, boolean animate) {
boolean layout = false;
if (peekHeight == PEEK_HEIGHT_AUTO) {
if (!peekHeightAuto) {
peekHeightAuto = true;
layout = true;
}
} else if (peekHeightAuto || this.peekHeight != peekHeight) {
peekHeightAuto = false;
this.peekHeight = Math.max(0, peekHeight);
layout = true;
}
// If sheet is already laid out, recalculate the collapsed offset based on new setting.
// Otherwise, let onLayoutChild handle this later. if (layout && viewRef != null) {
calculateCollapsedOffset();
if (state == STATE_COLLAPSED) {
V view = viewRef.get();
if (view != null) {
if (animate) {
settleToStatePendingLayout(state);
} else {
view.requestLayout();
}
}
}
}
}

/**
* Gets the height of the bottom sheet when it is collapsed. * * @return The height of the collapsed bottom sheet in pixels, or {@link #PEEK_HEIGHT_AUTO} if the
* sheet is configured to peek automatically at 16:9 ratio keyline * @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_peekHeight */ public int getPeekHeight() {
return peekHeightAuto ? PEEK_HEIGHT_AUTO : peekHeight;
}

/**
* Determines the height of the BottomSheet in the {@link #STATE_HALF_EXPANDED} state. The
* material guidelines recommended a value of 0.5, which results in the sheet filling half of the * parent. The height of the BottomSheet will be smaller as this ratio is decreased and taller as * it is increased. The default value is 0.5. * * @param ratio a float between 0 and 1, representing the {@link #STATE_HALF_EXPANDED} ratio.
* @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_halfExpandedRatio */ public void setHalfExpandedRatio(@FloatRange(from = 0.0f, to = 1.0f) float ratio) {

if ((ratio <= 0) || (ratio >= 1)) {
throw new IllegalArgumentException("ratio must be a float value between 0 and 1");
}
this.halfExpandedRatio = ratio;
// If sheet is already laid out, recalculate the half expanded offset based on new setting.
// Otherwise, let onLayoutChild handle this later. if (viewRef != null) {
calculateHalfExpandedOffset();
}
}

/**
* Gets the ratio for the height of the BottomSheet in the {@link #STATE_HALF_EXPANDED} state.
* * @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_halfExpandedRatio */ @FloatRange(from = 0.0f, to = 1.0f)
public float getHalfExpandedRatio() {
return halfExpandedRatio;
}

/**
* Determines the top offset of the BottomSheet in the {@link #STATE_EXPANDED} state when
* fitsToContent is false. The default value is 0, which results in the sheet matching the * parent's top. * * @param offset an integer value greater than equal to 0, representing the {@link
* #STATE_EXPANDED} offset. Value must not exceed the offset in the half expanded state.
* @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_expandedOffset */ public void setExpandedOffset(int offset) {
if (offset < 0) {
throw new IllegalArgumentException("offset must be greater than or equal to 0");
}
this.expandedOffset = offset;
}

/**
* Returns the current expanded offset. If {@code fitToContents} is true, it will automatically
* pick the offset depending on the height of the content. * * @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_expandedOffset */ public int getExpandedOffset() {
return fitToContents ? fitToContentsOffset : expandedOffset;
}

/**
* Sets whether this bottom sheet can hide when it is swiped down. * * @param hideable {@code true} to make this bottom sheet hideable.
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_hideable
*/ public void setHideable(boolean hideable) {
if (this.hideable != hideable) {
this.hideable = hideable;
if (!hideable && state == STATE_HIDDEN) {
// Lift up to collapsed state
setState(STATE_COLLAPSED);
}
updateAccessibilityActions();
}
}

/**
* Gets whether this bottom sheet can hide when it is swiped down. * * @return {@code true} if this bottom sheet can hide.
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_hideable
*/ public boolean isHideable() {
return hideable;
}

/**
* Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it * is expanded once. Setting this to true has no effect unless the sheet is hideable. * * @param skipCollapsed True if the bottom sheet should skip the collapsed state.
* @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_skipCollapsed */ public void setSkipCollapsed(boolean skipCollapsed) {
this.skipCollapsed = skipCollapsed;
}

/**
* Sets whether this bottom sheet should skip the collapsed state when it is being hidden after it * is expanded once. * * @return Whether the bottom sheet should skip the collapsed state.
* @attr ref
* com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_skipCollapsed */ public boolean getSkipCollapsed() {
return skipCollapsed;
}

/**
* Sets whether this bottom sheet is can be collapsed/expanded by dragging. Note: When disabling * dragging, an app will require to implement a custom way to expand/collapse the bottom sheet * * @param draggable {@code false} to prevent dragging the sheet to collapse and expand
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_draggable
*/ public void setDraggable(boolean draggable) {
this.draggable = draggable;
}

public boolean isDraggable() {
return draggable;
}

/**
* Sets save flags to be preserved in bottomsheet on configuration change. * * @param flags bitwise int of {@link #SAVE_PEEK_HEIGHT}, {@link #SAVE_FIT_TO_CONTENTS}, {@link
* #SAVE_HIDEABLE}, {@link #SAVE_SKIP_COLLAPSED}, {@link #SAVE_ALL} and {@link #SAVE_NONE}.
* @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_saveFlags
* @see #getSaveFlags()
*/ public void setSaveFlags(@SaveFlags int flags) {
this.saveFlags = flags;
}

/**
* Returns the save flags. * * @attr ref com.google.android.material.R.styleable#BottomSheetBehavior_Layout_behavior_saveFlags
* @see #setSaveFlags(int)
*/ @SaveFlags
public int getSaveFlags() {
return this.saveFlags;
}

/**
* Sets a callback to be notified of bottom sheet events. * * @param callback The callback to notify when bottom sheet events occur.
* @deprecated use {@link #addBottomSheetCallback(BottomSheetCallback)} and {@link
* #removeBottomSheetCallback(BottomSheetCallback)} instead
*/ @Deprecated
public void setBottomSheetCallback(BottomSheetCallback callback) {
Log.w(
TAG,
"BottomSheetBehavior now supports multiple callbacks. `setBottomSheetCallback()` removes"
+ " all existing callbacks, including ones set internally by library authors, which"
+ " may result in unintended behavior. This may change in the future. Please use"
+ " `addBottomSheetCallback()` and `removeBottomSheetCallback()` instead to set your"
+ " own callbacks.");
callbacks.clear();
if (callback != null) {
callbacks.add(callback);
}
}

/**
* Adds a callback to be notified of bottom sheet events. * * @param callback The callback to notify when bottom sheet events occur.
*/ public void addBottomSheetCallback(@NonNull BottomSheetCallback callback) {
if (!callbacks.contains(callback)) {
callbacks.add(callback);
}
}

/**
* Removes a previously added callback. * * @param callback The callback to remove.
*/ public void removeBottomSheetCallback(@NonNull BottomSheetCallback callback) {
callbacks.remove(callback);
}

/**
* Sets the state of the bottom sheet. The bottom sheet will transition to that state with * animation. * * @param state One of {@link #STATE_COLLAPSED}, {@link #STATE_EXPANDED}, {@link #STATE_HIDDEN},
* or {@link #STATE_HALF_EXPANDED}.
*/ public void setState(@State int state) {
if (state == this.state) {
return;
}
if (viewRef == null) {
// The view is not laid out yet; modify mState and let onLayoutChild handle it later
if (state == STATE_COLLAPSED
|| state == STATE_EXPANDED
|| state == STATE_HALF_EXPANDED
|| (hideable && state == STATE_HIDDEN)) {
this.state = state;
}
return;
}
settleToStatePendingLayout(state);
}

/**
* Sets whether this bottom sheet should adjust it's position based on the system gesture area on * Android Q and above. * * <p>Note: the bottom sheet will only adjust it's position if it would be unable to be scrolled
* upwards because the peekHeight is less than the gesture inset margins,(because that would cause * a gesture conflict), gesture navigation is enabled, and this {@code ignoreGestureInsetBottom}
* flag is false. */ public void setGestureInsetBottomIgnored(boolean gestureInsetBottomIgnored) {
this.gestureInsetBottomIgnored = gestureInsetBottomIgnored;
}

/**
* Returns whether this bottom sheet should adjust it's position based on the system gesture area. */ public boolean isGestureInsetBottomIgnored() {
return gestureInsetBottomIgnored;
}

private void settleToStatePendingLayout(@State int state) {
final V child = viewRef.get();
if (child == null) {
return;
}
// Start the animation; wait until a pending layout if there is one.
ViewParent parent = child.getParent();
if (parent != null && parent.isLayoutRequested() && ViewCompat.isAttachedToWindow(child)) {
final int finalState = state;
child.post(
new Runnable() {
@Override
public void run() {
settleToState(child, finalState);
}
});
} else {
settleToState(child, state);
}
}

/**
* Gets the current state of the bottom sheet. * * @return One of {@link #STATE_EXPANDED}, {@link #STATE_HALF_EXPANDED}, {@link #STATE_COLLAPSED},
* {@link #STATE_DRAGGING}, {@link #STATE_SETTLING}, or {@link #STATE_HALF_EXPANDED}.
*/ @State
public int getState() {
return state;
}

void setStateInternal(@State int state) {
if (this.state == state) {
return;
}
this.state = state;

if (viewRef == null) {
return;
}

View bottomSheet = viewRef.get();
if (bottomSheet == null) {
return;
}

if (state == STATE_EXPANDED) {
updateImportantForAccessibility(true);
} else if (state == STATE_HALF_EXPANDED || state == STATE_HIDDEN || state == STATE_COLLAPSED) {
updateImportantForAccessibility(false);
}

updateDrawableForTargetState(state);
for (int i = 0; i < callbacks.size(); i++) {
callbacks.get(i).onStateChanged(bottomSheet, state);
}
updateAccessibilityActions();
}

private void updateDrawableForTargetState(@State int state) {
if (state == STATE_SETTLING) {
// Special case: we want to know which state we're settling to, so wait for another call.
return;
}

boolean expand = state == STATE_EXPANDED;
if (isShapeExpanded != expand) {
isShapeExpanded = expand;
if (materialShapeDrawable != null && interpolatorAnimator != null) {
if (interpolatorAnimator.isRunning()) {
interpolatorAnimator.reverse();
} else {
float to = expand ? 0f : 1f;
float from = 1f - to;
interpolatorAnimator.setFloatValues(from, to);
interpolatorAnimator.start();
}
}
}
}

private int calculatePeekHeight() {
if (peekHeightAuto) {
return Math.max(peekHeightMin, parentHeight - parentWidth * 9 / 16);
}
return peekHeight;
}

private void calculateCollapsedOffset() {
int peek = calculatePeekHeight();

if (fitToContents) {
collapsedOffset = Math.max(parentHeight - peek, fitToContentsOffset);
} else {
collapsedOffset = parentHeight - peek;
}
}

private void calculateHalfExpandedOffset() {
this.halfExpandedOffset = (int) (parentHeight * (1 - halfExpandedRatio));
}

private void reset() {
activePointerId = ViewDragHelper.INVALID_POINTER;
if (velocityTracker != null) {
velocityTracker.recycle();
velocityTracker = null;
}
}

private void restoreOptionalState(@NonNull SavedState ss) {
if (this.saveFlags == SAVE_NONE) {
return;
}
if (this.saveFlags == SAVE_ALL || (this.saveFlags & SAVE_PEEK_HEIGHT) == SAVE_PEEK_HEIGHT) {
this.peekHeight = ss.peekHeight;
}
if (this.saveFlags == SAVE_ALL
|| (this.saveFlags & SAVE_FIT_TO_CONTENTS) == SAVE_FIT_TO_CONTENTS) {
this.fitToContents = ss.fitToContents;
}
if (this.saveFlags == SAVE_ALL || (this.saveFlags & SAVE_HIDEABLE) == SAVE_HIDEABLE) {
this.hideable = ss.hideable;
}
if (this.saveFlags == SAVE_ALL
|| (this.saveFlags & SAVE_SKIP_COLLAPSED) == SAVE_SKIP_COLLAPSED) {
this.skipCollapsed = ss.skipCollapsed;
}
}

boolean shouldHide(@NonNull View child, float yvel) {
if (skipCollapsed) {
return true;
}
if (child.getTop() < collapsedOffset) {
// It should not hide, but collapse.
return false;
}
int peek = calculatePeekHeight();
final float newTop = child.getTop() + yvel * HIDE_FRICTION;
return Math.abs(newTop - collapsedOffset) / (float) peek > HIDE_THRESHOLD;
}

//针对viewpager联动
boolean isFirstFind = true; //第一次寻找联动View,为viewPager添加滑动监听
private HashMap<Integer, Boolean> isScrollViewOnTopMap = new HashMap<>(); //关联页数与能否滑动flag的Map
private int currentPagePosition = 0; //当前ViewPager的页数

public void setCurrentScrollViewOnTop(boolean scrollViewOnTop) {
isScrollViewOnTopMap.put(currentPagePosition, scrollViewOnTop);
Log.d(TAG, isScrollViewOnTopMap.toString());
notifyScrollView();
}

private View globalView;

private void notifyScrollView() {
nestedScrollingChildRef = new WeakReference<>(findScrollingChild(globalView));
}

@Nullable
@VisibleForTesting View findScrollingChild(View view) {
if (view == null) {
return null;
}
globalView = view;
boolean b = isScrollViewOnTopMap.getOrDefault(currentPagePosition, false);
if (b) {
return null;
}

if (ViewCompat.isNestedScrollingEnabled(view)) {
return view;
}
if (view instanceof ViewPager) {
ViewPager viewPager = (ViewPager) view;
if (isFirstFind) {
for (int i = 0; i < Objects.requireNonNull(viewPager.getAdapter()).getCount(); i++) {
isScrollViewOnTopMap.put(i, true);
}
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//注意这里, 再切换时重新更新nestedScrollingChildRef
//防止page切换 状态不同步滑动值可能不对的问题
if (currentPagePosition != position) {
currentPagePosition = position;
notifyScrollView();
}
}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}
});
isFirstFind = false;
}
// 修改
View currentViewPagerChild = viewPager.getChildAt(viewPager.getCurrentItem());
// View currentViewPagerChild = ViewPagerUtils.getCurrentView(viewPager);
View scrollingChild = findScrollingChild(currentViewPagerChild);
if (scrollingChild != null) {
return scrollingChild;
}
return currentViewPagerChild;
}

if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0, count = group.getChildCount(); i < count; i++) {
View scrollingChild = findScrollingChild(group.getChildAt(i));
if (scrollingChild != null) {
return scrollingChild;
}
}
}
return null;
}

private void createMaterialShapeDrawable(
@NonNull Context context, AttributeSet attrs, boolean hasBackgroundTint) {
this.createMaterialShapeDrawable(context, attrs, hasBackgroundTint, null);
}

private void createMaterialShapeDrawable(
@NonNull Context context,
AttributeSet attrs,
boolean hasBackgroundTint,
@Nullable ColorStateList bottomSheetColor) {
if (this.shapeThemingEnabled) {
this.shapeAppearanceModelDefault =
ShapeAppearanceModel.builder(context, attrs, R.attr.bottomSheetStyle, DEF_STYLE_RES)
.build();

this.materialShapeDrawable = new MaterialShapeDrawable(shapeAppearanceModelDefault);
this.materialShapeDrawable.initializeElevationOverlay(context);

if (hasBackgroundTint && bottomSheetColor != null) {
materialShapeDrawable.setFillColor(bottomSheetColor);
} else {
// If the tint isn't set, use the theme default background color.
TypedValue defaultColor = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.colorBackground, defaultColor, true);
materialShapeDrawable.setTint(defaultColor.data);
}
}
}

private void createShapeValueAnimator() {
interpolatorAnimator = ValueAnimator.ofFloat(0f, 1f);
interpolatorAnimator.setDuration(CORNER_ANIMATION_DURATION);
interpolatorAnimator.addUpdateListener(
new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(@NonNull ValueAnimator animation) {
float value = (float) animation.getAnimatedValue();
if (materialShapeDrawable != null) {
materialShapeDrawable.setInterpolation(value);
}
}
});
}

private void setSystemGestureInsets(@NonNull CoordinatorLayout parent) {
if (VERSION.SDK_INT >= VERSION_CODES.Q && !isGestureInsetBottomIgnored()) {
WindowInsets windowInsets = parent.getRootWindowInsets();
if (windowInsets != null) {
int systemMandatoryInsetsBottom = windowInsets.getSystemGestureInsets().bottom;
peekHeight += systemMandatoryInsetsBottom;
}
}
}

private float getYVelocity() {
if (velocityTracker == null) {
return 0;
}
velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
return velocityTracker.getYVelocity(activePointerId);
}

void settleToState(@NonNull View child, int state) {
int top;
if (state == STATE_COLLAPSED) {
top = collapsedOffset;
} else if (state == STATE_HALF_EXPANDED) {
top = halfExpandedOffset;
if (fitToContents && top <= fitToContentsOffset) {
// Skip to the expanded state if we would scroll past the height of the contents.
state = STATE_EXPANDED;
top = fitToContentsOffset;
}
} else if (state == STATE_EXPANDED) {
top = getExpandedOffset();
} else if (hideable && state == STATE_HIDDEN) {
top = parentHeight;
} else {
throw new IllegalArgumentException("Illegal state argument: " + state);
}
startSettlingAnimation(child, state, top, false);
}

void startSettlingAnimation(View child, int state, int top, boolean settleFromViewDragHelper) {
boolean startedSettling =
settleFromViewDragHelper
? viewDragHelper.settleCapturedViewAt(child.getLeft(), top)
: viewDragHelper.smoothSlideViewTo(child, child.getLeft(), top);
if (startedSettling) {
setStateInternal(STATE_SETTLING);
// STATE_SETTLING won't animate the material shape, so do that here with the target state.
updateDrawableForTargetState(state);
if (settleRunnable == null) {
// If the singleton SettleRunnable instance has not been instantiated, create it.
settleRunnable = new SettleRunnable(child, state);
}
// If the SettleRunnable has not been posted, post it with the correct state.
if (settleRunnable.isPosted == false) {
settleRunnable.targetState = state;
ViewCompat.postOnAnimation(child, settleRunnable);
settleRunnable.isPosted = true;
} else {
// Otherwise, if it has been posted, just update the target state.
settleRunnable.targetState = state;
}
} else {
setStateInternal(state);
}
}


MaterialShapeDrawable getMaterialShapeDrawable() {
return materialShapeDrawable;
}

private final ViewDragHelper.Callback dragCallback =
new ViewDragHelper.Callback() {

@Override
public boolean tryCaptureView(@NonNull View child, int pointerId) {
if (state == STATE_DRAGGING) {
return false;
}
if (touchingScrollingChild) {
return false;
}
if (state == STATE_EXPANDED && activePointerId == pointerId) {
View scroll = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
if (scroll != null && scroll.canScrollVertically(-1)) {
// Let the content scroll up
return false;
}
}
return viewRef != null && viewRef.get() == child;
}

@Override
public void onViewPositionChanged(
@NonNull View changedView, int left, int top, int dx, int dy) {
dispatchOnSlide(top);
}

@Override
public void onViewDragStateChanged(int state) {
if (state == ViewDragHelper.STATE_DRAGGING && draggable) {
setStateInternal(STATE_DRAGGING);
}
}

private boolean releasedLow(@NonNull View child) {
// Needs to be at least half way to the bottom.
return child.getTop() > (parentHeight + getExpandedOffset()) / 2;
}

@Override
public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {
int top;
@State int targetState;
if (yvel < 0) { // Moving up
if (fitToContents) {
top = fitToContentsOffset;
targetState = STATE_EXPANDED;
} else {
int currentTop = releasedChild.getTop();
if (currentTop > halfExpandedOffset) {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
} else {
top = expandedOffset;
targetState = STATE_EXPANDED;
}
}
} else if (hideable && shouldHide(releasedChild, yvel)) {
// Hide if the view was either released low or it was a significant vertical swipe
// otherwise settle to closest expanded state. if ((Math.abs(xvel) < Math.abs(yvel) && yvel > SIGNIFICANT_VEL_THRESHOLD)
|| releasedLow(releasedChild)) {
top = parentHeight;
targetState = STATE_HIDDEN;
} else if (fitToContents) {
top = fitToContentsOffset;
targetState = STATE_EXPANDED;
} else if (Math.abs(releasedChild.getTop() - expandedOffset)
< Math.abs(releasedChild.getTop() - halfExpandedOffset)) {
top = expandedOffset;
targetState = STATE_EXPANDED;
} else {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
}
} else if (yvel == 0.f || Math.abs(xvel) > Math.abs(yvel)) {
// If the Y velocity is 0 or the swipe was mostly horizontal indicated by the X velocity
// being greater than the Y velocity, settle to the nearest correct height. int currentTop = releasedChild.getTop();
if (fitToContents) {
if (Math.abs(currentTop - fitToContentsOffset)
< Math.abs(currentTop - collapsedOffset)) {
top = fitToContentsOffset;
targetState = STATE_EXPANDED;
} else {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
}
} else {
if (currentTop < halfExpandedOffset) {
if (currentTop < Math.abs(currentTop - collapsedOffset)) {
top = expandedOffset;
targetState = STATE_EXPANDED;
} else {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
}
} else {
if (Math.abs(currentTop - halfExpandedOffset)
< Math.abs(currentTop - collapsedOffset)) {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
} else {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
}
}
}
} else { // Moving Down
if (fitToContents) {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
} else {
// Settle to the nearest correct height.
int currentTop = releasedChild.getTop();
if (Math.abs(currentTop - halfExpandedOffset)
< Math.abs(currentTop - collapsedOffset)) {
top = halfExpandedOffset;
targetState = STATE_HALF_EXPANDED;
} else {
top = collapsedOffset;
targetState = STATE_COLLAPSED;
}
}
}
startSettlingAnimation(releasedChild, targetState, top, true);
}

@Override
public int clampViewPositionVertical(@NonNull View child, int top, int dy) {
return MathUtils.clamp(
top, getExpandedOffset(), hideable ? parentHeight : collapsedOffset);
}

@Override
public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
return child.getLeft();
}

@Override
public int getViewVerticalDragRange(@NonNull View child) {
if (hideable) {
return parentHeight;
} else {
return collapsedOffset;
}
}
};

void dispatchOnSlide(int top) {
View bottomSheet = viewRef.get();
if (bottomSheet != null && !callbacks.isEmpty()) {
float slideOffset =
(top > collapsedOffset || collapsedOffset == getExpandedOffset())
? (float) (collapsedOffset - top) / (parentHeight - collapsedOffset)
: (float) (collapsedOffset - top) / (collapsedOffset - getExpandedOffset());
for (int i = 0; i < callbacks.size(); i++) {
callbacks.get(i).onSlide(bottomSheet, slideOffset);
}
}
}

@VisibleForTesting
int getPeekHeightMin() {
return peekHeightMin;
}

/**
* Disables the shaped corner {@link ShapeAppearanceModel} interpolation transition animations.
* Will have no effect unless the sheet utilizes a {@link MaterialShapeDrawable} with set shape
* theming properties. Only For use in UI testing. * * @hide
*/
@RestrictTo(LIBRARY_GROUP)
@VisibleForTesting
public void disableShapeAnimations() {
// Sets the shape value animator to null, prevents animations from occuring during testing.
interpolatorAnimator = null;
}

private class SettleRunnable implements Runnable {

private final View view;

private boolean isPosted;

@State
int targetState;

SettleRunnable(View view, @State int targetState) {
this.view = view;
this.targetState = targetState;
}

@Override
public void run() {
if (viewDragHelper != null && viewDragHelper.continueSettling(true)) {
ViewCompat.postOnAnimation(view, this);
} else {
setStateInternal(targetState);
}
this.isPosted = false;
}
}

/**
* State persisted across instances */ protected static class SavedState extends AbsSavedState {
@State
final int state;
int peekHeight;
boolean fitToContents;
boolean hideable;
boolean skipCollapsed;

public SavedState(@NonNull Parcel source) {
this(source, null);
}

public SavedState(@NonNull Parcel source, ClassLoader loader) {
super(source, loader);
//noinspection ResourceType
state = source.readInt();
peekHeight = source.readInt();
fitToContents = source.readInt() == 1;
hideable = source.readInt() == 1;
skipCollapsed = source.readInt() == 1;
}

public SavedState(Parcelable superState, @NonNull MinibarViewPagerBottomSheetBehavior<?> behavior) {
super(superState);
this.state = behavior.getState();
this.peekHeight = behavior.getPeekHeight();
this.fitToContents = behavior.isFitToContents();
this.hideable = behavior.isHideable();
this.skipCollapsed = behavior.getSkipCollapsed();
}


@Deprecated
public SavedState(Parcelable superstate, int state) {
super(superstate);
this.state = state;
}


@Override
public void writeToParcel(@NonNull Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(state);
out.writeInt(peekHeight);
out.writeInt(fitToContents ? 1 : 0);
out.writeInt(hideable ? 1 : 0);
out.writeInt(skipCollapsed ? 1 : 0);
}

public static final Creator<SavedState> CREATOR =
new ClassLoaderCreator<SavedState>() {
@NonNull
@Override public SavedState createFromParcel(@NonNull Parcel in, ClassLoader loader) {
return new SavedState(in, loader);
}

@Nullable
@Override public SavedState createFromParcel(@NonNull Parcel in) {
return new SavedState(in, null);
}

@NonNull
@Override public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}

/**
* A utility function to get the {@link BottomSheetBehavior} associated with the {@code view}.
* * @param view The {@link View} with {@link BottomSheetBehavior}.
* @return The {@link BottomSheetBehavior} associated with the {@code view}.
*/ @NonNull
@SuppressWarnings("unchecked")
public static <V extends View> MinibarViewPagerBottomSheetBehavior<V> from(@NonNull V view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
if (!(params instanceof LayoutParams)) {
throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
}
CoordinatorLayout.Behavior<?> behavior =
((LayoutParams) params).getBehavior();
if (!(behavior instanceof MinibarViewPagerBottomSheetBehavior)) {
throw new IllegalArgumentException("The view is not associated with BottomSheetBehavior");
}
return (MinibarViewPagerBottomSheetBehavior<V>) behavior;
}

/**
* Sets whether the BottomSheet should update the accessibility status of its {@link
* CoordinatorLayout} siblings when expanded.
* * <p>Set this to true if the expanded state of the sheet blocks access to siblings (e.g., when
* the sheet expands over the full screen). */ public void setUpdateImportantForAccessibilityOnSiblings(
boolean updateImportantForAccessibilityOnSiblings) {
this.updateImportantForAccessibilityOnSiblings = updateImportantForAccessibilityOnSiblings;
}

private void updateImportantForAccessibility(boolean expanded) {
if (viewRef == null) {
return;
}

ViewParent viewParent = viewRef.get().getParent();
if (!(viewParent instanceof CoordinatorLayout)) {
return;
}

CoordinatorLayout parent = (CoordinatorLayout) viewParent;
final int childCount = parent.getChildCount();
if ((VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) && expanded) {
if (importantForAccessibilityMap == null) {
importantForAccessibilityMap = new HashMap<>(childCount);
} else {
// The important for accessibility values of the child views have been saved already.
return;
}
}

for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
if (child == viewRef.get()) {
continue;
}

if (expanded) {
// Saves the important for accessibility value of the child view.
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
importantForAccessibilityMap.put(child, child.getImportantForAccessibility());
}
if (updateImportantForAccessibilityOnSiblings) {
ViewCompat.setImportantForAccessibility(
child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
}
} else {
if (updateImportantForAccessibilityOnSiblings
&& importantForAccessibilityMap != null
&& importantForAccessibilityMap.containsKey(child)) {
// Restores the original important for accessibility value of the child view.
ViewCompat.setImportantForAccessibility(child, importantForAccessibilityMap.get(child));
}
}
}

if (!expanded) {
importantForAccessibilityMap = null;
}
}

private void updateAccessibilityActions() {
if (viewRef == null) {
return;
}
V child = viewRef.get();
if (child == null) {
return;
}
ViewCompat.removeAccessibilityAction(child, AccessibilityNodeInfoCompat.ACTION_COLLAPSE);
ViewCompat.removeAccessibilityAction(child, AccessibilityNodeInfoCompat.ACTION_EXPAND);
ViewCompat.removeAccessibilityAction(child, AccessibilityNodeInfoCompat.ACTION_DISMISS);

if (hideable && state != STATE_HIDDEN) {
addAccessibilityActionForState(child, AccessibilityActionCompat.ACTION_DISMISS, STATE_HIDDEN);
}

switch (state) {
case STATE_EXPANDED: {
int nextState = fitToContents ? STATE_COLLAPSED : STATE_HALF_EXPANDED;
addAccessibilityActionForState(
child, AccessibilityActionCompat.ACTION_COLLAPSE, nextState);
break;
}
case STATE_HALF_EXPANDED: {
addAccessibilityActionForState(
child, AccessibilityActionCompat.ACTION_COLLAPSE, STATE_COLLAPSED);
addAccessibilityActionForState(
child, AccessibilityActionCompat.ACTION_EXPAND, STATE_EXPANDED);
break;
}
case STATE_COLLAPSED: {
int nextState = fitToContents ? STATE_EXPANDED : STATE_HALF_EXPANDED;
addAccessibilityActionForState(child, AccessibilityActionCompat.ACTION_EXPAND, nextState);
break;
}
default: // fall out
}
}

private void addAccessibilityActionForState(
V child, AccessibilityActionCompat action, final int state) {
ViewCompat.replaceAccessibilityAction(
child,
action,
null,
new AccessibilityViewCommand() {
@Override
public boolean perform(@NonNull View view, @Nullable CommandArguments arguments) {
setState(state);
return true;
}
});
}
}

MinibarViewPagerBottomSheetDialog.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
package com.base.custom;  
/*
* Copyright (C) 2015 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. */
import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
import static com.google.android.material.color.MaterialColors.isColorLight;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatDialog;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;

import com.base.custom_bottomsheet.R;
import com.google.android.material.shape.MaterialShapeDrawable;

/**
* Base class for {@link android.app.Dialog}s styled as a bottom sheet.
* <p>In edge to edge mode, padding will be added automatically to the top when sliding under the
* status bar. Padding can be applied automatically to the left, right, or bottom if any of * `paddingBottomSystemWindowInsets`, `paddingLeftSystemWindowInsets`, or * `paddingRightSystemWindowInsets` are set to true in the style. */public class MinibarViewPagerBottomSheetDialog extends AppCompatDialog {

private MinibarViewPagerBottomSheetBehavior<FrameLayout> behavior;

private FrameLayout container;
private CoordinatorLayout coordinator;
private FrameLayout bottomSheet;

boolean dismissWithAnimation;

boolean cancelable = true;
private boolean canceledOnTouchOutside = true;
private boolean canceledOnTouchOutsideSet;
private MinibarViewPagerBottomSheetBehavior.BottomSheetCallback edgeToEdgeCallback;
private boolean edgeToEdgeEnabled;

public MinibarViewPagerBottomSheetDialog(@NonNull Context context) {
this(context, 0);

edgeToEdgeEnabled = false;
}

public MinibarViewPagerBottomSheetDialog(@NonNull Context context, @StyleRes int theme) {
super(context, getThemeResId(context, theme));
// We hide the title bar for any style configuration. Otherwise, there will be a gap
// above the bottom sheet when it is expanded. supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

edgeToEdgeEnabled = false;
}

protected MinibarViewPagerBottomSheetDialog(
@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
this.cancelable = cancelable;

edgeToEdgeEnabled = false;
}

@Override
public void setContentView(@LayoutRes int layoutResId) {
super.setContentView(wrapInBottomSheet(layoutResId, null, null));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
if (window != null) {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
// The status bar should always be transparent because of the window animation.
window.setStatusBarColor(0);

window.addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (VERSION.SDK_INT < VERSION_CODES.M) {
// It can be transparent for API 23 and above because we will handle switching the status
// bar icons to light or dark as appropriate. For API 21 and API 22 we just set the // translucent status bar. window.addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
}

@Override
public void setContentView(View view) {
super.setContentView(wrapInBottomSheet(0, view, null));
}

@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
super.setContentView(wrapInBottomSheet(0, view, params));
}

@Override
public void setCancelable(boolean cancelable) {
super.setCancelable(cancelable);
if (this.cancelable != cancelable) {
this.cancelable = cancelable;
if (behavior != null) {
behavior.setHideable(cancelable);
}
}
}

@Override
protected void onStart() {
super.onStart();
if (behavior != null && behavior.getState() == MinibarViewPagerBottomSheetBehavior.STATE_HIDDEN) {
behavior.setState(MinibarViewPagerBottomSheetBehavior.STATE_COLLAPSED);
}
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
if (window != null) {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
// If the navigation bar is transparent at all the BottomSheet should be edge to edge.
boolean drawEdgeToEdge =
edgeToEdgeEnabled && Color.alpha(window.getNavigationBarColor()) < 255;
if (container != null) {
container.setFitsSystemWindows(!drawEdgeToEdge);
}
if (coordinator != null) {
coordinator.setFitsSystemWindows(!drawEdgeToEdge);
}
if (drawEdgeToEdge) {
// Automatically set up edge to edge flags if we should be drawing edge to edge.
int edgeToEdgeFlags =
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
window.getDecorView().setSystemUiVisibility(edgeToEdgeFlags);
}
}
}
}

/**
* This function can be called from a few different use cases, including Swiping the dialog down * or calling `dismiss()` from a `BottomSheetDialogFragment`, tapping outside a dialog, etc... * <p>If this function is called from a swipe down interaction, or dismissWithAnimation is false,
* then keep the default behavior. */ @Override
public void cancel() {
MinibarViewPagerBottomSheetBehavior<FrameLayout> behavior = getBehavior();

if (!dismissWithAnimation || behavior.getState() == MinibarViewPagerBottomSheetBehavior.STATE_HIDDEN) {
super.cancel();
} else {
behavior.setState(MinibarViewPagerBottomSheetBehavior.STATE_HIDDEN);
}
}

@Override
public void setCanceledOnTouchOutside(boolean cancel) {
super.setCanceledOnTouchOutside(cancel);
if (cancel && !cancelable) {
cancelable = true;
}
canceledOnTouchOutside = cancel;
canceledOnTouchOutsideSet = true;
}

@NonNull
public MinibarViewPagerBottomSheetBehavior<FrameLayout> getBehavior() {
if (behavior == null) {
// The content hasn't been set, so the behavior doesn't exist yet. Let's create it.
ensureContainerAndBehavior();
}
return behavior;
}

/**
* Set to perform the swipe down animation when dismissing instead of the window animation for the * dialog. * * @param dismissWithAnimation True if swipe down animation should be used when dismissing.
*/ public void setDismissWithAnimation(boolean dismissWithAnimation) {
this.dismissWithAnimation = dismissWithAnimation;
}

/**
* Returns if dismissing will perform the swipe down animation on the bottom sheet, rather than * the window animation for the dialog. */ public boolean getDismissWithAnimation() {
return dismissWithAnimation;
}

/**
* Returns if edge to edge behavior is enabled for this dialog. */ public boolean getEdgeToEdgeEnabled() {
return edgeToEdgeEnabled;
}

/**
* Creates the container layout which must exist to find the behavior */ private FrameLayout ensureContainerAndBehavior() {
if (container == null) {
container =
(FrameLayout) View.inflate(getContext(), R.layout.design_minibar_bottom_sheet_dialog, null);

coordinator = (CoordinatorLayout) container.findViewById(R.id.coordinator);
bottomSheet = (FrameLayout) container.findViewById(R.id.design_bottom_sheet);

behavior = MinibarViewPagerBottomSheetBehavior.from(bottomSheet);
behavior.addBottomSheetCallback(bottomSheetCallback);
behavior.setHideable(cancelable);
}
return container;
}

private View wrapInBottomSheet(
int layoutResId, @Nullable View view, @Nullable ViewGroup.LayoutParams params) {
ensureContainerAndBehavior();
CoordinatorLayout coordinator = (CoordinatorLayout) container.findViewById(R.id.coordinator);
if (layoutResId != 0 && view == null) {
view = getLayoutInflater().inflate(layoutResId, coordinator, false);
}

if (edgeToEdgeEnabled) {
ViewCompat.setOnApplyWindowInsetsListener(
bottomSheet,
new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat insets) {
if (edgeToEdgeCallback != null) {
behavior.removeBottomSheetCallback(edgeToEdgeCallback);
}

if (insets != null) {
edgeToEdgeCallback = new EdgeToEdgeCallback(bottomSheet, insets);
behavior.addBottomSheetCallback(edgeToEdgeCallback);
}

return insets;
}
});
}

bottomSheet.removeAllViews();
if (params == null) {
bottomSheet.addView(view);
} else {
bottomSheet.addView(view, params);
}
// We treat the CoordinatorLayout as outside the dialog though it is technically inside
coordinator
.findViewById(R.id.touch_outside)
.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (cancelable && isShowing() && shouldWindowCloseOnTouchOutside()) {
cancel();
}
}
});
// Handle accessibility events
ViewCompat.setAccessibilityDelegate(
bottomSheet,
new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, @NonNull AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
if (cancelable) {
info.addAction(AccessibilityNodeInfoCompat.ACTION_DISMISS);
info.setDismissable(true);
} else {
info.setDismissable(false);
}
}

@Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
if (action == AccessibilityNodeInfoCompat.ACTION_DISMISS && cancelable) {
cancel();
return true;
}
return super.performAccessibilityAction(host, action, args);
}
});
bottomSheet.setOnTouchListener(
new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
// Consume the event and prevent it from falling through
return true;
}
});
return container;
}

boolean shouldWindowCloseOnTouchOutside() {
if (!canceledOnTouchOutsideSet) {
TypedArray a =
getContext().obtainStyledAttributes(new int[]{android.R.attr.windowCloseOnTouchOutside});
canceledOnTouchOutside = a.getBoolean(0, true);
a.recycle();
canceledOnTouchOutsideSet = true;
}
return canceledOnTouchOutside;
}

private static int getThemeResId(Context context, int themeId) {
if (themeId == 0) {
TypedValue outValue = new TypedValue();
if (context.getTheme().resolveAttribute(
com.google.android.material.R.attr.bottomSheetDialogTheme, outValue, true)) {
themeId = outValue.resourceId;
} else {
themeId = com.google.android.material.R.style.Theme_Design_Light_BottomSheetDialog;
}

}
return themeId;
}

void removeDefaultCallback() {
behavior.removeBottomSheetCallback(bottomSheetCallback);
}

@NonNull
private MinibarViewPagerBottomSheetBehavior.BottomSheetCallback bottomSheetCallback =
new MinibarViewPagerBottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(
@NonNull View bottomSheet, @MinibarViewPagerBottomSheetBehavior.State int newState) {
if (newState == MinibarViewPagerBottomSheetBehavior.STATE_HIDDEN) {
cancel();
}
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};

private static class EdgeToEdgeCallback extends MinibarViewPagerBottomSheetBehavior.BottomSheetCallback {

private final boolean lightBottomSheet;
private final boolean lightStatusBar;
private final WindowInsetsCompat insetsCompat;

private EdgeToEdgeCallback(
@NonNull final View bottomSheet, @NonNull WindowInsetsCompat insetsCompat) {
this.insetsCompat = insetsCompat;
lightStatusBar =
VERSION.SDK_INT >= VERSION_CODES.M
&& (bottomSheet.getSystemUiVisibility() & SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0;

// Try to find the background color to automatically change the status bar icons so they will
// still be visible when the bottomsheet slides underneath the status bar. ColorStateList backgroundTint;
MaterialShapeDrawable msd = MinibarViewPagerBottomSheetBehavior.from(bottomSheet).getMaterialShapeDrawable();
if (msd != null) {
backgroundTint = msd.getFillColor();
} else {
backgroundTint = ViewCompat.getBackgroundTintList(bottomSheet);
}

if (backgroundTint != null) {
// First check for a tint
lightBottomSheet = isColorLight(backgroundTint.getDefaultColor());
} else if (bottomSheet.getBackground() instanceof ColorDrawable) {
// Then check for the background color
lightBottomSheet = isColorLight(((ColorDrawable) bottomSheet.getBackground()).getColor());
} else {
// Otherwise don't change the status bar color
lightBottomSheet = lightStatusBar;
}
}

@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
setPaddingForPosition(bottomSheet);
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
setPaddingForPosition(bottomSheet);
}

private void setPaddingForPosition(View bottomSheet) {
if (bottomSheet.getTop() < insetsCompat.getSystemWindowInsetTop()) {
// If the bottomsheet is light, we should set light status bar so the icons are visible
// since the bottomsheet is now under the status bar. setLightStatusBar(bottomSheet, lightBottomSheet);
// Smooth transition into status bar when drawing edge to edge.
bottomSheet.setPadding(
bottomSheet.getPaddingLeft(),
(insetsCompat.getSystemWindowInsetTop() - bottomSheet.getTop()),
bottomSheet.getPaddingRight(),
bottomSheet.getPaddingBottom());
} else if (bottomSheet.getTop() != 0) {
// Reset the status bar icons to the original color because the bottomsheet is not under the
// status bar. setLightStatusBar(bottomSheet, lightStatusBar);
bottomSheet.setPadding(
bottomSheet.getPaddingLeft(),
0,
bottomSheet.getPaddingRight(),
bottomSheet.getPaddingBottom());
}
}
}

public static void setLightStatusBar(@NonNull View view, boolean isLight) {
if (VERSION.SDK_INT >= VERSION_CODES.M) {
int flags = view.getSystemUiVisibility();
if (isLight) {
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
view.setSystemUiVisibility(flags);
}
}
}

更新日志:

版本 时间 说明
version 1.0 2024年7月30日 初版整理(修正原方案中的缺陷)