【Android開源庫】CityPicker:類似美團等APP城市選擇器
CityPicker
現在使用較多的類似美團、外賣等APP的城市選擇介面, 一行程式碼搞定 ,就是這麼簡單粗暴!!!
Tips:(舊版本1.x)會報高德定位jar包衝突,推薦使用2.x版本。
主要功能:
- 字母懸浮欄
- 指定熱門城市
- 自定義動畫效果
- 自定義主題
- 名稱或拼音搜尋
- 返回城市名、code等資料
- 提供定位介面,解耦定位SDK
Preview

預設

主題

搜尋
APK
下載 ofollow,noindex">demo.apk 體驗.
Download
library已上傳 Jitpack ,需要在root build.gradle裡新增如下程式碼:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
add dependency:
dependencies { implementation 'com.github.zaaach:CityPicker:2.0.3' //必選 implementation 'com.android.support:recyclerview-v7:27.1.1' //必選 }
or Maven:
<dependency> <groupId>com.zaaach</groupId> <artifactId>citypicker</artifactId> <version>2.0.3</version> <type>pom</type> </dependency>
or 下載library手動匯入.
Usage
CityPicker
基於 DialogFragment
實現,已提供定位介面,需要APP自身實現定位。
基本使用:
Step1:
在 manifest.xml
中給使用 CityPicker
的 activity
新增主題 android:theme="@style/DefaultCityPickerTheme"
<activity android:name=".MainActivity" android:theme="@style/DefaultCityPickerTheme"> ...... </activity>
Step2:
注意:熱門城市使用 HotCity
,定位城市使用 LocatedCity
List<HotCity> hotCities = new ArrayList<>(); hotCities.add(new HotCity("北京", "北京", "101010100")); //code為城市程式碼 hotCities.add(new HotCity("上海", "上海", "101020100")); hotCities.add(new HotCity("廣州", "廣東", "101280101")); hotCities.add(new HotCity("深圳", "廣東", "101280601")); hotCities.add(new HotCity("杭州", "浙江", "101210101")); ...... CityPicker.from(activity) //activity或者fragment .enableAnimation(true)//啟用動畫效果,預設無 .setAnimationStyle(anim)//自定義動畫 .setLocatedCity(new LocatedCity("杭州", "浙江", "101210101")))//APP自身已定位的城市,傳null會自動定位(預設) .setHotCities(hotCities)//指定熱門城市 .setOnPickListener(new OnPickListener() { @Override public void onPick(int position, City data) { Toast.makeText(getApplicationContext(), data.getName(), Toast.LENGTH_SHORT).show(); } @Override public void onCancel(){ Toast.makeText(getApplicationContext(), "取消選擇", Toast.LENGTH_SHORT).show(); } @Override public void onLocate() { //定位介面,需要APP自身實現,這裡模擬一下定位 new Handler().postDelayed(new Runnable() { @Override public void run() { //定位完成之後更新資料 CityPicker.getInstance() .locateComplete(new LocatedCity("深圳", "廣東", "101280601"), LocateState.SUCCESS); } }, 3000); } }) .show();
關於自定義主題:
在 style.xml
中自定義主題並且繼承 DefaultCityPickerTheme
,別忘了在 manifest.xml
設定給 activity
。
<style name="CustomTheme" parent="DefaultCityPickerTheme"> <item name="cpCancelTextColor">@color/color_green</item> <item name="cpSearchCursorDrawable">@color/color_green</item> <item name="cpIndexBarNormalTextColor">@color/color_green</item> <item name="cpIndexBarSelectedTextColor">@color/color_green</item> <item name="cpSectionHeight">@dimen/custom_section_height</item> <item name="cpOverlayBackground">@color/color_green</item> ...... </style>
CityPicker
中自定義的所有屬性如下,有些屬性值必須是引用型別 refrence
,使用時注意。
<resources> <attr name="cpCancelTextSize" format="dimension|reference" /> <attr name="cpCancelTextColor" format="color|reference" /> <attr name="cpClearTextIcon" format="reference" /> <attr name="cpSearchTextSize" format="dimension|reference" /> <attr name="cpSearchTextColor" format="color|reference" /> <attr name="cpSearchHintText" format="string|reference" /> <attr name="cpSearchHintTextColor" format="color|reference" /> <attr name="cpSearchCursorDrawable" format="reference" /> <attr name="cpListItemTextSize" format="dimension|reference" /> <attr name="cpListItemTextColor" format="color|reference" /> <attr name="cpListItemHeight" format="dimension|reference"/> <attr name="cpEmptyIcon" format="reference"/> <attr name="cpEmptyIconWidth" format="dimension|reference"/> <attr name="cpEmptyIconHeight" format="dimension|reference"/> <attr name="cpEmptyText" format="string|reference"/> <attr name="cpEmptyTextSize" format="dimension|reference"/> <attr name="cpEmptyTextColor" format="color|reference"/> <attr name="cpGridItemBackground" format="color|reference"/> <attr name="cpGridItemSpace" format="reference"/> <!--懸浮欄--> <attr name="cpSectionHeight" format="reference"/> <attr name="cpSectionTextSize" format="reference" /> <attr name="cpSectionTextColor" format="reference" /> <attr name="cpSectionBackground" format="reference" /> <attr name="cpIndexBarTextSize" format="reference" /> <attr name="cpIndexBarNormalTextColor" format="reference" /> <attr name="cpIndexBarSelectedTextColor" format="reference" /> <!--特寫佈局--> <attr name="cpOverlayWidth" format="dimension|reference"/> <attr name="cpOverlayHeight" format="dimension|reference"/> <attr name="cpOverlayTextSize" format="dimension|reference"/> <attr name="cpOverlayTextColor" format="color|reference"/> <attr name="cpOverlayBackground" format="color|reference"/> </resources>
OK,enjoy it~
Changelog
v2.0.3
- 修復狀態列變黑問題
- 修復字母索引欄被軟遮擋的問題
- 新增取消選擇監聽
- 方法呼叫方式稍微改變
v2.0.2
- 修復定位城市偶爾不重新整理的bug
v2.0.1
City
v2.0.0
- 專案重構優化,結構更清晰
- 使用RecyclerView
連結
https://github.com/zaaach/CityPicker
QQ群
有興趣可以加入QQ群一起交流:601783167