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);
}
}
//欢迎提出宝贵意见
1.不带有图标的
2.带有图标的
主要有两个类:
1.Picker
2.CustomListView
Picker控件的外部接口:
Picker控件的使用方法:
Picker控件的核心代码如下:
相关文章推荐
- Android自定义控件实战——水流波动效果的实现WaveView
- 【Android随记】-- Android自定义控件效果实现
- android自定义控件自动换行效果实现
- android实现iphone风格的picker
- Android自定义控件实现下拉刷新效果
- Android自定义控件实现雷达效果
- 安卓学习笔记---Android-PickerView实现 3D滚轮效果(时间选择器、省市区三级联动,单项选择效果)
- Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么
- Android自定义控件系列(二)—icon+文字的多种效果实现
- Android 类似IPhone图片点击效果实现,点击logo变暗
- Android自定义控件_水流波动效果的实现WaveView
- Android自定义控件系列(二)—icon+文字的多种效果实现
- Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么
- Android自定义控件实现按钮滚动选择效果
- android自定义控件自动换行效果实现
- Android自定义控件实战——水流波动效果的实现WaveView
- Android自定义控件实现icon+文字的多种效果
- 如何在android上实现IPhone的按钮滑动效果
- android自定义控件实现刮刮乐效果
- Android 通过自定义控件方式实现带开关效果的左右切换选择器。