// 给ListView设置一个滑动监听器,就可以把它变成WheelViewlistView.setOnScrollListener(new AbsListView.OnScrollListener() { /* 实现代码如下所示 */});
@Override Public void onscrollstatechanged(Abslistview view,intScrollstate) {//When slide is stopped if(scrollstate = = AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {View Firstvisibleview = View.getchildat (0);intHeight = firstvisibleview.getheight ();/** * Top is the relative distance, in pixels, between the top of the Firstvisibleview and the parent. * Android's Axis origin is in the upper left corner of the screen, and the higher the Y value, the smaller. * Since Firstvisibleview is always partially invisible, the value range of top is: (-height, 0] */ inttop = Firstvisibleview.gettop ();intVisibleheight = top + height;if(Visibleheight <= (height >>1)) {//If the Firstvisibleview visible height is less than or equal to half, slide up until invisibleView.scrollby (0, Visibleheight + (ListView) view. Getdividerheight ()); }Else{//If the Firstvisibleview visible height is greater than half, slide until fully visibleView.scrolllistby (0, top); }//Call Scrollby (), the onscroll () is raised}}@Override Public void onscroll(Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {intNewmiddleposition = Firstvisibleitem + ((VisibleItemCount-1) >>1);if(mmiddleposition! = newmiddleposition) {mmiddleposition = newmiddleposition; Baseadapter adapter = ((Baseadapter) view.getadapter ()); Adapter.notifydatasetchanged ();///You can then invoke the custom listener that listens for mmiddleposition changes}}
40-line key code for Android custom Wheelview