Recently encountered a project to use the scrolling selector, the native numberpicker is too poor customization, does not meet the UI requirements.
Open Source online Wheelview is written in ScrollView, can not cycle scrolling, and when the data volume is very large to load the item too much, performance is very low.
Then, or write a more reliable own, with the ListView implementation. Finished writing their own experience, performance is good, and then large data is not afraid of.
It feels good, mainactivity, and provides some interfaces that can be customized directly to your needs, calling methods in the back of the box.
Fill in a Picture:
Not much to say, directly on the code:
Cyclewheelview.java:
/** * Copyright (C) 2015 * * Cyclewheelview.java * * Description: * Author:liao Longhui * * Ver 1.0, 2015-0
7-15, Liao Longhui, Create file */package Com.example.wheelviewdemo;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.ColorFilter;
Import Android.graphics.Paint;
Import android.graphics.drawable.Drawable;
Import Android.os.Handler;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AbsListView;
Import Android.widget.BaseAdapter;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import java.util.ArrayList;
Import java.util.List; /** * Recyclable selector * @author Liao Longhui */public class Cyclewheelview extends ListView {public static final Strin
G TAG = CycleWheelView.class.getSimpleName ();
private static final int color_divider_defalut = Color.parsecolor ("#747474"); Private Static final int height_divider_default = 2;
private static final int color_solid_default = Color.parsecolor ("#3e4043");
private static final int color_solid_selet_default = Color.parsecolor ("#323335");
private static final int wheel_size_default = 3;
Private Handler Mhandler;
Private Cyclewheelviewadapter Madapter;
/** * Labels/private list<string> mlabels;
/** * Color of Selected Label */private int mlabelselectcolor = Color.White;
/** * Color of unselected Label */private int mlabelcolor = Color.gray;
/** * Gradual Alph * * Private float malphagradual = 0.7f;
/** * Color of Divider * * private int dividercolor = Color_divider_defalut;
/** * Height of Divider * * private int dividerheight = Height_divider_default;
/** * Color of Selected Solid * * private int seletedsolidcolor = Color_solid_selet_default;
/** * Color of unselected Solid * * private int solidcolor = Color_solid_default; /** * Size of Wheel, it shoUld is odd number like 3 or greater/private int mwheelsize = Wheel_size_default;
/** * Res Id of Wheel Item Layout */private int mitemlayoutid;
/** * Res Id of Label TextView * * private int mitemlabeltvid;
/** * Height of Wheel Item */private int mitemheight;
Private Boolean cylceenable;
private int Mcurrentpositon;
Private Wheelitemselectedlistener Mitemselectedlistener;
Public Cyclewheelview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Public Cyclewheelview (context, AttributeSet attrs) {Super (context, attrs);
Init ();
Public Cyclewheelview {Super (context);
private void Init () {Mhandler = new Handler ();
Mitemlayoutid = R.layout.item_cyclewheel;
Mitemlabeltvid = R.id.tv_label_item_wheel;
Madapter = new Cyclewheelviewadapter ();
Setverticalscrollbarenabled (FALSE);
Setscrollingcacheenabled (FALSE);
Setcachecolorhint (color.transparent); SetfadingedgElength (0);
Setoverscrollmode (Over_scroll_never);
Setdividerheight (0);
Setadapter (Madapter); Setonscrolllistener (New Onscrolllistener () {@Override public void onscrollstatechanged (Abslistview view, int scroll
State) {if (scrollstate = = scroll_state_idle) {View Itemview = getchildat (0);
if (Itemview!= null) {Float DeltaY = itemview.gety ();
if (DeltaY = = 0) {return;
} if (Math.Abs (DeltaY) < MITEMHEIGHT/2) {Smoothscrollby (Getdistance (DeltaY), 50);
else {Smoothscrollby (getdistance (mitemheight + deltay), 50);
@Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount,
int totalitemcount) {refreshitems ();
}
}); private int getdistance (float scrolldistance) {if (Math.Abs (scrolldistance) <= 2) {return (int) Scrolldistan
Ce else if (Math.Abs (scrolldistance) <) {return scrolldistance > 0? 2: -2;
else {return (int) (SCROLLDISTANCE/6);
} private void Refreshitems () {int offset = MWHEELSIZE/2;
int firstposition = Getfirstvisibleposition ();
int position = 0;
if (getchildat (0) = = null) {return;
} if (Math.Abs (Getchildat (0). GetY ()) <= mitemheight/2) {position = firstposition + offset;
else {position = firstposition + offset + 1;
} if (position = = Mcurrentpositon) {return;
} Mcurrentpositon = position;
if (Mitemselectedlistener!= null) {mitemselectedlistener.onitemselected (GetSelection (), Getselectlabel ());
} resetitems (firstposition, position, offset); private void Resetitems (int firstposition, int position, int offset) {for (int i = position-offset-1; I < POS Ition + offset + 1;
i++) {View Itemview = Getchildat (i-firstposition);
if (Itemview = = null) {continue;
} TextView LABELTV = (TextView) Itemview.findviewbyid (Mitemlabeltvid); if (position = i) {Labeltv.setTextColor (Mlabelselectcolor);
Itemview.setalpha (1f);
else {labeltv.settextcolor (mlabelcolor);
int delta = math.abs (i-position);
Double alpha = Math.pow (malphagradual, Delta);
Itemview.setalpha ((float) alpha); /** * Set the scale list of the wheel * * @param labels/public void Setlabels (list<string> labels) {mlabels =
Labels
Madapter.setdata (Mlabels);
Madapter.notifydatasetchanged ();
Initview (); /** * Set Wheel rolling monitor * * @param mitemselectedlistener/public void Setonwheelitemselectedlistener (Wheelitemselec
Tedlistener mitemselectedlistener) {this.mitemselectedlistener = Mitemselectedlistener;
/** * Get the scale list of the wheel * * @return/public list<string> getlabels () {return mlabels; /** * Set whether the wheel is circular scrolling * * @param enable true-loop false-one-way/public void setcycleenable (Boolean enable) {if (
Cylceenable!= enable) {cylceenable = enable;
Madapter.notifydatasetchanged (); SetSelection (GetselectIon ());
}/* * Scroll to the specified position * * @Override public void setselection (final int position) {Mhandler.post (new Runnable () {
@Override public void Run () {CycleWheelView.super.setSelection (getPosition (position));
}
});
private int getPosition (int positon) {if (Mlabels = null | | mlabels.size () = 0) {return 0;
} if (cylceenable) {int d = INTEGER.MAX_VALUE/2/Mlabels.size ();
return positon + D * mlabels.size ();
return positon; /** * Get current wheel Position * * @return/public int getselection () {if (Mcurrentpositon = 0) {Mcurrentpositon =
MWHEELSIZE/2;
Return (MCURRENTPOSITON-MWHEELSIZE/2)% mlabels.size ();
/** * Gets the tick of the current wheel position * * @return/public String Getselectlabel () {int position = GetSelection (); Position = Position < 0?
0:position;
try {return mlabels.get (position);
catch (Exception e) {return ""; }/** * If you need to customize the wheel per item, call this method to set the custom item layout, a TextView is required in the custom layout to display the wheelScale * * @param itemresid layout file ID * @param labeltvid scale TextView Resource ID */public void setwheelitemlayout (int itemresi
d, int labeltvid) {mitemlayoutid = Itemresid;
Mitemlabeltvid = Labeltvid;
Madapter = new Cyclewheelviewadapter ();
Madapter.setdata (Mlabels);
Setadapter (Madapter);
Initview (); /** * Set unselected tick text color * * @param labelcolor/public void Setlabelcolor (int labelcolor) {This.mlabelcolor =
Labelcolor;
Resetitems (Getfirstvisibleposition (), Mcurrentpositon, MWHEELSIZE/2); /** * Set selected tick text color * * @param labelselectcolor/public void Setlabelselectcolor (int labelselectcolor) {th
Is.mlabelselectcolor = Labelselectcolor;
Resetitems (Getfirstvisibleposition (), Mcurrentpositon, MWHEELSIZE/2); /** * Set wheel scale transparent gradient value * * @param alphagradual */public void setalphagradual (float alphagradual) {This.malpha
gradual = alphagradual;
Resetitems (Getfirstvisibleposition (), Mcurrentpositon, MWHEELSIZE/2); /** * Set the number of ticks that the wheel can display, you mustis odd, and is greater than or equal to 3 * * @param wheelsize * @throws cyclewheelviewexception Wheel number error/public void setwheelsize (int wheelsi Ze) throws cyclewheelviewexception {if (Wheelsize < 3 | | | wheelsize% 2!= 1) {throw new Cyclewheelviewexception (
"Wheel Size Error, must be 3,5,7,9 ...");
else {mwheelsize = wheelsize;
Initview (); }/** * Set block color * @param unselectedsolidcolor the color of unselected blocks * @param the color of the selected block/public void SE
Tsolid (int unselectedsolidcolor, int selectedsolidcolor) {this.solidcolor = Unselectedsolidcolor;
This.seletedsolidcolor = Selectedsolidcolor;
Initview (); /** * Set Split line style * @param dividercolor split Line Color * @param dividerheight Split line height (px) */public void setdivider (int div
Idercolor, int dividerheight) {this.dividercolor = Dividercolor;
This.dividerheight = Dividerheight;
@SuppressWarnings ("deprecation") private void Initview () {mitemheight = Measureheight ();
Viewgroup.layoutparams LP = Getlayoutparams (); Lp.height = Mitemheight * mwheelsize;
Madapter.setdata (Mlabels);
Madapter.notifydatasetchanged ();
drawable Backgroud = new drawable () {@Override public void Draw (Canvas Canvas) {int viewwidth = getwidth ();
Paint dividerpaint = new Paint ();
Dividerpaint.setcolor (Dividercolor);
Dividerpaint.setstrokewidth (Dividerheight);
Paint seletedsolidpaint = new Paint ();
Seletedsolidpaint.setcolor (Seletedsolidcolor);
Paint solidpaint = new Paint ();
Solidpaint.setcolor (Solidcolor);
Canvas.drawrect (0, 0, viewwidth, Mitemheight * (MWHEELSIZE/2), solidpaint);
Canvas.drawrect (0, Mitemheight * (MWHEELSIZE/2 + 1), Viewwidth, Mitemheight * (mwheelsize), solidpaint); Canvas.drawrect (0, Mitemheight * (MWHEELSIZE/2), Viewwidth, Mitemheight * (MWHEELSIZE/2 + 1), seletedsolidpaint)
;
Canvas.drawline (0, Mitemheight * (MWHEELSIZE/2), Viewwidth, Mitemheight * (MWHEELSIZE/2), dividerpaint); Canvas.drawline (0, Mitemheight* (MWHEELSIZE/2 + 1), Viewwidth, Mitemheight * (MWHEELSIZE/2 + 1), dividerpaint);
@Override public void Setalpha (int alpha) {} @Override public void Setcolorfilter (Colorfilter CF) {
@Override public int getopacity () {return 0;
}
};
Setbackgrounddrawable (Backgroud);
private int measureheight () {View Itemview = Layoutinflater.from (GetContext ()). Inflate (Mitemlayoutid, NULL);
Itemview.setlayoutparams (New Viewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);
Itemview.measure (W, h);
int height = itemview.getmeasuredheight ();
int width = view.getmeasuredwidth ();
return height;
Public interface Wheelitemselectedlistener {public void onitemselected (int position, String label); } public class CyclewheelviewexCeption extends Exception {private static final long serialversionuid = 1L;
Public cyclewheelviewexception (String detailmessage) {super (detailmessage); } public class Cyclewheelviewadapter extends Baseadapter {private list<string> mdata = new Arraylist<str
Ing> ();
public void SetData (list<string> mwheellabels) {mdata.clear ();
Mdata.addall (Mwheellabels);
@Override public int GetCount () {if (cylceenable) {return integer.max_value;
return mdata.size () + mWheelSize-1;
@Override public Object getitem (int position) {return ";
@Override public long getitemid (int position) {return position;
@Override public boolean isenabled (int position) {return false;
@Override public View getview (int position, View Convertview, ViewGroup parent) {if (Convertview = null) {
Convertview = Layoutinflater.from (GetContext ()). Inflate (Mitemlayoutid, NULL); } TextView TextView = (TextView) Convertview.findviewbyid (Mitemlabeltvid); if (Position < MWHEELSIZE/2 | | (!cylceenable && position >= mdata.size () + MWHEELSIZE/2))
{Textview.settext ("");
Convertview.setvisibility (view.invisible);
else {textview.settext (mdata.get (POSITION-MWHEELSIZE/2)% mdata.size ()));
Convertview.setvisibility (view.visible);
return convertview;
}
}
}
Mainactivity.java:
public class Mainactivity extends activity {private Cyclewheelview cyclewheelview0,cyclewheelview1, cycleWheelView2;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
CycleWheelView0 = (Cyclewheelview) Findviewbyid (R.id.cyclewheelview);
list<string> labels = new arraylist<> ();
for (int i = 0; i < i++) {Labels.add ("" + i);
} cyclewheelview0.setlabels (labels);
Cyclewheelview0.setalphagradual (0.5f); Cyclewheelview0.setonwheelitemselectedlistener (New Wheelitemselectedlistener () {@Override public void onitemselecte
d (int position, String label) {LOG.D ("test", label);
}
});
CycleWheelView1 = (Cyclewheelview) Findviewbyid (R.ID.CYCLEWHEELVIEW1);
list<string> labels1 = new arraylist<> ();
for (int i = 0; i < i++) {Labels1.add ("" + i);
} cyclewheelview1.setlabels (LABELS1); try {cyclewheelview1.setwheelsize(5);
catch (Cyclewheelviewexception e) {e.printstacktrace ();
} cyclewheelview1.setselection (2);
Cyclewheelview1.setwheelitemlayout (R.layout.item_cyclewheel_custom, R.id.tv_label_item_wheel_custom); Cyclewheelview1.setonwheelitemselectedlistener (New Wheelitemselectedlistener () {@Override public void onitemselecte
d (int position, String label) {LOG.D ("test", label);
}
});
CycleWheelView2 = (Cyclewheelview) Findviewbyid (R.ID.CYCLEWHEELVIEW2);
list<string> labels2 = new arraylist<> ();
for (int i = 0; i < i++) {Labels2.add ("" + i);
} cyclewheelview2.setlabels (LABELS2);
try {cyclewheelview2.setwheelsize (7);
catch (Cyclewheelviewexception e) {e.printstacktrace ();
} cyclewheelview2.setcycleenable (True);
Cyclewheelview2.setselection (30);
Cyclewheelview2.setalphagradual (0.6f);
Cyclewheelview2.setdivider (Color.parsecolor ("#abcdef"), 2);
Cyclewheelview2.setsolid (Color.white,color.white); Cyclewheelview2.sEtlabelcolor (Color.Blue);
Cyclewheelview2.setlabelselectcolor (color.red); Cyclewheelview2.setonwheelitemselectedlistener (New Wheelitemselectedlistener () {@Override public void onitemselecte
d (int position, String label) {LOG.D ("test", label);
}
});
}
}
Item_cyclewheel.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" Wrap_content "
android: padding= "16DP"
android:background= "@android: Color/transparent" >
<textview
android:id= "@+id/ Tv_label_item_wheel "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_content
" Android:textsize= "20SP"
android:singleline= "true"
android:layout_centerhorizontal= "true
" Android:layout_centervertical= "true"/>
</RelativeLayout>
Item_cyclewheel_custom.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" Wrap_content "
android: padding= "16DP"
android:background= "@android: Color/transparent" >
<textview
android:id= "@+id/ Tv_label_item_wheel_custom "
android:layout_width=" wrap_content "
android:layout_height=" Wrap_content "
android:singleline= "true"
android:layout_alignparentleft= "true"
android:layout_centervertical= "True"/>
<imageview
android:layout_width= "25DP"
android:layout_height= "25DP"
Android : Layout_centervertical= "true"
android:layout_alignparentright= "true"
android:src= "@drawable/ic_ Launcher "/>
</RelativeLayout>
Activity_main.xml:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools=
"Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_parent" android:orientation= "Horizontal" > <com.example.wheelviewdemo.cyclewheelview android:id= "@+id/ Cyclewheelview "android:layout_width=" 0DP "android:layout_height=" Wrap_content "android:layout_weight=" 1 "> < /com.example.wheelviewdemo.cyclewheelview> <com.example.wheelviewdemo.cyclewheelview android:id= "@+id/ CycleWheelView1 "android:layout_width=" 0DP "android:layout_height=" Wrap_content "android:layout_weight=" 1 "> ;/com.example.wheelviewdemo.cyclewheelview> <com.example.wheelviewdemo.cyclewheelview android:id= "@+id/ CycleWheelView2 "android:layout_width=" 0DP "android:layout_height=" Wrap_content "android:layout_weight=" 1 "> ;/com.example.wheelviewdemo.cyclewheelview> </LinearLayout>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.