Android BottomSheet實現(xiàn)可拉伸控件
一、簡介
Bottom Sheet是Design Support Library23.2 版本引入的一個類似于對話框的控件。 Bottom Sheet中的內(nèi)容默認是隱藏起來的,只顯示很小一部分,可以通過在代碼中設置其狀態(tài)或者手勢操作將其完全展開,或者完全隱藏,或者部分隱藏。
二、使用
1、添加依賴:
implementation 'com.android.support:design:28.0.0'
2、布局
<?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、代碼實現(xiàn)
//底部抽屜欄展示地址 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";//過渡狀態(tài)此時用戶正在向上或者向下拖動bottom sheet break; case 2: state = "STATE_SETTLING"; // 視圖從脫離手指自由滑動到最終停下的這一小段時間 break; case 3: state = "STATE_EXPANDED"; //處于完全展開的狀態(tài) break; case 4: state = "STATE_COLLAPSED"; //默認的折疊狀態(tài) break; case 5: state = "STATE_HIDDEN"; //下滑動完全隱藏 bottom sheet break; } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { Log.i("BottomSheetDemo", "slideOffset:" + slideOffset); } });
4、幾個屬性含義:
// behavior_hideable:定義是否能通過下滑手勢收起B(yǎng)ottom Sheet。 app:behavior_hideable="true" // behavior_peekHeight:定義可見部分的高度。 app:behavior_peekHeight="80dp" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
5、BottomSheet的五種狀態(tài):
STATE_DRAGGING:手指在BottomSheet上下拖動從而使得布局跟著上下移動
STATE_SETTLING:當手指抬起之后,會根據(jù)當前的偏移量,決定是要將BottomSheet收起還是展開
這兩種屬于中間態(tài),類似于ViewPager的SCROLL_STATE_DRAGGING和SCROLL_STATE_SETTLING
--------------------------------------
STATE_EXPANDED:展開
STATE_COLLAPSED:收起
STATE_HIDDEN:隱藏
三、封裝的框架推薦
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Flutter通過Container實現(xiàn)時間軸效果
時間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實現(xiàn),感興趣的朋友可以了解下2021-05-05Android自定義ViewPagerIndicator實現(xiàn)炫酷導航欄指示器(ViewPager+Fragment)
這篇文章主要為大家詳細介紹了Android自定義ViewPagerIndicator實現(xiàn)炫酷導航欄指示器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android控件系列之Button以及Android監(jiān)聽器使用介紹
Button是各種UI中最常用的控件之一,它同樣也是Android開發(fā)中最受歡迎的控件之一,用戶可以通過觸摸它來觸發(fā)一系列事件,要知道一個沒有點擊事件的Button是沒有任何意義的,因為使用者的固定思維是見到它就想去點2012-11-11Kotlin結(jié)合Rxjava+Retrofit實現(xiàn)極簡網(wǎng)絡請求的方法
這篇文章主要給大家介紹了關于Kotlin結(jié)合Rxjava+Retrofit實現(xiàn)極簡網(wǎng)絡請求的相關內(nèi)容,文中分別對Rxjava和Retrofit進行了簡單的介紹,然后通過示例代碼詳細介紹了如何實現(xiàn)極簡網(wǎng)絡請求,需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11