Android BottomSheet實(shí)現(xiàn)可拉伸控件
一、簡(jiǎn)介
Bottom Sheet是Design Support Library23.2 版本引入的一個(gè)類似于對(duì)話框的控件。 Bottom Sheet中的內(nèi)容默認(rèn)是隱藏起來(lái)的,只顯示很小一部分,可以通過在代碼中設(shè)置其狀態(tài)或者手勢(shì)操作將其完全展開,或者完全隱藏,或者部分隱藏。
二、使用
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、代碼實(shí)現(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)此時(shí)用戶正在向上或者向下拖動(dòng)bottom sheet break; case 2: state = "STATE_SETTLING"; // 視圖從脫離手指自由滑動(dòng)到最終停下的這一小段時(shí)間 break; case 3: state = "STATE_EXPANDED"; //處于完全展開的狀態(tài) break; case 4: state = "STATE_COLLAPSED"; //默認(rèn)的折疊狀態(tài) break; case 5: state = "STATE_HIDDEN"; //下滑動(dòng)完全隱藏 bottom sheet break; } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { Log.i("BottomSheetDemo", "slideOffset:" + slideOffset); } });
4、幾個(gè)屬性含義:
// behavior_hideable:定義是否能通過下滑手勢(shì)收起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上下拖動(dòng)從而使得布局跟著上下移動(dòng)
STATE_SETTLING:當(dāng)手指抬起之后,會(huì)根據(jù)當(dāng)前的偏移量,決定是要將BottomSheet收起還是展開
這兩種屬于中間態(tài),類似于ViewPager的SCROLL_STATE_DRAGGING和SCROLL_STATE_SETTLING
--------------------------------------
STATE_EXPANDED:展開
STATE_COLLAPSED:收起
STATE_HIDDEN:隱藏
三、封裝的框架推薦
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter通過Container實(shí)現(xiàn)時(shí)間軸效果
時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android自定義ViewPagerIndicator實(shí)現(xiàn)炫酷導(dǎo)航欄指示器(ViewPager+Fragment)
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewPagerIndicator實(shí)現(xiàn)炫酷導(dǎo)航欄指示器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android下SDL2實(shí)現(xiàn)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了Android下SDL2實(shí)現(xiàn)五子棋游戲的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02Android實(shí)現(xiàn)登錄郵箱的自動(dòng)補(bǔ)全功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)登錄郵箱的自動(dòng)補(bǔ)全功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04Android控件系列之Button以及Android監(jiān)聽器使用介紹
Button是各種UI中最常用的控件之一,它同樣也是Android開發(fā)中最受歡迎的控件之一,用戶可以通過觸摸它來(lái)觸發(fā)一系列事件,要知道一個(gè)沒有點(diǎn)擊事件的Button是沒有任何意義的,因?yàn)槭褂谜叩墓潭ㄋ季S是見到它就想去點(diǎn)2012-11-11android 預(yù)加載進(jìn)程的實(shí)現(xiàn)方法
這篇文章主要介紹了android 預(yù)加載進(jìn)程的實(shí)現(xiàn)方法,大家需要注意清單文件中注冊(cè)并制定 android:process 要預(yù)加載的進(jìn)程,需要的朋友可以參考下2024-05-05Kotlin結(jié)合Rxjava+Retrofit實(shí)現(xiàn)極簡(jiǎn)網(wǎng)絡(luò)請(qǐng)求的方法
這篇文章主要給大家介紹了關(guān)于Kotlin結(jié)合Rxjava+Retrofit實(shí)現(xiàn)極簡(jiǎn)網(wǎng)絡(luò)請(qǐng)求的相關(guān)內(nèi)容,文中分別對(duì)Rxjava和Retrofit進(jìn)行了簡(jiǎn)單的介紹,然后通過示例代碼詳細(xì)介紹了如何實(shí)現(xiàn)極簡(jiǎn)網(wǎng)絡(luò)請(qǐng)求,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-11-11Android自定義View實(shí)現(xiàn)微信語(yǔ)音界面
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)微信語(yǔ)音界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11