Android CityPicker2.0:類似美團等APP選擇城市界面

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中給使用CityPickeractivity添加主題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或提供建議~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末票摇,一起剝皮案震驚了整個濱河市拘鞋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌矢门,老刑警劉巖盆色,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異祟剔,居然都是意外死亡隔躲,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門物延,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宣旱,“玉大人,你說我怎么就攤上這事叛薯∠炀椋” “怎么了驾霜?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長买置。 經(jīng)常有香客問我粪糙,道長,這世上最難降的妖魔是什么忿项? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任蓉冈,我火速辦了婚禮,結(jié)果婚禮上轩触,老公的妹妹穿的比我還像新娘寞酿。我一直安慰自己,他們只是感情好脱柱,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布伐弹。 她就那樣靜靜地躺著,像睡著了一般榨为。 火紅的嫁衣襯著肌膚如雪惨好。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天随闺,我揣著相機與錄音日川,去河邊找鬼。 笑死矩乐,一個胖子當著我的面吹牛龄句,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播散罕,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼分歇,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了欧漱?” 一聲冷哼從身側(cè)響起卿樱,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎硫椰,沒想到半個月后繁调,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡靶草,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年蹄胰,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奕翔。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡裕寨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情宾袜,我是刑警寧澤捻艳,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站庆猫,受9級特大地震影響认轨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜月培,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一嘁字、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧杉畜,春花似錦纪蜒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至灭袁,卻和暖如春猬错,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背简卧。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留烤芦,地道東北人举娩。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像构罗,于是被迫代替她去往敵國和親铜涉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,116評論 25 707
  • afinalAfinal是一個android的ioc遂唧,orm框架 https://github.com/yangf...
    passiontim閱讀 15,429評論 2 45
  • 1芙代、概念 進程和線程都是一個時間段的描述,是CPU工作時間段的描述盖彭。兩者顆粒度不同纹烹。進程是CPU資源分配的最小單位...
    丶序曲閱讀 518評論 0 1