Android 根据首字母给城市排序
来源:互联网 发布:北京汽车美容软件 编辑:程序博客网 时间:2024/05/19 14:51
最近做的项目里面有用到选城市的功能,城市是根据首字母排序的,数据都是通过后台返回的
这个功能用到了一个第三方的库,indexlib,然后列表是用RecycleView实现的,
先给大家看一下效果图:
1.我写的model
public class CityModel implements Serializable { public String cityid; public String cityno; public String initials; public String citycode; public String cityshortcode; public String cityname; public String status; public String country; public String countrycode; public boolean domestic;// true 是国内,false 国际 public String enname;//国际城市 英文名
2.布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorWhite" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorWhite"> </android.support.v7.widget.RecyclerView> <com.mcxtzhang.indexlib.IndexBar.widget.IndexBar android:id="@+id/indexBar" android:layout_width="24dp" android:layout_height="match_parent" android:layout_gravity="right" android:layout_marginBottom="70dp" android:layout_marginTop="70dp" app:indexBarPressBackground="@color/colorWhite" app:indexBarTextSize="10sp" /> <TextView android:id="@+id/tvSideBarHint" android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center" android:background="@drawable/btn_sure_default" android:gravity="center" android:textColor="@android:color/white" android:textSize="48sp" android:visibility="gone" tools:text="A" tools:visibility="visible" /> </FrameLayout></LinearLayout>
2.使用 这里吧 你需要的数据 都加上,然后进行排序
mBodyDatas = new ArrayList<>(); for (int i = 0; i < data.length; i++) { CityListBean cityBean = new CityListBean(); cityBean.setCity(data[i].getCity());//设置城市名称 cityBean.setInitials(data[i].getInitials()); cityBean.setCityShortCode(data[i].getCityShortCode()); mBodyDatas.add(cityBean); } for (int i = 0; i < mBodyDatas.size(); i++) { Log.i("mBodyDatas", "name===" + mBodyDatas.get(i).getCity() + " initials==" + mBodyDatas.get(i).getInitials()); } mAdapter.setDatas(mBodyDatas); mHeaderAdapter.notifyDataSetChanged(); mSourceDatas.addAll(mBodyDatas); mIndexBar.setmSourceDatas(mSourceDatas)//设置数据 .invalidate(); mDecoration.setmDatas(mSourceDatas); }}, 100);
在IndexBarDaaHelperImpl类里面设置首字母
//以下代码设置城市拼音首字母// String tagString = indexPinyinBean.getBaseIndexPinyin().toString().substring(0, 1); String tagString = indexPinyinBean.getInitials();//这个就是后台给我们传的首字母 if (tagString.matches("[A-Z]")) {//如果是A-Z字母开头 indexPinyinBean.setBaseIndexTag(tagString); } else {//特殊字母这里统一用#处理 indexPinyinBean.setBaseIndexTag("#"); } }
项目源码可以到我的gitup上下载:https://github.com/xiao-er/SelectCitys
阅读全文
1 0
- Android 根据首字母给城市排序
- 城市列表-根据拼音首字母排序
- 城市列表-根据拼音首字母排序
- 根据城市首字母进行分类,区分多音字,获取城市首字母
- Android城市列表,首字母排序,右侧点击字母定位
- 根据城市名称的首字母,将城市分类
- Android根据汉字拼音首字母定位和排序分组
- Android开发实现选择城市界面,可根据拼音、首字母进行搜索
- 根据汉字首字母排序 mysql
- mysql 根据汉字首字母排序
- Android开发--全国各城市列表并按首字母排序加快速定位
- Android 汉字首字母排序
- 根据数字的首字母大小排序
- 根据字符串首字母分组并内部排序
- mysql根据汉字首字母排序的方法
- java中实现根据中文首字母排序
- Android按照拼音排序获取联系人并根据拼音首字母获取联系人
- php获取中文字符拼音首字母和城市的排序
- 设计模式(十五)------23种设计模式(7):中介者模式
- 分答项目_知识点_AMD规范及js加载器
- c++单例模式析构函数的调用
- 查看CentOS系统版本和位数的方法
- 关于sql 查询时的 字段拼接
- Android 根据首字母给城市排序
- 震惊心酸!月薪5千就是低薪人士?
- 获取EasyUI的treegrid的checkbox所有已勾选的数据
- solr学习日记···简单介绍
- 织梦-分页样式
- nuxt.js中如何引入jquery和boostrap
- 锁
- sqlldr批量导入数据到Oracle
- 不往初心 方得始终