CityPicker送上GitHub地址:https://github.com/zaaach/CityPicker
一、效果图展示
二、导入依赖
app:build.gradle
dependencies {
implementation 'com.zaaach:citypicker:2.0.3' //必选
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
三、基本使用:
步骤1:manifest配置
在manifest.xml
中给使用CityPicker
的activity
添加主题android:theme="@style/DefaultCityPickerTheme"
<activity android:name=".MainActivity" android:theme="@style/DefaultCityPickerTheme">
......
</activity>
步骤2:activity/fragment调用
注意:热门城市使用HotCity
,定位城市使用LocatedCity
//自定义热门城市
List<HotCity> 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"));
......
//城市列表
private void getCity() {
CityPicker.from(Objects.requireNonNull(getActivity()))
//启用动画效果,默认无
.enableAnimation(true)
//自定义动画
.setAnimationStyle(R.style.CustomAnim)
//APP自身已定位的城市,传null会自动定位(默认)
.setLocatedCity(null)
//指定热门城市
.setHotCities(hotCities)
.setOnPickListener(new OnPickListener() {
/**
* 选中某个城市点击事件
* @param position 第几个
* @param data 获取具体的数据
*/
@Override
public void onPick(int position, City data) {
//获取到的地区数据传值给全局使用
get_where.setWhere(data.getName());
//刷新界面
onStart();
//设置地区控件显示地区数据
homeArea.setText(data.getName());
}
/**
* 取消点击事件
*/
@Override
public void onCancel() {
onStart();
}
@Override
public void onLocate() {
//开始定位,这里模拟一下定位
new Handler().postDelayed(() -> CityPicker.from(Objects.requireNonNull(getActivity())).locateComplete(new LocatedCity(get_where.getGPS_where(), "广东", "101280601"), LocateState.SUCCESS), 0);
}
})
.show();
}
四、自定义主题
在style.xml
中自定义主题并且继承DefaultCityPickerTheme
,别忘了在manifest.xml
设置给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~