I. Definition of Wheelview
Combine the masterpiece of the Netizen, realize the sliding choice effect.
Second, the use of Wheelview
public class Mainactivity extends Activity {private Button mainbt;private static final String TAG = MainActivity.class.get Simplename ();p rivate static final string[] planets = new string[] {"Zhao Yun", "Guan Yu", "Zhang Fei", "Bu", "Dian Wei", "Ma Chao", "Jack Huang", "Zhuge Liang"}; @Over rideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) MAINBT = (Button) Findviewbyid (R.ID.MAIN_BT); Mainbt.setonclicklistener (New OnClickListener () {@Overridepublic void OnClick (View v) {//build popup Viewview Outerview = Layoutinflater.from (mainactivity.this). Inflate ( R.layout.wheel_view, NULL); Wheelview WV = (Wheelview) Outerview.findviewbyid (R.ID.WHEEL_VIEW_WV);//Wv.setoffset (0);//Offset wv.setoffset (2); Wv.setitems (Arrays.aslist (planets));//actual content wv.setseletion (0);//Set Default selected Items//wv.setseletion (3); Wv.setonwheelviewlistener (New Wheelview.onwheelviewlistener () {@Overridepublic void onselected (int selectedindex, String item) {//Selected after processing event log.d (TAG, "[Dialog]selectedindex:" + selecteDindex+ ", Item:" + Item);}}); /Show pop-up Box new Alertdialog.builder (Mainactivity.this). Settitle ("Wheelview in Dialog"). Setview (Outerview). Setpositivebutton ("OK", null). Show ();}});}}Show the final effect:
Third, custom control source "Copy can be used directly"
Yes, the source code of the custom control is the most precious. Go back to the Android and hopefully later, you can build more cool controls.
public class Wheelview extends ScrollView {public static final String TAG = WheelView.class.getSimpleName (); public static class Onwheelviewlistener {public void onselected (int selectedindex, String item) {}} Private context context; Private linearlayout views; Public Wheelview (Context context) {super (context); Init (context); } public Wheelview (context context, AttributeSet Attrs) {Super (context, attrs); Init (context); } public Wheelview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); Init (context); } list<string> items; Private List<string> GetItems () {return items; public void Setitems (list<string> List) {if (null = = items) {items = new Arraylist<strin G> (); } items.clear (); Items.addall (list); Front and back complement for (int i = 0; i < offset; i++) {iTems.add (0, ""); Items.Add (""); } initdata (); } public static final int off_set_default = 1; int offset = Off_set_default; Offset (needs to be complete at the front and back) public int getoffset () {return offset; } public void SetOffset (int offset) {this.offset = offset; } int displayitemcount; The number shown per page int selectedindex = 1; private void init (context context) {This.context = context; LOG.D (TAG, "Parent:" + this.getparent ()); This.setverticalscrollbarenabled (FALSE); views = new LinearLayout (context); Views.setorientation (linearlayout.vertical); This.addview (views); Scrollertask = new Runnable () {public void run () {int newy = getscrolly (); if (Initialy-newy = = 0) {//stopped final int remainder = initialy% ItemHeight; Final int divided = Initialy/itemheight; if (remainder = = 0) { SelectedIndex = divided + offset; Onseletedcallback (); } else {if (remainder > Itemheight/2) {WheelView.this.post (new Ru Nnable () {@Override public void run () { WheelView.this.smoothScrollTo (0, Initialy-remainder + itemheight); SelectedIndex = divided + offset + 1; Onseletedcallback (); } }); } else {WheelView.this.post (new Runnable () {@Override public void Run () {WheelView.this.smoothScrollTo (0, Initialy- remainder); SelectedIndex = divided + offset; Onseletedcallback (); } }); }}}} else {initialy = getscrolly (); WheelView.this.postDelayed (Scrollertask, Newcheck); } } }; } int initialy; Runnable Scrollertask; int newcheck = 50; public void Startscrollertask () {initialy = getscrolly (); This.postdelayed (Scrollertask, Newcheck); } private void InitData () {displayitemcount = offset * 2 + 1; for (String item:items) {Views.addview (CreateView (item)); } refreshitemview (0); } int itemheight = 0; Private TextView CreateView (String item) {TextView TV = new TextView (context); Tv.setlayoutparams (New Layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); Tv.setsingleline (TRUE); Tv.settextsize (TypedvALUE.COMPLEX_UNIT_SP, 20); Tv.settext (item); Tv.setgravity (Gravity.center); int padding = dip2px (15); tv.setpadding (padding, padding, padding, padding); if (0 = = itemheight) {itemheight = getviewmeasuredheight (TV); LOG.D (TAG, "itemheight:" + itemheight); Views.setlayoutparams (New Layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ItemHeight * displayitemcount)); Linearlayout.layoutparams LP = (linearlayout.layoutparams) this.getlayoutparams (); This.setlayoutparams (New Linearlayout.layoutparams (Lp.width, ItemHeight * displayitemcount)); } return TV; } @Override protected void onscrollchanged (int l, int t, int oldl, int Oldt) {super.onscrollchanged (L, T, OL DL, Oldt); Refreshitemview (t); if (T > Oldt) {scrolldirection = Scroll_direction_down; } else {scrolldirection = scroll_direction_up; }} private void refReshitemview (int y) {int position = y/itemheight + offset; int remainder = y% ItemHeight; int divided = Y/itemheight; if (remainder = = 0) {position = divided + offset; } else {if (remainder > itemheight/2) {position = divided + offset + 1; }} int childsize = Views.getchildcount (); for (int i = 0; i < childsize; i++) {TextView Itemview = (TextView) views.getchildat (i); if (null = = Itemview) {return; } if (position = = i) {Itemview.settextcolor (Color.parsecolor ("#0288ce")); } else {Itemview.settextcolor (Color.parsecolor ("#bbbbbb")); }}}/** * Gets the bounds of the selected area */int[] Selectedareaborder; Private int[] Obtainselectedareaborder () {if (null = = Selectedareaborder) {Selectedareaborder = new in T[2]; SelectedarEABORDER[0] = itemheight * OFFSET; SELECTEDAREABORDER[1] = ItemHeight * (offset + 1); } return Selectedareaborder; } private int scrolldirection =-1; private static final int scroll_direction_up = 0; private static final int scroll_direction_down = 1; Paint paint; int viewwidth; @Override public void setbackgrounddrawable (drawable background) {if (Viewwidth = = 0) {Viewwidth = (Activity). Getwindowmanager (). Getdefaultdisplay (). GetWidth (); LOG.D (TAG, "viewwidth:" + viewwidth); } if (null = = Paint) {paint = new paint (); Paint.setcolor (Color.parsecolor ("#83cde6")); Paint.setstrokewidth (dip2px (1f)); } background = new drawable () {@Override public void draw (canvas canvas) {CA Nvas.drawline (Viewwidth * 1/6, Obtainselectedareaborder () [0], Viewwidth * 5/6, Obtainselectedareaborder () [0], paint); Canvas.drawline (Viewwidth * 1/6, Obtainselectedareaborder () [1], Viewwidth * 5/6, Obtainselectedareaborder () [1], Paint); } @Override public void Setalpha (int alpha) {} @Override public Vo ID setcolorfilter (Colorfilter CF) {} @Override public int getopacity () { return 0; } }; Super.setbackgrounddrawable (background); } @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH); LOG.D (TAG, "W:" + W + ", H:" + H + ", Oldw:" + OLDW + ", OLDH:" + OLDH "); Viewwidth = W; Setbackgrounddrawable (NULL); }/** * Check callback */private void Onseletedcallback () {if (null! = Onwheelviewlistener) {ONWH Eelviewlistener.onselected (SelectedIndex, Items.get (SelectedIndex)); }} public void setseletion (int position) {Final int p = position; SelectedIndex = p + offset; This.post (New Runnable () {@Override public void run () {WheelView.this.smoothScrollT O (0, p * itemheight); } }); } public String Getseleteditem () {return items.get (SelectedIndex); } public int Getseletedindex () {return selectedindex-offset; } @Override public void fling (int velocityy) {super.fling (VELOCITYY/3); } @Override public boolean ontouchevent (motionevent ev) {if (ev.getaction () = = motionevent.action_up) { Startscrollertask (); } return super.ontouchevent (EV); } private Onwheelviewlistener Onwheelviewlistener; Public Onwheelviewlistener Getonwheelviewlistener () {return onwheelviewlistener; } public void Setonwheelviewlistener (Onwheelviewlistener onwheelviewlistener) {This.onwheelviewlistener = OnWhe Elviewlistener; } private int dip2px (fLoat dpvalue) {final float scale = context.getresources (). Getdisplaymetrics (). density; return (int) (Dpvalue * scale + 0.5f); } private int Getviewmeasuredheight (view view) {int width = View.MeasureSpec.makeMeasureSpec (0, View.measurespe c.unspecified); int expandspec = View.MeasureSpec.makeMeasureSpec (integer.max_value >> 2, View.MeasureSpec.AT_MOST); View.measure (width, expandspec); return View.getmeasuredheight (); }} If you find a bug in the code, please do not hesitate to enlighten ~_~
Here is the source demo
Experience + Summary = Life
there is no best choice, only the most suitable choice!
Android Custom Control Wheelview