Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zaaach/CityPicker

:fire::fire::fire:城市选择、定位、搜索及右侧字母导航,类似美团 百度糯米 饿了么等APP选择城市功能
https://github.com/zaaach/CityPicker

city indexbar location recyclerview

Last synced: 3 months ago
JSON representation

:fire::fire::fire:城市选择、定位、搜索及右侧字母导航,类似美团 百度糯米 饿了么等APP选择城市功能

Awesome Lists containing this project

README

        



# CityPicker

[![Platform](https://img.shields.io/badge/platform-android-green.svg)](http://developer.android.com/index.html) [![API](https://img.shields.io/badge/API-14%2B-yellow.svg?style=flat)](https://android-arsenal.com/api?level=14)

现在使用较多的类似美团、外卖等APP的城市选择界面,**一行代码搞定**,就是这么简单粗暴!!!

**Tips:**(旧版本1.x)会报高德定位jar包冲突,推荐使用2.x版本。

#### 主要功能:

- 字母悬浮栏
- 指定热门城市
- 自定义动画效果
- 自定义主题
- 名称或拼音搜索
- 返回城市名、code等数据
- 提供定位接口,解耦定位SDK

# Preview

![image](https://github.com/zaaach/CityPicker/raw/master/art/screen.gif) ![image](https://github.com/zaaach/CityPicker/raw/master/art/screen1.gif)
![image](https://github.com/zaaach/CityPicker/raw/master/art/screen2.gif)

# APK

下载[demo.apk](https://github.com/zaaach/CityPicker/raw/master/art/demo.apk)体验.

# Download

**1:** 项目根目录的build.gradle添加如下配置:

```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

**2:** app添加依赖:

```groovy
dependencies {
implementation 'com.github.zaaach:CityPicker:x.y.z'
}
```

记得把`x.y.z`替换为[![jitpack](https://jitpack.io/v/zaaach/CityPicker.svg)](https://jitpack.io/#zaaach/CityPicker)中的数字

# Usage

`CityPicker` 基于`DialogFragment` 实现,已提供定位接口,需要APP自身实现定位。

### 基本使用:

#### Step1:

在`manifest.xml`中给使用`CityPicker` 的`activity`添加主题`android:theme="@style/DefaultCityPickerTheme"`

```xml

......

```

#### Step2:

注意:热门城市使用`HotCity` ,定位城市使用`LocatedCity`

```java
List 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`。

```xml

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

```

`CityPicker` 中自定义的所有属性如下,有些属性值必须是引用类型`refrence`,使用时注意。

```xml




























```

OK,enjoy it~

# Changelog

#### v2.1.0

- 迁移至AndroidX

#### v2.0.3

- 修复状态栏变黑问题
- 修复字母索引栏被软遮挡的问题
- 新增取消选择监听
- 方法调用方式稍微改变

#### v2.0.2

- 修复定位城市偶尔不刷新的bug

#### v2.0.1

- 新增定位接口
- 修改返回类型为`City` ,可获取城市名、code等数据

#### v2.0.0

- 项目重构优化,结构更清晰
- 使用RecyclerView

# 感谢

[ImmersionBar](https://github.com/gyf-dev/ImmersionBar)

# About me

掘金:[ https://juejin.im/user/56f3dfe8efa6310055ac719f ](https://juejin.im/user/56f3dfe8efa6310055ac719f)

简书:[ https://www.jianshu.com/u/913a8bb93d12 ](https://www.jianshu.com/u/913a8bb93d12)

淘宝店:[ LEON家居生活馆 (动漫摆件)]( https://shop238932691.taobao.com)

![LEON](https://raw.githubusercontent.com/zaaach/imgbed/master/arts/leon_shop_qrcode.png)

:wink:淘宝店求个关注:wink:
# QQ群

有兴趣可以加入QQ群一起交流:
![扫码入群601783167](https://raw.githubusercontent.com/zaaach/imgbed/master/arts/qqgroup.jpg)