欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

Android CityPicker2.0:类似美团等APP选择城市界面

发表日期:2018-03 文章编辑:小灯 浏览次数:1315

CityPicker

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

主要功能:

  • 字母悬浮栏
  • 指定热门城市
  • 自定义动画效果
  • 自定义主题
  • 名称或拼音搜索
  • 返回城市名、code等数据
  • 提供定位接口,解耦定位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手动导入.

Usage

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

基本使用:

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())//此方法必须调用 .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() { //定位完成之后更新数据 CityPicker.getInstance() .locateComplete(new LocatedCity("深圳", "广东", "101280601"), LocateState.SUCCESS); } }, 2000); } }) .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~

Changelog

v2.0.1

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

v2.0.0

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

Github地址:

https://github.com/zaaach/CityPicker欢迎star或提供建议~


本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:http://www.dengtar.com/13841.html
相关APP开发
    SQL执行错误,请检查