CityPicker

repository·master·Indexed 25 days ago

https://github.com/zaaach/citypicker

An Android library providing a city selection interface with an alphabetical index bar, search functionality, and customizable themes. It supports hot cities, location updates via LocatedCity, and event handling through OnPickListener.

Tokens
2.2K
Snippets
5
Records
5
Agent score
35%

What's inside CityPicker

  1. Install CityPicker via JitPack

    master

    To use CityPicker in your Android project, follow these two steps:

    1. Add the JitPack repository to your project's root build.gradle file:

    2. Add the CityPicker dependency to your app's build.gradle file. Replace x.y.z with the latest version available on JitPack.

    // 1. Project root build.gradle
    allprojects {
    	repositories {
    		...
    		maven { url 'https://jitpack.io' }
    	}
    }
    
    // 2. App build.gradle
    dependencies {
    	 implementation 'com.github.zaaach:CityPicker:x.y.z'
    }
  2. Customize CityPicker Theme

    master

    To customize the appearance, create a new style in styles.xml that inherits from DefaultCityPickerTheme. Then, apply this custom theme to your Activity in AndroidManifest.xml.

    <!-- styles.xml -->
    <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>
  3. Configure CityPicker Theme in AndroidManifest

    master

    CityPicker is implemented using DialogFragment. You must apply the DefaultCityPickerTheme to the Activity that hosts the picker in your AndroidManifest.xml.

    <activity android:name=".MainActivity" android:theme="@style/DefaultCityPickerTheme">
      ......
    </activity>
  4. Use CityPicker to implement city selection

    master

    Use CityPicker.from(activity) to initialize the picker. You can configure hot cities using HotCity, set the currently located city using LocatedCity, and handle selection or location events via OnPickListener.

    Note: To handle location updates, use CityPicker.getInstance().locateComplete(LocatedCity, LocateState) within your location callback.

    List<HotCity> hotCities = new ArrayList<>();
    hotCities.add(new HotCity("北京", "北京", "101010100"));
    hotCities.add(new HotCity("上海", "上海", "101020100"));
    
    CityPicker.from(activity)
      .enableAnimation(true)
      .setAnimationStyle(anim)
      .setLocatedCity(new LocatedCity("杭州", "浙江", "101210101"))
      .setHotCities(hotCities)
      .setOnPickListener(new OnPickListener() {
        @Override
        public void onPick(int position, City data) {
          // data contains name, code, etc.
          Toast.makeText(getApplicationContext(), data.getName(), Toast.LENGTH_SHORT).show();
        }
          
        @Override
        public void onCancel(){
          Toast.makeText(getApplicationContext(), "取消选择", Toast.LENGTH_SHORT).show();     
        }
        
        @Override
        public void onLocate() {
          // Implement your own location logic here
          // Once location is successful, update the picker:
          CityPicker.getInstance()
            .locateComplete(new LocatedCity("深圳", "广东", "101280601"), LocateState.SUCCESS);
        }
      })
      .show();
  5. Reference: CityPicker Custom Theme Attributes

    master

    The following attributes are available for customizing the DefaultCityPickerTheme. Some values require a reference type.

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