一、简介
Bottom Sheet是Design Support Library23.2 版本引入的一个类似于对话框的控件。 Bottom Sheet中的内容默认是隐藏起来的,只显示很小一部分,可以通过在代码中设置其状态或者手势操作将其完全展开,或者完全隐藏,或者部分隐藏。
二、使用
1、添加依赖:
1
|
implementation 'com.android.support:design:28.0.0' |
2、布局
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
|
<? xml version = "1.0" encoding = "utf-8" ?> < android.support.design.widget.CoordinatorLayout 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:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" > < com.amap.api.maps.MapView android:id = "@+id/map" android:layout_width = "match_parent" android:layout_height = "match_parent" /> < RelativeLayout android:id = "@+id/bottom_sheet" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginBottom = "@dimen/height52px" app:behavior_hideable = "false" app:behavior_peekHeight = "@dimen/height84px" app:layout_behavior = "android.support.design.widget.BottomSheetBehavior" tools:ignore = "MissingPrefix" android:background = "#ffffffff" > < include layout = "@layout/bottom_sheet" /> </ RelativeLayout > </ android.support.design.widget.CoordinatorLayout > <? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "@dimen/height216px" > < TextView android:layout_width = "match_parent" android:layout_height = "100dp" android:gravity = "center" android:text = "bottom_sheet_peek" /> </ RelativeLayout > |
3、代码实现
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
|
//底部抽屉栏展示地址 mBehavior = BottomSheetBehavior.from(mRelativeLayout); mBehavior.setBottomSheetCallback( new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged( @NonNull View bottomSheet, @BottomSheetBehavior .State int newState) { String state = "null" ; switch (newState) { case 1 : state = "STATE_DRAGGING" ; //过渡状态此时用户正在向上或者向下拖动bottom sheet break ; case 2 : state = "STATE_SETTLING" ; // 视图从脱离手指自由滑动到最终停下的这一小段时间 break ; case 3 : state = "STATE_EXPANDED" ; //处于完全展开的状态 break ; case 4 : state = "STATE_COLLAPSED" ; //默认的折叠状态 break ; case 5 : state = "STATE_HIDDEN" ; //下滑动完全隐藏 bottom sheet break ; } } @Override public void onSlide( @NonNull View bottomSheet, float slideOffset) { Log.i( "BottomSheetDemo" , "slideOffset:" + slideOffset); } }); |
4、几个属性含义:
1
2
3
4
5
|
// behavior_hideable:定义是否能通过下滑手势收起Bottom Sheet。 app:behavior_hideable= "true" // behavior_peekHeight:定义可见部分的高度。 app:behavior_peekHeight= "80dp" app:layout_behavior= "android.support.design.widget.BottomSheetBehavior" |
5、BottomSheet的五种状态:
STATE_DRAGGING:手指在BottomSheet上下拖动从而使得布局跟着上下移动
STATE_SETTLING:当手指抬起之后,会根据当前的偏移量,决定是要将BottomSheet收起还是展开这两种属于中间态,类似于ViewPager的SCROLL_STATE_DRAGGING和SCROLL_STATE_SETTLING
--------------------------------------
STATE_EXPANDED:展开
STATE_COLLAPSED:收起
STATE_HIDDEN:隐藏
三、封装的框架推荐
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/chaoyu168/article/details/103080145