最近开发一个项目需要用到城市列表,需求如下
在github上搜索了下发现要么有的不便于维护,有的占用空间太大,有的和需求差别很大,一怒之下冲动了,自己写了一个,,,,,,,,,,
最终小半天搞定,发现比想像中的简单多了,,,,,,,
package com.xdx.hostay.views.home.activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.xdx.hostay.R;
import com.xdx.hostay.base.BaseActivity;
import com.xdx.hostay.base.CallBackOne;
import com.xdx.hostay.base.CallBackSix;
import com.xdx.hostay.base.Constant;
import com.xdx.hostay.bean.CityListBean;
import com.xdx.hostay.utils.common.string.PinYinManager;
import com.xdx.hostay.utils.common.string.PinYinUtils;
import com.xdx.hostay.utils.data.http.access.HttpRequest;
import com.xdx.hostay.utils.data.http.modle.BeanRequest;
import com.xdx.hostay.views.fabu.adapter.SimpleStringAdapter2;
import com.xdx.hostay.views.home.adapter.CityChoseAdapter;
import com.xdx.hostay.views.home.adapter.SimpleStringAdapterSearch;
import com.xdx.hostay.views.home.adapter.ZMAdapter;
import com.xdx.hostay.views.home.inter.MyOnclickListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
* Created by biyunlong on 2017/7/24.
* Make progress every day
*/
public class CityChoseActivity extends BaseActivity {
private RelativeLayout reaBack;
private ImageView iv;
private TextView tvTopbar;
private EditText etSearch;
private FrameLayout frmCommon;
private RecyclerView rvCity;
private RecyclerView rvCityZM;
private LinearLayout linSearch;
private RecyclerView rvSearch;
private boolean move = false;
private int mIndex = 0;
private List<CityListBean.CityBean> curCityList = new ArrayList<>();
private List<CityListBean.CityBean> hotCityList = new ArrayList<>();
private List<CityListBean.CityBean> searchCityList = new ArrayList<>();
private ArrayList<String> zmList = new ArrayList<>();
private CityChoseAdapter adapter;
private ZMAdapter adapter2;
private SimpleStringAdapterSearch adapter3;
private String city="";
private TextWatcher watcher=new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
search(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
if(s.length()>0){
linSearch.setVisibility(View.VISIBLE);
frmCommon.setVisibility(View.GONE);
}else{
linSearch.setVisibility(View.GONE);
frmCommon.setVisibility(View.VISIBLE);
}
}
};
public void search(String s){
searchCityList.clear();
for(int i=0;i<curCityList.size();i++){
String name=curCityList.get(i).getName();
String head=PinYinUtils.getPingYin(name);
String head2=PinYinUtils.getPinYinHeadChar(name);
if(name.contains(s)&&name.length()>1){
searchCityList.add(curCityList.get(i));
}else if(head2.contains(s)&&head2.length()>1){
if(s.equals("s")){
String head3=head2.substring(0,1);
if(head3.contains(s)&&head3.length()>1){
searchCityList.add(curCityList.get(i));
}
}else{
searchCityList.add(curCityList.get(i));
}
}
}
adapter3.notifyDataSetChanged();
}
@Override
public void callback(int type) {
}
@Override
public void callback(int type, String msg) {
}
@Override
public void setContent() {
setContentView(R.layout.citychose_activity);
}
@Override
public void findView() {
reaBack = (RelativeLayout) findViewById(R.id.rea_back);
iv = (ImageView) findViewById(R.id.iv);
tvTopbar = (TextView) findViewById(R.id.tv_topbar);
etSearch = (EditText) findViewById(R.id.et_search);
frmCommon = (FrameLayout) findViewById(R.id.frm_common);
rvCity = (RecyclerView) findViewById(R.id.rv_city);
rvCityZM = (RecyclerView) findViewById(R.id.rv_cityZM);
linSearch = (LinearLayout) findViewById(R.id.lin_search);
rvSearch = (RecyclerView) findViewById(R.id.rv_search);
}
@Override
public void setValue() {
city=getIntent().getStringExtra("city");
tvTopbar.setText("选择城市");
reaBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
rvCity.setLayoutManager(new LinearLayoutManager(context));
adapter = new CityChoseAdapter(new CallBackSix() {
@Override
public void callBack(int key, String value) {
Intent intent=new Intent();
intent.putExtra("city",value);
intent.putExtra("id",key);
setResult(Constant.STATUS3,intent);
finish();
}
});
rvCity.setAdapter(adapter);
rvCity.setOnScrollListener(new RecyclerViewListener());
getCityList();
rvCityZM.setLayoutManager(new LinearLayoutManager(context));
adapter2 = new ZMAdapter(new MyOnclickListener() {
@Override
public void onClick(String zm) {
moveToPosition(adapter.getPosition(zm));
}
});
rvCityZM.setAdapter(adapter2);
etSearch.addTextChangedListener(watcher);
adapter3=new SimpleStringAdapterSearch(searchCityList, new CallBackOne() {
@Override
public void callback(String str, int type) {
Intent intent=new Intent();
intent.putExtra("city",str);
intent.putExtra("id",type);
setResult(Constant.STATUS3,intent);
finish();
}
@Override
public void callback(String str, int type, int position) {
}
});
rvSearch.setLayoutManager(new LinearLayoutManager(context));
rvSearch.setAdapter(adapter3);
}
public void getCityList() {
// /hostel/cityList
HttpRequest.postRequest(context, null, "/hostel/cityList", new BeanRequest<CityListBean>() {
@Override
protected void onSucess(CityListBean cityListBean, int status, String msg) {
if (status == 1) {
if (cityListBean != null) {
if (cityListBean.getCity() != null && cityListBean.getCity().size() > 0) {
curCityList.clear();
curCityList.addAll(PinYinManager.getInstance().getPinList(cityListBean.getCity()));
hotCityList.clear();
hotCityList.addAll(cityListBean.getHot());
adapter.setData(curCityList,hotCityList);
getZM();
adapter2.setData(zmList);
}
}
}
}
@Override
protected void onFaild(Exception e) {
}
});
}
private void moveToPosition(int n) {
LinearLayoutManager mLinearLayoutManager = (LinearLayoutManager) rvCity.getLayoutManager();
//先从RecyclerView的LayoutManager中获取第一项和最后一项的Position
int firstItem = mLinearLayoutManager.findFirstVisibleItemPosition();
int lastItem = mLinearLayoutManager.findLastVisibleItemPosition();
//然后区分情况
if (n <= firstItem) {
//当要置顶的项在当前显示的第一个项的前面时
rvCity.scrollToPosition(n);
} else if (n <= lastItem) {
//当要置顶的项已经在屏幕上显示时
int top = rvCity.getChildAt(n - firstItem).getTop();
rvCity.scrollBy(0, top);
} else {
//当要置顶的项在当前显示的最后一项的后面时
rvCity.scrollToPosition(n);
//这里这个变量是用在RecyclerView滚动监听里面的
move = true;
}
}
class RecyclerViewListener extends RecyclerView.OnScrollListener {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//在这里进行第二次滚动(最后的100米!)
LinearLayoutManager mLinearLayoutManager = (LinearLayoutManager) rvCity.getLayoutManager();
if (move) {
move = false;
//获取要置顶的项在当前屏幕的位置,mIndex是记录的要置顶项在RecyclerView中的位置
int n = mIndex - mLinearLayoutManager.findFirstVisibleItemPosition();
if (0 <= n && n < rvCity.getChildCount()) {
//获取要置顶的项顶部离RecyclerView顶部的距离
int top = rvCity.getChildAt(n).getTop();
//最后的移动
rvCity.scrollBy(0, top);
}
}
}
}
public void getZM() {
zmList.clear();
HashSet set = new HashSet();
for (int i = 0; i < curCityList.size(); i++) {
String str = PinYinUtils.getPinYinHeadChar(curCityList.get(i).getName()).substring(0, 1);
if (!set.contains(str)) {
set.add(str);
zmList.add(str.toUpperCase());
}
}
}
}
用到的工具类有pinyin4j-2.5
public class PinYinManager {
private static PinYinManager manager;
public static PinYinManager getInstance() {
if (manager == null) {
manager = new PinYinManager();
}
return manager;
}
public List<CityListBean.CityBean> getPinList(List<CityListBean.CityBean> list) {
PinyinComparator1 comparator1=new PinyinComparator1();
Collections.sort(list,comparator1);
return list;
}
}
public class PinyinComparator1 implements Comparator<CityListBean.CityBean> {
@Override
public int compare(CityListBean.CityBean o1, CityListBean.CityBean o2) {
String s1 = PinYinUtils.getPingYin(o1.getName());
String s2 = PinYinUtils.getPingYin(o2.getName());
int flag = s1.compareTo(s2);
return flag;
}
}
adapter 类public class ZMAdapter extends RecyclerView.Adapter<ZMAdapter.ZMViewholder>{
private Context context;
private MyOnclickListener listener;
private ArrayList<String> list=new ArrayList();
public ZMAdapter (MyOnclickListener listener){
this.listener=listener;
}
public void setData(ArrayList<String> list){
this.list=list;
notifyDataSetChanged();
}
@Override
public ZMViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
context=parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.zm_item,parent,false);
return new ZMViewholder(view);
}
@Override
public void onBindViewHolder(ZMViewholder holder, final int position) {
holder.tvZm.setText(list.get(position));
holder.tvZm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(list.get(position).toLowerCase());
}
});
}
@Override
public int getItemCount() {
return list!=null?list.size():0;
}
public class ZMViewholder extends RecyclerView.ViewHolder{
private TextView tvZm;
public ZMViewholder(View itemView) {
super(itemView);
tvZm = (TextView) itemView.findViewById(R.id.tv_zm);
}
}
}