Android 根据首字母给城市排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37794706/article/details/78779774

最近做的项目里面有用到选城市的功能,城市是根据首字母排序的,数据都是通过后台返回的


这个功能用到了一个第三方的库,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















猜你喜欢

转载自blog.csdn.net/m0_37794706/article/details/78779774
今日推荐