Android BottomSheet實(shí)現(xiàn)可拉伸控件
一、簡介
Bottom Sheet是Design Support Library23.2 版本引入的一個(gè)類似于對話框的控件。 Bottom Sheet中的內(nèi)容默認(rèn)是隱藏起來的,只顯示很小一部分,可以通過在代碼中設(shè)置其狀態(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、代碼實(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:定義是否能通過下滑手勢收起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:隱藏
三、封裝的框架推薦
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android 仿ios數(shù)字密碼解鎖界面的實(shí)例
下面小編就為大家分享一篇android 仿ios數(shù)字密碼解鎖界面的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
ExpandableListView實(shí)現(xiàn)簡單二級列表
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)簡單二級列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
ListView上滑和下滑,顯示和隱藏Toolbar的實(shí)現(xiàn)方法
下面小編就為大家分享一篇ListView上滑和下滑,顯示和隱藏Toolbar的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android 檢測鍵盤顯示或隱藏鍵盤的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 檢測鍵盤顯示或隱藏鍵盤的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android中RecyclerView 滑動(dòng)時(shí)圖片加載的優(yōu)化
本篇文章主要介紹了Android中RecyclerView 滑動(dòng)時(shí)圖片加載的優(yōu)化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android自定義進(jìn)度條的圓角橫向進(jìn)度條實(shí)例詳解
本文通過實(shí)例代碼給大家詳細(xì)介紹了Android自定義進(jìn)度條的圓角橫向進(jìn)度條的相關(guān)資料。非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09
Android實(shí)現(xiàn)的狀態(tài)欄定制和修改方法
這篇文章主要介紹了Android實(shí)現(xiàn)的狀態(tài)欄定制和修改方法,涉及Android針對狀態(tài)欄屬性設(shè)置的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android自定義控件實(shí)現(xiàn)圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)圓形進(jìn)度條的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

