Android如何仿高德地圖實(shí)現(xiàn)可拉伸的BottomSheet-創(chuàng)新互聯(lián)

這篇文章主要介紹了Android如何仿高德地圖實(shí)現(xiàn)可拉伸的BottomSheet的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Android如何仿高德地圖實(shí)現(xiàn)可拉伸的BottomSheet文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、金秀ssl等。為超過(guò)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的金秀網(wǎng)站制作公司

效果圖:


Android如何仿高德地圖實(shí)現(xiàn)可拉伸的BottomSheet

而我看到這個(gè)效果圖,覺(jué)得這個(gè)就是一個(gè)slidingpanel,但是翻閱了一些發(fā)現(xiàn)用google自帶的bottomsheet實(shí)現(xiàn)更方便

什么是BottomSheet?


Bottom Sheet是Design Support Library23.2 版本引入的一個(gè)類似于對(duì)話框的控件,可以暫且叫做底部彈出框吧。 Bottom Sheet中的內(nèi)容默認(rèn)是隱藏起來(lái)的,只顯示很小一部分,可以通過(guò)在代碼中設(shè)置其狀態(tài)或者手勢(shì)操作將其完全展開(kāi),或者完全隱藏,或者部分隱藏。

怎么使用?


添加依賴


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" />
   <!--為了更好與定位之后的紅點(diǎn)適配此imagview只是適配用沒(méi)有意義-->
   <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="附近熱點(diǎn)"
  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";//過(guò)渡狀態(tài)此時(shí)用戶正在向上或者向下拖動(dòng)bottom sheet
      break;
     case 2:
      state = "STATE_SETTLING"; // 視圖從脫離手指自由滑動(dòng)到最終停下的這一小段時(shí)間
      break;
     case 3:
      state = "STATE_EXPANDED"; //處于完全展開(kāi)的狀態(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.d("BottomSheetDemo", "slideOffset:" + slideOffset);
   }
  });

注意分析


CoordinatorLayout 是Meterial Design中的一個(gè)新控件,通過(guò)behavior用來(lái)協(xié)調(diào)其他組件, 實(shí)現(xiàn)聯(lián)動(dòng),因此父布局必須是CoordinatorLayout 。


注意到布局中,RelativeLayout中的app:layout_behavior=”@string/bottom_sheet_behavior”屬性,點(diǎn)進(jìn)去可以看到,這個(gè)屬性實(shí)際上是設(shè)置系統(tǒng)默認(rèn)實(shí)現(xiàn)的BottomSheet的behavior。原則上來(lái)說(shuō),只要是可以滾動(dòng)的View,在加上了這個(gè)屬性后,都可以作為BottomSheet來(lái)使用,建議使用NestedScrollView或者RecyclerView。


關(guān)于“Android如何仿高德地圖實(shí)現(xiàn)可拉伸的BottomSheet”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Android如何仿高德地圖實(shí)現(xiàn)可拉伸的BottomSheet”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站欄目:Android如何仿高德地圖實(shí)現(xiàn)可拉伸的BottomSheet-創(chuàng)新互聯(lián)
標(biāo)題URL:http://bm7419.com/article32/dicosc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、服務(wù)器托管、ChatGPT、品牌網(wǎng)站建設(shè)、虛擬主機(jī)、軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司