您的位置:首页 > 移动开发 > Android开发

Android自定义控件,实现iphone的picker效果

2011-07-04 13:14 746 查看
效果图如下:

1.不带有图标的





2.带有图标的





主要有两个类:

1.Picker

2.CustomListView

Picker控件的外部接口:





Picker控件的使用方法:

publicclassMainextendsActivity{ /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); finalPickerpicker=newPicker(this); ArrayList<String>data=newArrayList<String>(); ArrayList<Drawable>icons=newArrayList<Drawable>(); icons.add(getResources().getDrawable(R.drawable.icon1)); icons.add(getResources().getDrawable(R.drawable.icon2)); icons.add(getResources().getDrawable(R.drawable.icon3)); icons.add(getResources().getDrawable(R.drawable.icon1)); icons.add(getResources().getDrawable(R.drawable.icon2)); icons.add(getResources().getDrawable(R.drawable.icon3)); for(inti=2000;i<2006;i++){ data.add(""+i); } picker.setPickerData(data); picker.setPickerSize(100,200); picker.defaultScrollToPos(4); picker.setOnPickerItemSelectedListener(newOnPickerItemSelectedListener(){ @Override publicvoidonItemSelected(Viewview,intpos,Stringtext){ } }); setContentView(picker); } }

Picker控件的核心代码如下:

publicclassPickerextendsLinearLayoutimplements CustomScrollView.OnScrollOverListener{ privateArrayList<String>data;//字符串数据List privateArrayList<Drawable>iconList;//与data相对应的图标List privateintheadAndFootHeight=150; privateintcellHeight=50; privateContextmContext; privateLinearLayoutcontentLinear; privateCustomScrollViewscrollview; privateTextViewselector; privateintindicaterOffset;//选择条的偏移量 privateViewipicker; privateintwidth; privateintheight; privateintdefaultPos; privateArrayList<View>viewlist=newArrayList<View>();//每一项中的View(可以是他的子类) privatebooleanisLayoutOnce;//只是layout一次的标记 privateintdestPos; publicPicker(Contextcontext){ this(context,null); } publicPicker(Contextcontext,AttributeSetattrs){ super(context,attrs); this.mContext=context; defaultPos=headAndFootHeight+cellHeight*1-indicaterOffset; FrameLayoutframeLayout=newFrameLayout(mContext); frameLayout.setBackgroundResource(R.drawable.while_bg); LayoutParamsparams=newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); frameLayout.setLayoutParams(params); CustomScrollViewmyScroll=newCustomScrollView(mContext); myScroll.setVerticalFadingEdgeEnabled(false); myScroll.setVerticalScrollBarEnabled(false); LayoutParamsparams2=newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); myScroll.setLayoutParams(params2); LinearLayoutlinear=newLinearLayout(mContext); linear.setLayoutParams(params2); linear.setOrientation(LinearLayout.VERTICAL); myScroll.addView(linear); myScroll.setLayoutParams(params2); TextViewselector1=newTextView(mContext); selector1.setBackgroundResource(R.drawable.selector_bg); selector1.setLayoutParams(params2); frameLayout.addView(myScroll); FrameLayout.LayoutParamsparams3=newFrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); params3.gravity=Gravity.CENTER_VERTICAL; frameLayout.addView(selector1,params3); scrollview=myScroll; contentLinear=linear; selector=selector1; ipicker=frameLayout; selector.setHeight(cellHeight);//将所有设置child宽高,位置的代码在onlayout之前调用 addView(ipicker); } @Override protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){ super.onLayout(false,l,t,r,b); Log.e("","onLayout"); if(isLayoutOnce){ return; } isLayoutOnce=true; inttop=selector.getTop()-4;//9patch图片的问题,经多次调试偏移四个像素最好 indicaterOffset=top; if(defaultPos==0){ scrollview.scrollTo(0,headAndFootHeight-indicaterOffset); }else{ scrollview.smoothScrollTo(0,defaultPos); } this.handler.sendEmptyMessage(destPos); } privateHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ scrollToPos(destPos); super.handleMessage(msg); } }; privateTextViewfootText; /**Ipicker显示的时候,默认滚动到pos位置*/ publicvoiddefaultScrollToPos(intpos){ this.destPos=pos; //flag=true; //defaultPos=headAndFootHeight+cellHeight*pos-indicaterOffset; } /**滚动到Pos位置*/ publicvoidscrollToPos(intpos){ scrollview.smoothScrollTo(0,headAndFootHeight+cellHeight*pos -indicaterOffset); } /**为控件添加数据 *listStringList **/ publicvoidsetPickerData(ArrayList<String>list){ this.data=list; prepare(); } /**为控件添加数据 *listStringList **/ publicvoidsetPickerData(ArrayList<String>stringlist,ArrayList<Drawable>iconlist){ if(stringlist.size()!=iconlist.size()){ Log.e("error","!!error!!thelengthofstringlistandiconlistisnotsame!!"); return; } this.data=stringlist; this.iconList=iconlist; prepare(); } /**setthesizeofthepicker*/ publicvoidsetPickerSize(intwidth,intheight){ this.width=width; this.height=height; } /** *preparepickerview,preparetheipcker'schildren(willcallonlayoutagain) **/ privatevoidprepare(){ contentLinear.removeAllViews(); LayoutParamsparams=newLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); if(width==0||height==0){ params.width=200; params.height=200; }else{ params.width=width; params.height=height; } ipicker.setLayoutParams(params); scrollview.setOnScrollOverListener(this); TextViewheadText=newTextView(mContext); headText.setHeight(headAndFootHeight); contentLinear.addView(headText); insertData(); footText=newTextView(mContext); footText.setHeight(headAndFootHeight); contentLinear.addView(footText); } privatevoidinsertData(){ viewlist.clear(); if(data==null||data.size()==0){ selector.setVisibility(View.GONE); return; } for(inti=0;i<data.size();i++){ LinearLayoutitemLayout=newLinearLayout(mContext); itemLayout.setGravity(Gravity.CENTER_VERTICAL); itemLayout.setPadding(20,0,0,0); if(iconList!=null){ ImageViewimage=newImageView(mContext); image.setBackgroundDrawable(iconList.get(i)); itemLayout.addView(image); } TextViewtext=newTextView(mContext); text.setHeight(cellHeight); text.setGravity(Gravity.CENTER); text.setTextSize(25); text.setTypeface(Typeface.DEFAULT_BOLD); text.setTextColor(Color.BLACK); itemLayout.addView(text); LayoutParamsparams=newLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); text.setLayoutParams(params); text.setText(data.get(i)); contentLinear.addView(itemLayout); viewlist.add(itemLayout); } } @Override publicvoidonScrollOver(finalintstopy){ finalintnum=(stopy-headAndFootHeight+indicaterOffset) /cellHeight; if(stopy+indicaterOffset//totrigeronItemSelectedonlyonetime -(headAndFootHeight+cellHeight*(num))==0){ if(listener!=null){ listener.onItemSelected(viewlist.get(num),num,data.get(num)); } } ((Activity)mContext).runOnUiThread(newRunnable(){ @Override publicvoidrun(){ if(stopy+indicaterOffset<headAndFootHeight){ scrollview.smoothScrollTo(0,headAndFootHeight -indicaterOffset); }elseif(stopy+indicaterOffset>=headAndFootHeight +cellHeight*num &&stopy+indicaterOffset<=headAndFootHeight +cellHeight*(data.size()-1)){ if(stopy+indicaterOffset -(headAndFootHeight+cellHeight*(num))<=cellHeight/2){//less scrollview.smoothScrollTo(0,headAndFootHeight +cellHeight*num-indicaterOffset); }else{ scrollview.smoothScrollTo(0,headAndFootHeight +cellHeight*(num+1)-indicaterOffset); } }elseif(stopy+indicaterOffset>headAndFootHeight +cellHeight*(data.size()-1)){ scrollview.smoothScrollTo(0,headAndFootHeight+cellHeight *(data.size()-1)-indicaterOffset); }else{ } } }); } privateOnPickerItemSelectedListenerlistener; publicvoidsetOnPickerItemSelectedListener( OnPickerItemSelectedListenerlistener){ this.listener=listener; } interfaceOnPickerItemSelectedListener{ /** *@paramview:viewoftheaspecificitem(linearLayout) *@parampos:position *@paramtext:Stringoftheaspecificitem **/ voidonItemSelected(Viewview,intpos,Stringtext); } }



//欢迎提出宝贵意见
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: