项目中采用了三种样式城市选择(无语....),第一种是:

地址:https://github.com/yanxing/android-util   sortlistviewlibrary

第二种是:

第三种是:

我用两个水平的ListView实现的(可以用ListFragment,也可以两个Fragment),布局代码:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ListViewandroid:id="@+id/province"android:layout_width="match_parent"android:layout_height="match_parent"android:cacheColorHint="@android:color/transparent"android:divider="@color/line_xx"android:dividerHeight="1px"android:layout_weight="2"android:choiceMode="singleChoice"android:scrollbars="none"/><ListViewandroid:id="@+id/city"android:layout_width="match_parent"android:layout_height="match_parent"android:cacheColorHint="@android:color/transparent"android:divider="@color/line_xx"android:dividerHeight="1px"android:layout_weight="1"android:scrollbars="none"/></LinearLayout>

ListView需要单选模式

android:choiceMode="singleChoice"

Activity代码:

    @AfterViews@Overrideprotected void afterInstanceView() {//构造当前地区Area currentArea=new Area();currentArea.setName("当前地区");ArrayList<Area.CityBean> currentCity=new ArrayList<Area.CityBean>();Area.CityBean cityBean=new Area.CityBean();String currentCityStr=getIntent().getStringExtra("currentCity");cityBean.setName(currentCityStr);currentCity.add(cityBean);currentArea.setCity(currentCity);mAreaList.add(currentArea);mAreaList.addAll(ParseJson.getArea(getApplicationContext()));mProvinceAdapter=new ProvinceAdapter();mProvince.setAdapter(mProvinceAdapter);mProvince.setOnItemClickListener(this);new Handler().postDelayed(() -> {//选中第一项mProvince.performItemClick(mProvince.getChildAt(0),0,mProvince.getItemIdAtPosition(0));},700);}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {mIsClick=true;mIndex=position;for (int i=0;i<mProvince.getChildCount();i++){if (mIndex!=i){View view1=mProvince.getChildAt(i);view1.findViewById(R.id.current).setVisibility(View.GONE);}}view.findViewById(R.id.current).setVisibility(View.VISIBLE);if (mCityAdapter==null){mCityAdapter=new CommonAdapter<Area.CityBean>(mAreaList.get(0).getCity(),R.layout.adapter_city) {@Overridepublic void onBindViewHolder(ViewHolder holder, int index) {if (mIndex==0){holder.findViewById(R.id.current).setVisibility(View.VISIBLE);}else {holder.findViewById(R.id.current).setVisibility(View.GONE);}holder.setText(R.id.city,mAreaList.get(mIndex).getCity().get(index).getName());}};mCity.setAdapter(mCityAdapter);}else {mCityAdapter.update(mAreaList.get(mIndex).getCity());}}

ListView子View布局背景选择器代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@color/white"/><item android:state_selected="true" android:drawable="@color/white"/><item android:state_activated="true" android:drawable="@color/white"/><item android:drawable="@color/list_line"/>
</selector>

一开始我没有采用遍历的方式,而是子View中的ImageView( )用选择器,代码如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:drawable="@mipmap/current"/><item android:state_selected="true" android:drawable="@mipmap/current"/><item android:state_activated="true" android:drawable="@mipmap/current"/>
</selector>

但是发现两个bug,一是:ListView选中ItemClick其他的子View中的 没有GONE。二是上、下拉ListView选中的Item从可见-》不可见-》可见, 就不可见了。最后放弃了这种方式,采用遍历的方式。

完整代码地址:SelectCityActivity

更新:今天发现上面的思路太费事了,需要遍历listview子布局,而且view没有复用。修改为在Area类添加一个布尔型的check变量,标记当前item是否被选中,然后点击时调用notifyDataSetChanged方法就行了,代码已更新。

android 城市列表相关推荐

  1. Android 城市列表选择

    最近开发一个城市选择的功能,找了这个文章 Android 城市列表选择 结果发现它使用的拼音转换库pinyin4j有一个很严重的问题,竟然长沙chang sha识别为zhang sha,结果长沙北分在 ...

  2. Android城市列表,首字母排序,右侧点击字母定位

    效果图: 自动定位(百度地图)+列表选择 思路: 1.请求到的城市列表,提取首字母 2.城市列表比较混乱,先使用实体类把相同首字母的城市放到一起 3.显示城市列表 4.使用QuicLocationBa ...

  3. android 城市列表数据,用RecyclerView写的城市列表

    分享一下城市列表的网格布局样式的demo,代码里面包括网格布局和竖直列表布局两种样式. 网格样式: image.png 竖直列表样式: image.png 数据来源,本地的citylist.json城 ...

  4. Android城市列表

    大家好,分享一个热门城市列表的Demo,里面显示了热门的城市,原作者是Kayone,在他的基础上加上了全中国城市的搜索功能和拼音搜索功能,但是现在只能完整的搜索.模糊搜索还没完成. 下载地址:http ...

  5. Android自定义滚动条——城市列表

    Android自定义滚动条--城市列表 效果视频 绘制滚动条 区别选中与未选择文字 绘制等高间距 滑动事件监听 解析承载城市数据的XML文件 下载XML文件 解析文件 适配器 建立适配器类 适配器子项 ...

  6. android百度地图定位、城市列表选择搜索

    百度地图的集成在百度地图的开发文档中有详细的介绍:Android定位SDK  | 百度地图API SDK 本文介绍的主要功能有: kotlin语言搭建的项目 百度地图的定位 选择城市(包括省市县三级搜 ...

  7. 仿美团实现地域选择和城市列表

    介绍 在开发O2O相关应用的时候,肯定会有定位,选择所在城市,选择地域,然后再向服务器请求该地区的相关数据,这时就需要我们提供一个导向让用户选择所在区域. 看来看去,最终还是选择模仿美团,感觉用户体验 ...

  8. 城市列表-根据拼音首字母排序

    今天我们就简单的实现一下城市的排序 读取我们城市的信息并通过listview展示 首先看一下我们的布局文件 <LinearLayout xmlns:android="http://sc ...

  9. 安卓开发选取城市列表

    public class Activity01 extends Activity implements OnScrollListener { private DatabaseHelper helper ...

最新文章

  1. 关于pytorch--embedding的问题
  2. 【神经网络】(2) 网络优化,案例:服装图像分类,附python完整代码
  3. MATLAB_edge()
  4. 面试官:不会看 Explain执行计划,简历敢写 SQL 优化?
  5. 详解Pattern类和Matcher类
  6. Android targetSdkVersion 原理
  7. python没有英文基础能学吗-学python需要英语基础吗
  8. html请求接口_python接口自动化测试 - 2.Django开发接口
  9. Vue + Element UI——监听DOM元素高度和宽度解决方案整理(八种方法)
  10. 测试jdbc连mysql数据库_java连接mysql数据库及测试是否连接成功的方法
  11. 避坑!使用 Kubernetes 最易犯的 10 个错误
  12. Node.js学习准备篇
  13. 是什么让你开始劝退的?(@韩冬)
  14. 矩阵键盘数 码管显示多位数 c语言,4×4矩阵键盘数码管显示按键值程序
  15. win10安装pytorch简易教程
  16. 联想服务器改win7系统教程,联想台式机10代cpu改win7系统详细教程
  17. 用PLSQL查询Oracle数据库某字段的本年数,上年同期数,同比
  18. 在服务器上下载安装anaconda
  19. 学习STM32的理由
  20. w7设置双显示器_Win7系统双屏显示设置的方法

热门文章

  1. Android 语音播报之项目实战
  2. 网络技术摘抄(入门需知)
  3. jsf中应用javascript
  4. win服务器的远程连接--如何打开远程连接服务器?如何设置远程连接?连接工具?
  5. 视频监控系统流媒体服务器的用处,监控视频流媒体服务器作用
  6. Servlet-01
  7. yii2框架教程 入门篇(一)
  8. WRF后处理/Python处理nc数据与可视化/极坐标网格绘制(Cartopy、netcdf4)——以北极雪水当量数据为例
  9. spreadjs~~一个Excel在线编辑的工具
  10. 如何用HTML5开发一款手机游戏