CityPicker
現(xiàn)在使用較多的類似美團昏翰、外賣等APP的城市選擇界面,一行代碼搞定朋魔,就是這么簡單粗暴!G洳佟警检!
主要功能:
- 字母懸浮欄
- 指定熱門城市
- 自定義動畫效果
- 自定義主題
- 名稱或拼音搜索
- 返回城市名、code等數(shù)據(jù)
- 提供定位接口害淤,解耦定位SDK
Preview
定位接口
默認主題
搜索功能
自定義主題
APK
下載demo.apk體驗.
Install
Gradle:
implementation 'com.zaaach:citypicker:2.0.1'
or Maven:
<dependency>
<groupId>com.zaaach</groupId>
<artifactId>citypicker</artifactId>
<version>2.0.1</version>
<type>pom</type>
</dependency>
or 下載library手動導(dǎo)入.
Usage
CityPicker
基于DialogFragment
實現(xiàn)解滓,已提供定位接口,需要APP自身實現(xiàn)定位筝家。
基本使用:
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"));
hotCities.add(new HotCity("上海", "上海", "101020100"));
hotCities.add(new HotCity("廣州", "廣東", "101280101"));
hotCities.add(new HotCity("深圳", "廣東", "101280601"));
hotCities.add(new HotCity("杭州", "浙江", "101210101"));
......
CityPicker.getInstance()
.setFragmentManager(getSupportFragmentManager()) //此方法必須調(diào)用
.enableAnimation(enable) //啟用動畫效果
.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 onLocate() {
//開始定位,這里模擬一下定位
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//定位完成之后更新數(shù)據(jù)
CityPicker.getInstance()
.locateComplete(new LocatedCity("深圳", "廣東", "101280601"), LocateState.SUCCESS);
}
}, 2000);
}
})
.show();
關(guān)于自定義主題:
在style.xml
中自定義主題并且繼承DefaultCityPickerTheme
值骇,別忘了在manifest.xml
設(shè)置給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.1
- 新增定位接口
- 修改返回類型為City 使碾,可獲取城市名蜜徽、code等數(shù)據(jù)
v2.0.0
- 項目重構(gòu)優(yōu)化,結(jié)構(gòu)更清晰
- 使用RecyclerView
Github地址:
https://github.com/zaaach/CityPicker歡迎star或提供建議~