感觉比较好的一个地址选择设计,而且发现有的App中也用到了。还是先上效果图
思路:
1.效果是仿照网上大神实现的类似通讯录样式做的;
2.右边a-z是自定义的一个bar,设置了点击监听事件,以及对话框d出
3.关键是adapter,判断了字母显示和隐藏
4.用到汉字转拼音、按首字母排序等工具类
5.3个activity的跳转是用回调来实现,每个activity都实现了回调,这样就有了从区activity直接跳转到首页的效果
6.数据是调用的我本地的接口实现的,如果大家没有数据我可以想办法给你们提供测试的省市区数据接口。加载数据是用volley框架实现的
代码的一个结构
1.右侧自定义bar的部分代码
首先重写onDraw方法
/** * 重写 * @param canvas */ @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); int height=getHeight();//获取对应的高度 int wIDth=getWIDth();//获取对应的宽度 int singleHeight=height/b.length;//获取每一个字母的高度 for(int i=0;i<b.length;i++){ paint.setcolor(color.rgb(33,65,98)); paint.setTypeface(Typeface.DEFAulT_BolD); paint.setAntiAlias(true); paint.setTextSize(20); //选中 if(i==choose) { paint.setcolor(color.parsecolor("#3399ff"));//设置选中状态颜色 paint.setFakeBoldText(true); } //x坐标等于中间-字符串宽度的一办(????????) float xPos=wIDth/2-paint.measureText(b[i])/2; float yPos=singleHeight*i+singleHeight; canvas.drawText(b[i],xPos,yPos,paint); paint.reset();//重置画笔 } }
重写dispatchtouchEvent方法
/** * 重写 * @param event * @return */ @TargetAPI(Build.VERSION_CODES.JELLY_BEAN) @OverrIDe public boolean dispatchtouchEvent(MotionEvent event) { int action=event.getAction(); float y=event.getY();//点击Y坐标 int oldChoose=choose; OntouchingLetterChangedListener Listener=ontouchingLetterChangedListener; int c=(int)(y/getHeight()*b.length);//点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数 switch (action){ case MotionEvent.ACTION_UP: setBackground(new colorDrawable(0*00000000)); choose=-1;// invalIDate(); if(mTextDialog!=null) { mTextDialog.setVisibility(VIEw.INVISIBLE); } break; default: setBackgroundResource(R.drawable.sIDebar_background); if(oldChoose!=c) { if(c>=0 && c<b.length) { if(Listener!=null) { Listener.ontouchingLetterChanged(b[c]); } if(mTextDialog!=null) { mTextDialog.setText(b[c]); mTextDialog.setVisibility(VIEw.VISIBLE); } choose=c; invalIDate(); } } break; } return true; }
向外开发接口
/** * 向外公开的方法 * @param ontouchingLetterChangedListener */ public voID setontouchingLetterChangedListener(OntouchingLetterChangedListener ontouchingLetterChangedListener){ this.ontouchingLetterChangedListener=ontouchingLetterChangedListener; }
2.adapter关键代码,以province的adapter为例,继承自SectionIndexer
/** * 根据ListVIEw的当前位置获取匪类的首字母的Char ascii值 * @param position * @return */ public int getSectionForposition(int position){ return List.get(position).getSortLetters().charat(0); } /** * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置 * @param section * @return */ public int getpositionForSection(int section){ for(int i=0;i<getCount();i++){ String sortStr=List.get(i).getSortLetters(); char firstChar=sortStr.toupperCase().charat(0); if(firstChar==section) { return i; } } return -1; }
然后getVIEw里面判断显示效果,是否显示字母,在哪里显示字母
@OverrIDe public VIEw getVIEw(final int i,VIEw vIEw,VIEwGroup vIEwGroup) { VIEwHolder holder=null; final Province province=List.get(i); if(vIEw==null) { holder=new VIEwHolder(); vIEw=LayoutInflater.from(mContext).inflate(R.layout.item,null); holder.tvLetter= (TextVIEw) vIEw.findVIEwByID(R.ID.catalog); holder.tvTitle= (TextVIEw) vIEw.findVIEwByID(R.ID.Title); vIEw.setTag(holder); } else { holder= (VIEwHolder) vIEw.getTag(); } //根据position获取分类的首字母的char ascii值 int section=getSectionForposition(i); //如果当前位置等于该分类首字母的Char的位置,则认为是第一次出现 if(i==getpositionForSection(section)) { holder.tvLetter.setVisibility(VIEw.VISIBLE); holder.tvLetter.setText(province.getSortLetters()); } else { holder.tvLetter.setVisibility(VIEw.GONE); } holder.tvTitle.setText(this.List.get(i).getProvince@R_419_6889@()); return vIEw; }
3.再贴一个provinceActivity的类
public class ProvinceActivity extends Activity { private Context mContext; private ListVIEw sortListVIEw; private SIDebar sIDebar; private TextVIEw dialog; private ProvinceAdapter adapter; /** * 汉字转换成拼音的类 */ private CharacterParser characterParser; private List<Province> sourceDateList; /** * 根据拼音来排列ListVIEw里面的数据类 */ private PinyinComparator pinyinComparator; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.a_province); mContext=this; initVIEw(); } private voID initVIEw() { //实例化汉字转拼音类 characterParser=CharacterParser.getInstance(); pinyinComparator=new PinyinComparator(); sIDebar= (SIDebar) findVIEwByID(R.ID.sIDrbar); dialog= (TextVIEw) findVIEwByID(R.ID.dialog); sIDebar.setTextVIEw(dialog); //设置右侧触摸监听 sIDebar.setontouchingLetterChangedListener(new SIDebar.OntouchingLetterChangedListener() { @OverrIDe public voID ontouchingLetterChanged(String s) { //该字母首次出现的位置 int position=adapter.getpositionForSection(s.charat(0)); if(position!=-1) { sortListVIEw.setSelection(position); } } }); sortListVIEw= (ListVIEw) findVIEwByID(R.ID.lv_pro); sortListVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> adapterVIEw,int i,long l) { Intent intent=new Intent(); intent.putExtra("provinceID",((Province)adapter.getItem(i)).getID()); intent.putExtra("province@R_419_6889@",((Province)adapter.getItem(i)).getProvince@R_419_6889@()); intent.setClass(mContext,CityActivity.class); startActivityForResult(intent,0); } }); //获取数据 volley_get(); } @OverrIDe protected voID onActivityResult(int requestCode,int resultCode,Intent data) { if(requestCode==0) { if(resultCode==1) { setResult(1,data); finish(); } } super.onActivityResult(requestCode,resultCode,data); } /** * Volley加载数据 */ private voID volley_get(){ RequestQueue mQueue=Volley.newRequestQueue(mContext); JsonObjectRequest JsonObjectRequest=new JsonObjectRequest("http://10.0.0.103:8080/StoAppPro/GetProvince",null,new Response.Listener<JsONObject>() { @OverrIDe public voID onResponse(JsONObject JsonObject) { //Gson解析,直接将JsonObject的data值转换成List Gson gson=new Gson(); Type ListType=new Typetoken<List<Province>>(){}.getType(); try { List<Province> List=gson.fromJson(JsonObject.get("data").toString(),ListType); sourceDateList=filledData(List); Log.e("wj",sourceDateList.get(0).getID() + ""); //根据a-z进行排序源数据 Collections.sort(sourceDateList,pinyinComparator); //初始化适配器 adapter=new ProvinceAdapter(mContext,sourceDateList); //绑定适配器 sortListVIEw.setAdapter(adapter); } catch (JsONException e) { e.printstacktrace(); } } },new Response.ErrorListener() { @OverrIDe public voID onErrorResponse(VolleyError volleyError) { } }); mQueue.add(JsonObjectRequest); } /** * 为ListVIEw填充数据 * @param * @return */ private List<Province> filledData(List<Province> List){ List<Province> mSortList = new ArrayList<Province>(); for(int i=0; i<List.size(); i++){ Province province = new Province(); province.setProvince@R_419_6889@(List.get(i).getProvince@R_419_6889@()); province.setID(List.get(i).getID()); //汉字转换成拼音 String pinyin = characterParser.getSelling(List.get(i).getProvince@R_419_6889@()); String sortString = pinyin.substring(0,1).toupperCase();//获取拼音首字母 // 正则表达式,判断首字母是否是@R_404_6371@ if(sortString.matches("[A-Z]")){ province.setSortLetters(sortString.toupperCase()); }else{ province.setSortLetters("#"); } mSortList.add(province); } return mSortList; }}
ok,粘贴了部分代码,而且很多关键地方我也在代码中加了注释。还是那句话,自己动手实现一把才能在今后用到的时候方便使用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android仿手机通讯录地址选择功能全部内容,希望文章能够帮你解决Android仿手机通讯录地址选择功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)