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

CityPicker

Platform
API

如今使用較多的相似美團、外賣等APP的城市選擇界面,一行代碼搞定,就是這麼簡單粗暴!!!html

主要功能:

  • 字母懸浮欄
  • 指定熱門城市
  • 自定義動畫效果
  • 自定義主題
  • 名稱或拼音搜索
  • 返回城市名、code等數據
  • 提供定位接口,解耦定位SDK

Preview

image
image
image
image

APK

下載demo.apk體驗.android

Install

Gradle:git

implementation 'com.zaaach:citypicker:2.0.1'
複製代碼

or Maven:github

<dependency>
  <groupId>com.zaaach</groupId>
  <artifactId>citypicker</artifactId>
  <version>2.0.1</version>
  <type>pom</type>
</dependency>
複製代碼

or 下載library手動導入.api

Usage

CityPicker 基於DialogFragment 實現,已提供定位接口,須要APP自身實現定位。bash

基本使用:

Step1:

manifest.xml中給使用CityPickeractivity添加主題android:theme="@style/DefaultCityPickerTheme"ide

<activity android:name=".MainActivity" android:theme="@style/DefaultCityPickerTheme">
  ......
</activity>
複製代碼

Step2:

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())	//此方法必須調用
  .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 onLocate() {
      //開始定位,這裏模擬一下定位
      new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
          //定位完成以後更新數據
          CityPicker.getInstance()
            .locateComplete(new LocatedCity("深圳", "廣東", "101280601"), LocateState.SUCCESS);
        }
      }, 2000);
    }
  })
  .show();
複製代碼

關於自定義主題:

style.xml 中自定義主題而且繼承DefaultCityPickerTheme ,別忘了在manifest.xml 設置給activitysvg

<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~post

Changelog

v2.0.1

  • 新增定位接口
  • 修改返回類型爲City ,可獲取城市名、code等數據

v2.0.0

  • 項目重構優化,結構更清晰
  • 使用RecyclerView

Github地址:CityPicker 歡迎你們star或者提供建議~

相關文章
相關標籤/搜索