Android 仿高德地圖可拉伸的BottomSheet的示例代碼
前言
最近項目中需要用到高德地圖搜索結果后的結果展示的可拉伸控件。
gaode.gif
而我看到這個效果圖,覺得這個就是一個slidingpanel,但是翻閱了一些發(fā)現(xiàn)用google自帶的bottomsheet實現(xiàn)更方便
什么是BottomSheet?
Bottom Sheet是Design Support Library23.2 版本引入的一個類似于對話框的控件,可以暫且叫做底部彈出框吧。 Bottom Sheet中的內(nèi)容默認是隱藏起來的,只顯示很小一部分,可以通過在代碼中設置其狀態(tài)或者手勢操作將其完全展開,或者完全隱藏,或者部分隱藏。對于Bottom Sheet的描述可以在官網(wǎng)查詢:https://material.io/design/#
怎么使用?
添加依賴
implemention 'com.android.support:design:25.3.1'
布局文件
<?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" android:id="@+id/cl_chouti" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.amap.api.maps.MapView android:id="@+id/map_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> <ImageView android:id="@+id/mark" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/poi_marker_pressed" /> <!--為了更好與定位之后的紅點適配此imagview只是適配用沒有意義--> <ImageView android:layout_width="30dp" android:layout_height="40dp" android:layout_below="@+id/mark" /> </RelativeLayout> </FrameLayout> <RelativeLayout android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:behavior_hideable="true" app:behavior_peekHeight="160dp" app:layout_behavior="@string/bottom_sheet_behavior"> <include layout="@layout/layout_bottom_sheet" /> </RelativeLayout> </android.support.design.widget.CoordinatorLayout>
layout_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical"> <TextView android:id="@+id/tv_tishi" android:layout_gravity="center_horizontal" android:layout_marginTop="40dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="附近熱點" android:textSize="10sp"/> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
activity中的使用
//底部抽屜欄展示地址 bottomSheet = findViewById(R.id.bottom_sheet); behavior = BottomSheetBehavior.from(bottomSheet); behavior.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.d("BottomSheetDemo", "slideOffset:" + slideOffset); } });
注意分析
CoordinatorLayout 是Meterial Design中的一個新控件,通過behavior用來協(xié)調(diào)其他組件, 實現(xiàn)聯(lián)動,因此父布局必須是CoordinatorLayout 。
注意到布局中,RelativeLayout中的app:layout_behavior=”@string/bottom_sheet_behavior”屬性,點進去可以看到,這個屬性實際上是設置系統(tǒng)默認實現(xiàn)的BottomSheet的behavior。原則上來說,只要是可以滾動的View,在加上了這個屬性后,都可以作為BottomSheet來使用,建議使用NestedScrollView或者RecyclerView。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android ListView 和ScroolView 出現(xiàn)onmeasure空指針的解決辦法
這篇文章主要介紹了Android ListView 和ScroolView 出現(xiàn)onmeasure空指針的解決辦法的相關資料,需要的朋友可以參考下2017-02-02Jetpack Compose實現(xiàn)列表和動畫效果詳解
這篇文章主要為大家詳細講講Jetpack Compose實現(xiàn)列表和動畫效果的方法步驟,文中的代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2022-06-06Android使用元數(shù)據(jù)實現(xiàn)配置信息的傳遞方法詳細介紹
這篇文章主要介紹了Android使用元數(shù)據(jù)實現(xiàn)配置信息的傳遞方法,也就是實現(xiàn)配置快捷菜單功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09Android實現(xiàn)信號強度監(jiān)聽的方法
這篇文章主要介紹了Android實現(xiàn)信號強度監(jiān)聽的方法,是Android手機中很常見的一個實用功能,需要的朋友可以參考下2014-08-08Android實現(xiàn)網(wǎng)易云推薦歌單界面
大家好,本篇文章主要講的是Android實現(xiàn)網(wǎng)易云推薦歌單界面,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02Android實現(xiàn)頁面翻轉(zhuǎn)和自動翻轉(zhuǎn)功能
這篇文章主要介紹了Android中簡單實現(xiàn)頁面翻轉(zhuǎn)和自動翻轉(zhuǎn)的功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10