Android MVVM框架搭建(八)高德地图定位、天气查询、BottomSheetDialog
Android MVVM框架搭建(八)高德地圖定位、天氣查詢、BottomSheetDialog
- 前言
- 正文
- 一、集成SDK
- 二、基礎(chǔ)配置
- ① 權(quán)限配置
- ② 配置Key
- 三、顯示地圖
- ① MapFragment
- ② Navigation綁定
- ③ Fragment中地圖生命周期綁定
- 四、顯示當(dāng)前所在地
- ① 定位動態(tài)權(quán)限申請
- ② 地圖定位當(dāng)前所在地
- 五、獲取詳細的地址信息
- 六、獲取天氣數(shù)據(jù)
- 七、顯示天氣數(shù)據(jù)
- ① 實時天氣數(shù)據(jù)
- ② 天氣彈窗布局
- ③ BottomSheetDialog使用
- 八、源碼
前言
??在上一篇文章中完成了對個人用戶信息的修改,同時講述了對彈窗數(shù)據(jù)的處理,權(quán)限的使用,本文將在App中接入一個地圖SDK,同時完成天氣的查詢,完成后的效果如下圖所示:
正文
??實際上地圖和天氣我在之前的好天氣App上都寫過了,那個上面寫的就比較多了,也比較復(fù)雜了,在這里第一個是為了豐富MVVM的使用場景,同時滿足在Fragment中使用地圖的需要,第三個就是底部彈窗與MVVM的結(jié)合。
可以掃描二維碼下載體驗一下:
一、集成SDK
??本文中將會使用地圖,這里我選擇使用高德地圖,需要去集成SDK,首先需要登錄高德開放平臺去注冊,創(chuàng)建應(yīng)用,獲取key,由于之前寫過這樣的文章,因此不用再重復(fù)了,不了解的可以看看Android 高德地圖API,看完步驟一,拿到了key就可以了,沒有問題再回來本文。
點擊提交就會有一個key了。
點擊下載SDK,對SDK進行選擇,如下圖所示:
點擊下載,到本地之后解壓,如下圖所示:
將這些文件復(fù)制到你的項目的libs下,如下圖所示:
現(xiàn)在這個SDK還沒有集成的,你只是放到了項目中,打開app模塊下的build.gradle,在android{}閉包下添加如下代碼:
defaultConfig {ndk {//設(shè)置支持的SO庫架構(gòu)abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64"}}sourceSets {main {jniLibs.srcDirs = ['libs']}}如下圖所示:
然后在dependencies{}閉包下添加如下代碼:
添加位置如下:這行代碼老版本的AS是自帶的,后面新版本的沒有了。
然后點擊右上角的Sync Now,進行依賴庫下載配置同步,此時你的jar包可以展開了,就說明SDK集成成功了。
二、基礎(chǔ)配置
??SDK已經(jīng)引入,要使用還需要進行一些配置,首先是權(quán)限配置。
① 權(quán)限配置
在AndroidManifest.xml下新增如下權(quán)限:
<!--允許獲取粗略位置--><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><!--允許獲取精準位置--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><!--允許獲取wifi狀態(tài)改變,用于網(wǎng)絡(luò)定位--><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />其他的所需權(quán)限,在之前寫相應(yīng)功能的時候都已經(jīng)添加好了,因此就不用重復(fù)添加了,添加位置如下圖所示:
下面配置Key
② 配置Key
然后配置高德地圖Key,在application標簽內(nèi)
<!--高德地圖Key--><meta-dataandroid:name="com.amap.api.v2.apikey"android:value="597d296d723c5adb7165b732bc6f831e"/>添加位置如下圖所示:
現(xiàn)在來說,前期的準備工作就差不多了,下面就要去實現(xiàn)地圖顯示和定位了。
三、顯示地圖
??我們讓地圖顯示在HomeActivity上,因此我們需要創(chuàng)建一個MapFragment去加載地圖,一些讀者只看到我用Activity加載過地圖,換到Fragment上就不會用了,我很心痛啊。痛定思痛之后,我決定在Fragment上演示一下地圖怎么操作,當(dāng)然這是很簡單的,我也會慢慢提高難度,你準備好了嗎?
① MapFragment
首先在fragment包下新建一個MapFragment,對應(yīng)的布局是map_fragment.xml,布局代碼如下:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ui.fragment.MapFragment"><com.amap.api.maps.MapViewandroid:id="@+id/map_view"android:layout_width="match_parent"android:layout_height="match_parent"/></FrameLayout> </layout>很簡單,就一個地圖。下面回到MapFragment,里面的代碼如下:
public class MapFragment extends BaseFragment {private MapFragmentBinding binding;public static MapFragment newInstance() {return new MapFragment();}@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {binding = DataBindingUtil.inflate(inflater,R.layout.map_fragment,container,false);return binding.getRoot();}@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);// TODO: Use the ViewModel}}這里只是簡單的繼承了一下BaseFragment,然后就是DataBinding的綁定。
② Navigation綁定
然后我們需要在nav_graph.xml中添加這個布局。
<!--地圖--><fragmentandroid:id="@+id/map_fragment"android:name="com.llw.mvvm.ui.fragment.MapFragment"android:label="map_fragment"tools:layout="@layout/map_fragment" />??這里配置了,那么底部的菜單同樣要配置,畢竟我們是通過菜單去控制Fragment的切換的,打開navigation_menu.xml,在里面添加一個item,代碼如下:
<itemandroid:id="@+id/map_fragment"android:icon="@mipmap/ic_map"android:title="地圖" />這個圖標可以去我的源碼里面去找,或者自己去網(wǎng)上找一個也行。
下面進入到HomeActivity中去配置,配置切換菜單時的Fragment布局改變,如下圖所示:
③ Fragment中地圖生命周期綁定
要顯示地圖需要將地圖的生命周期與Fragment的生命周期綁定起來,如下圖所示:
如果你是線上的項目你需要在隱私政策中引入高德SDK的說明,然后在MapFragment中綁定地圖的生命周期。
下面運行一下:
現(xiàn)在只是顯示了地圖,但是并沒有定位到我當(dāng)前所在地,這當(dāng)然是不行的。
四、顯示當(dāng)前所在地
??顯示當(dāng)前所在地則需要定位權(quán)限,之前在AndroidManifest.xml中已經(jīng)配置好了,下面則需要在代碼中動態(tài)請求。
① 定位動態(tài)權(quán)限申請
在上一篇文章中寫過一個PermissionUtils類,這里給這個類再加一點東西進去,在PermissionUtils中增加如下代碼:
public static final String LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;public static final int REQUEST_LOCATION_CODE = 1003;然后在getPermissionRequestCode方法中增加一個case,如下圖所示:
下面就是在HomeActivity中請求動態(tài)權(quán)限了,在HomeActivity中新增如下方法:
然后在initView方法中調(diào)用它,如下圖所示:
② 地圖定位當(dāng)前所在地
下面回到MapFragment,新增如下代碼:
private static final String TAG = MapFragment.class.getSimpleName();/*** 初始化地圖*/private void initMap() {//初始化地圖控制器對象AMap aMap = binding.mapView.getMap();// 設(shè)置為true表示顯示定位層并可觸發(fā)定位,false表示隱藏定位層并不可觸發(fā)定位,默認是falseaMap.setMyLocationEnabled(true);MyLocationStyle style = new MyLocationStyle();//初始化定位藍點樣式類myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//連續(xù)定位、且將視角移動到地圖中心點,定位點依照設(shè)備方向旋轉(zhuǎn),并且會跟隨設(shè)備移動。(1秒1次定位)如果不設(shè)置myLocationType,默認也會執(zhí)行此種模式。style.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;//定位一次,且將視角移動到地圖中心點。aMap.setMyLocationStyle(style);//設(shè)置定位藍點的StyleaMap.getUiSettings().setMyLocationButtonEnabled(true);//設(shè)置默認定位按鈕是否顯示,非必需設(shè)置。aMap.setMyLocationEnabled(true);// 設(shè)置為true表示啟動顯示定位藍點,false表示隱藏定位藍點并不進行定位,默認是false。//設(shè)置SDK 自帶定位消息監(jiān)聽aMap.setOnMyLocationChangeListener(this);}@Overridepublic void onMyLocationChange(Location location) {// 定位回調(diào)監(jiān)聽if(location != null) {Log.e(TAG, "onMyLocationChange 定位成功, lat: " + location.getLatitude() + " lon: " + location.getLongitude());} else {Log.e(TAG, "定位失敗");}}實現(xiàn)位置監(jiān)聽
調(diào)用initMap()
運行一下
五、獲取詳細的地址信息
??通過地圖定位的方式定位在了當(dāng)前所在地,但是onMyLocationChange的location中只有當(dāng)前所在地的經(jīng)緯度,如果我們需要拿到詳細的地址信息要怎么做呢?通過SearchSDK實現(xiàn),通過逆地理編碼來將坐標轉(zhuǎn)換成詳細的地址。依然是在MapFragment,繼承GeocodeSearch.OnGeocodeSearchListener,實現(xiàn)兩個回調(diào)方法。代碼如下:
/*** 坐標轉(zhuǎn)地址*/@Overridepublic void onRegeocodeSearched(RegeocodeResult regeocodeResult, int rCode) {}/*** 地址轉(zhuǎn)坐標*/@Overridepublic void onGeocodeSearched(GeocodeResult geocodeResult, int rCode) {}然后這個也需要初始化,代碼如下:
//解析成功標識碼private static final int PARSE_SUCCESS_CODE = 1000;private GeocodeSearch geocoderSearch = null;private String district = null;// 區(qū)/縣下面寫一個方法初始化搜索
/*** 初始化搜索*/private void initSearch() {try {geocoderSearch = new GeocodeSearch(requireActivity());geocoderSearch.setOnGeocodeSearchListener(this);} catch (AMapException e) {e.printStackTrace();}}調(diào)用的地方如下圖:
當(dāng)收位置信息改變時,進行坐標的搜索,在onMyLocationChange中添加如下代碼:
添加位置如下:
??然后就會觸發(fā)onRegeocodeSearched的回調(diào),在onRegeocodeSearched中則對所在地的信息進行打印和簡單的區(qū)/縣賦值,在onRegeocodeSearched方法中添加如下代碼:
添加位置如下:
下面運行一下:
這樣就拿到了詳細的位置信息。
六、獲取天氣數(shù)據(jù)
??高德是自帶了天氣數(shù)據(jù)接口的,可以用,只不過數(shù)據(jù)不是很多,如果需要更多的數(shù)據(jù)的話可以自己去對接天氣API,例如和風(fēng)、彩云。
在MapFragment創(chuàng)建變量
然后MapFragment繼承WeatherSearch.OnWeatherSearchListener,實現(xiàn)兩個方法。
/*** 實時天氣返回*/@Overridepublic void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int code) {liveResult = localWeatherLiveResult.getLiveResult();if (liveResult != null) {Log.e(TAG, "onWeatherLiveSearched: " + new Gson().toJson(liveResult));} else {showMsg("實時天氣數(shù)據(jù)為空");}}/*** 天氣預(yù)報返回*/@Overridepublic void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int code) {forecastResult = localWeatherForecastResult.getForecastResult();if (forecastResult != null) {Log.e(TAG, "onWeatherForecastSearched: " + new Gson().toJson(forecastResult));} else {showMsg("天氣預(yù)報數(shù)據(jù)為空");}}在方法回調(diào)中打印一下返回的數(shù)據(jù),然后寫一個搜索天氣的方法,根據(jù)傳入不同的天氣類型,進行不同的天氣數(shù)據(jù)搜索,代碼如下:
/*** 搜索天氣** @param type WEATHER_TYPE_LIVE 實時天氣 WEATHER_TYPE_FORECAST 預(yù)報天氣*/private void searchWeather(int type) {WeatherSearchQuery weatherSearchQuery = new WeatherSearchQuery(district, type);try {WeatherSearch weatherSearch = new WeatherSearch(requireActivity());weatherSearch.setOnWeatherSearchListener(this);weatherSearch.setQuery(weatherSearchQuery);weatherSearch.searchWeatherAsyn(); //異步搜索} catch (AMapException e) {e.printStackTrace();}}最后在onRegeocodeSearched中,拿到地址信息時調(diào)用searchWeather方法,代碼如下:
//搜索天氣 實時天氣和預(yù)報天氣searchWeather(WeatherSearchQuery.WEATHER_TYPE_LIVE);searchWeather(WeatherSearchQuery.WEATHER_TYPE_FORECAST);添加位置如下:
下面運行一下,查看日志,天氣的數(shù)據(jù)就有了
七、顯示天氣數(shù)據(jù)
??有了天氣數(shù)據(jù)之后就是顯示天氣數(shù)據(jù)了,這里我們可以這么做,就是在MapFragment中添加一個浮動按鈕,點擊之后從屏幕底部彈出一個,先來修改一下map_fragment中的代碼,我們增加一個浮動按鈕。
<com.google.android.material.floatingactionbutton.FloatingActionButtonandroid:id="@+id/fab_weather"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="end|bottom"android:layout_margin="20dp"android:visibility="gone"android:contentDescription="天氣"android:src="@mipmap/ic_weather"app:backgroundTint="@color/white"app:fabSize="auto"tools:ignore="UsingOnClickInXml" />添加的位置就如下圖所示:
??這里我先把這個按鈕給隱藏了,當(dāng)?shù)貓D上獲取了當(dāng)前的位置,查詢到了天氣之后再顯示這個按鈕。下面回到MapFragment中,在initMap方法中增加一行代碼,
//修改放大縮小按鈕的位置aMap.getUiSettings().setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_CENTER);這樣做就是讓我們的浮動按鈕不至于擋住這個地圖的放大縮小按鈕。
在MapFragment中添加一個變量
//天氣預(yù)報列表private List<LocalDayWeatherForecast> weatherForecast;然后在onWeatherForecastSearched回調(diào)中對找個變量賦值,這才是實際的天氣數(shù)據(jù)
查詢到天氣預(yù)報數(shù)據(jù)后,顯示這個按鈕。
① 實時天氣數(shù)據(jù)
??現(xiàn)在已經(jīng)可以看到所在地的天氣了,當(dāng)需要顯示出來的時候你會發(fā)現(xiàn)找個數(shù)據(jù)里面是沒有所在地的區(qū)/縣的,只有省和市。因此在model包下新建一個LiveWeather,把我們在通過你地理編碼返回時的區(qū)/縣的值放進去,代碼如下:
public class LiveWeather {private String district;private LocalWeatherLive localWeatherLive;public LiveWeather(String district, LocalWeatherLive localWeatherLive) {this.district = district;this.localWeatherLive = localWeatherLive;}public String getDistrict() {return district;}public void setDistrict(String district) {this.district = district;}public LocalWeatherLive getLocalWeatherLive() {return localWeatherLive;}public void setLocalWeatherLive(LocalWeatherLive localWeatherLive) {this.localWeatherLive = localWeatherLive;} }這個數(shù)據(jù)將會綁定到我們的天氣彈窗,現(xiàn)在來創(chuàng)建這個彈窗的布局。
② 天氣彈窗布局
??彈窗布局分為兩個環(huán)節(jié),一個是實時天氣,一個是預(yù)報天氣。首先colors.xml創(chuàng)建一個顏色值,如下:
<color name="translucent">#90000000</color>然后在drawable下創(chuàng)建一個shape_translucent_radius_12.xml樣式文件,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="12dp"/><solid android:color="@color/translucent"/> </shape>下面創(chuàng)建彈窗的布局,在layout下新建一個dialog_weather.xml,里面的代碼如下:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"><data><variablename="liveWeather"type="com.llw.mvvm.model.LiveWeather" /></data><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#00000000"android:orientation="vertical"android:paddingStart="12dp"android:paddingEnd="12dp"android:paddingBottom="?attr/actionBarSize"><TextViewandroid:id="@+id/tv_city"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="12dp"android:background="@drawable/shape_translucent_radius_12"android:gravity="center"android:padding="12dp"android:text="@{liveWeather.district}"android:textColor="@color/white"android:textSize="28sp" /><TextViewandroid:id="@+id/tv_weather"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/tv_report_time"android:layout_below="@+id/tv_city"android:layout_alignParentStart="true"android:layout_marginEnd="12dp"android:layout_marginBottom="12dp"android:background="@drawable/shape_translucent_radius_12"android:gravity="center"android:padding="12dp"android:text="@{liveWeather.localWeatherLive.weather}"android:textColor="@color/white"android:textSize="24sp" /><LinearLayoutandroid:id="@+id/wind_lay"android:layout_width="wrap_content"android:layout_height="44dp"android:layout_below="@+id/tv_city"android:layout_toStartOf="@+id/tv_temp"android:layout_toEndOf="@+id/tv_weather"android:background="@drawable/shape_translucent_radius_12"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_wind_direction"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="12dp"android:text="@{liveWeather.localWeatherLive.windDirection+`風(fēng)`}"android:textColor="@color/white" /><TextViewandroid:id="@+id/tv_wind_power"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="12dp"android:text="@{liveWeather.localWeatherLive.windPower+`級`}"android:textColor="@color/white" /></LinearLayout><LinearLayoutandroid:id="@+id/humidity_lay"android:layout_width="wrap_content"android:layout_height="44dp"android:layout_below="@+id/wind_lay"android:layout_marginTop="12dp"android:layout_toStartOf="@+id/tv_temp"android:layout_toEndOf="@+id/tv_weather"android:background="@drawable/shape_translucent_radius_12"android:gravity="center"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="24dp"android:text="濕度"android:textColor="@color/white" /><TextViewandroid:id="@+id/tv_humidity"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@{liveWeather.localWeatherLive.humidity+`%`}"android:textColor="@color/white" /></LinearLayout><TextViewandroid:id="@+id/tv_temp"android:layout_width="100dp"android:layout_height="100dp"android:layout_below="@+id/tv_city"android:layout_alignParentEnd="true"android:layout_marginStart="12dp"android:layout_marginBottom="12dp"android:background="@drawable/shape_translucent_radius_12"android:gravity="center"android:padding="12dp"android:text="@{liveWeather.localWeatherLive.temperature+`℃`}"android:textColor="@color/white"android:textSize="32sp" /><TextViewandroid:id="@+id/tv_report_time"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/tv_temp"android:background="@drawable/shape_translucent_radius_12"android:gravity="center"android:padding="12dp"android:text="@{liveWeather.localWeatherLive.reportTime+`發(fā)布`}"android:textColor="@color/white" /><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_forecast"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/tv_report_time"android:layout_marginTop="12dp" /></RelativeLayout> </layout>有列表就有對應(yīng)的item布局,在layout下創(chuàng)建item_forecast.xml布局,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"><data><variablename="forecast"type="com.amap.api.services.weather.LocalDayWeatherForecast" /><!--引入一個工具類--><import type="com.llw.mvvm.utils.EasyDate" /></data><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="12dp"android:background="@drawable/shape_translucent_radius_12"android:padding="12dp"><TextViewandroid:id="@+id/tv_date"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@{forecast.date}"android:textColor="@color/white" /><TextViewandroid:id="@+id/tv_week"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="12dp"android:layout_toEndOf="@+id/tv_date"android:text="@{EasyDate.getWeek(forecast.date)}"android:textColor="@color/white" /><TextViewandroid:id="@+id/tv_temp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:text="@{forecast.dayTemp+`° /`+ forecast.nightTemp+`°`}"android:textColor="@color/white" /></RelativeLayout> </layout>??這里我引入的數(shù)據(jù)源是高德的,同時我還插入了一個工具類,這個工具類用于對日期進行處理,可以直接在xml中調(diào)用,例如將2021-12-22,轉(zhuǎn)換成星期三。
布局都寫好了,下面在themes.xml中增加一個樣式,代碼如下:
<style name="BottomSheetDialogStyle_Light" parent="Theme.Design.BottomSheetDialog"><item name="android:windowFrame">@null</item><item name="android:windowIsFloating">true</item><item name="android:windowIsTranslucent">true</item><item name="android:background">@android:color/transparent</item><item name="android:backgroundDimEnabled">false</item></style>這表示底部彈窗出現(xiàn)是不會使我們的屏幕變暗。
③ BottomSheetDialog使用
在寫之前,我們先寫一個ForecastAdapter,這是天氣預(yù)報的列表適配器,在adapter包下創(chuàng)建它,代碼如下:
public class ForecastAdapter extends BaseQuickAdapter<LocalDayWeatherForecast, BaseDataBindingHolder<ItemForecastBinding>> {public ForecastAdapter(@Nullable List<LocalDayWeatherForecast> data) {super(R.layout.item_forecast, data);}@Overrideprotected void convert(@NotNull BaseDataBindingHolder<ItemForecastBinding> bindingHolder, LocalDayWeatherForecast localDayWeatherForecast) {ItemForecastBinding binding = bindingHolder.getDataBinding();if (binding != null) {binding.setForecast(localDayWeatherForecast);binding.executePendingBindings();}} }下面在MapFragment中添加一個方法,代碼如下:
/*** 顯示天氣彈窗*/private void showWeatherDialog() {//隱藏浮動按鈕binding.fabWeather.hide();BottomSheetDialog dialog = new BottomSheetDialog(requireActivity(), R.style.BottomSheetDialogStyle_Light);DialogWeatherBinding weatherBinding = DataBindingUtil.inflate(LayoutInflater.from(requireActivity()), R.layout.dialog_weather, null, false);//設(shè)置數(shù)據(jù)源weatherBinding.setLiveWeather(new LiveWeather(district,liveResult));//配置天氣預(yù)報列表ForecastAdapter forecastAdapter = new ForecastAdapter(weatherForecast);weatherBinding.rvForecast.setLayoutManager(new LinearLayoutManager(requireActivity()));weatherBinding.rvForecast.setAdapter(forecastAdapter);dialog.setContentView(weatherBinding.getRoot());dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundColor(Color.TRANSPARENT);//彈窗關(guān)閉時顯示浮動按鈕dialog.setOnDismissListener(dialog1 -> binding.fabWeather.show());dialog.show();}??當(dāng)點擊浮動按鈕的時候就會出現(xiàn)這個彈窗,出現(xiàn)彈窗后就隱藏浮動按鈕,然后我們對彈窗的樣式進行了修改,同時綁定數(shù)據(jù),設(shè)置數(shù)據(jù)到xml中,在再配置列表數(shù)據(jù)和適配器,最后是設(shè)置背景透明,以及彈窗消失時顯示浮動按鈕。
最后在onActivityCreated的方法中進行浮動按鈕的點擊事件處理,代碼如下:
//點擊按鈕顯示天氣彈窗binding.fabWeather.setOnClickListener(v -> showWeatherDialog());
下面來運行一下:
效果就是這個樣子,應(yīng)該是比較的明顯了。
八、源碼
GitHub:MVVM-Demo
CSDN: MVVMDemo_8.rar
總結(jié)
以上是生活随笔為你收集整理的Android MVVM框架搭建(八)高德地图定位、天气查询、BottomSheetDialog的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django中的关联查询
- 下一篇: android sensor hal,A