嗨 大家好,我是不服不行 。
今天为大家带来一个手机界面中的这个,这个一个常客。经常被使用在时间,日期选择之中,让我们先看看效果。
那么是如何实现的,仅仅只涉及到一个类: 此类在github被我寻找出来(请原谅我不记得原类的地址),做了稍加改动。
地址找到啦[2015年11月20号]! https://github.com/wangjiegulu/WheelView
但不是非常推荐,实现原理是继承了ScrollView 。那么当内容过多的时候由于没有缓存机制,数据越多滑动越卡。
不过江湖救急能用就不错了,慢慢再去发现好的。如果有使用自定义view或者继承ListView、RecyclerView的就好了。
2016年1月17号 新方案~
http://blog.csdn.net/bfbx5173/article/details/50532187
此方案为自定义View,效果性能都不错。
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 = new ArrayList<String>(); /** 设置内容 */ public void setItems(List<String> list) { items.clear(); items.addAll(list); // 前面和后面补全 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; // 偏移量(需要在最前面和最后面补全) public int getOffset() { return offset; } /** 设置显示界面上展现多少个item */ public void setOffset(int offset) { this.offset = offset; } int displayItemCount; // 每页显示的数量 int selectedIndex = 1; private void init(Context context) { this.context = context; 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 Runnable() { @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; /** 滚轮控件中的View 在这里创建, 本方法里面只是简单的TextView */ private TextView createView(String item) { TextView tv = new TextView(context); itemHeight = BaseUtil.dip2px(context, 46); LayoutParams tvlp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight); tv.setLayoutParams(tvlp); tv.setSingleLine(true); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); tv.setText(item); tv.setGravity(Gravity.CENTER); views.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight * displayItemCount)); FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) this.getLayoutParams(); this.setLayoutParams(new FrameLayout.LayoutParams(lp.width, itemHeight * displayItemCount)); int padding = BaseUtil.dip2px(context, 15); this.setPadding(padding, 0, padding, 0); return tv; } public static int getViewMeasuredHeight(View view) { calcViewMeasure(view); return view.getMeasuredHeight(); } /** * 获取控件的宽度,如果获取的宽度为0,则重新计算尺寸后再返回宽度 * * @param view * @return */ public static int getViewMeasuredWidth(View view) { calcViewMeasure(view); return view.getMeasuredWidth(); } /** * 测量控件的尺寸 * * @param view */ public static void calcViewMeasure(View view) { int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST); view.measure(width, expandSpec); } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); refreshItemView(t); if (t > oldt) { // Logger.d(TAG, "向下滚动"); scrollDirection = SCROLL_DIRECTION_DOWN; } else { // Logger.d(TAG, "向上滚动"); 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("#00a2ff")); itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); } else if (Math.abs(position - i) == 1) { itemView.setTextColor(Color.parseColor("#afafaf")); itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); } else { itemView.setTextColor(Color.parseColor("#dfdfdf")); itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); } } } /** * 获取选中区域的边界 */ int[] selectedAreaBorder; private int[] obtainSelectedAreaBorder() { if (null == selectedAreaBorder) { selectedAreaBorder = new int[2]; selectedAreaBorder[0] = itemHeight * offset; selectedAreaBorder[1] = itemHeight * (offset + 1); } return selectedAreaBorder; } @SuppressWarnings("unused") private int scrollDirection = -1; private static final int SCROLL_DIRECTION_UP = 0; private static final int SCROLL_DIRECTION_DOWN = 1; Paint paint; int viewWidth; @SuppressWarnings("deprecation") @Override public void setBackgroundDrawable(Drawable background) { if (viewWidth == 0) { viewWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth(); } if (null == paint) { paint = new Paint(); paint.setColor(Color.parseColor("#919191")); paint.setStrokeWidth(1); } background = new Drawable() { @Override public void draw(Canvas canvas) { canvas.drawLine(viewWidth * 0 / 6, obtainSelectedAreaBorder()[0], viewWidth * 6 / 6, obtainSelectedAreaBorder()[0], paint); canvas.drawLine(viewWidth * 0 / 6, obtainSelectedAreaBorder()[1], viewWidth * 6 / 6, obtainSelectedAreaBorder()[1], paint); } @Override public void setAlpha(int alpha) { } @Override public void 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); viewWidth = w; setBackgroundDrawable(null); } /** * 选中回调 */ private void onSeletedCallBack() { if (null != onWheelViewListener) { onWheelViewListener.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.smoothScrollTo(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 = onWheelViewListener; } }
从代码中先寻找public对外公开方法,主要有setOffset() setItems()方法, 那么观察一下后会发现需要调用setItems()来初始化滚轮控件。在setItems()中又会调用initData(),而initData()的逻辑便是根据传入的参数创建一大堆TextView。最后放在了一个views对象中。查找views对象会发现在init(),它被添加在了ScrollView里面,同时它(views)自己又是个LinearLayout。这个时候便逻辑顺畅了。
这个WheelView 只不过是动态生成的,它对应的布局文件应该是这样的:
<span style="color:#333333;"><ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" </span><strong><span style="color:#ff0000;">* n</span></strong><span style="color:#333333;"> android:layout_height="wrap_content" /> </LinearLayout> </ScrollView></span>
那么他和普通的scrollView有什么区别了。关键是在于一个“回滚“。 因为当滑动完成后,它必须停留在一项上面。
分析出了不同的同时,该如何做的逻辑是不是也清晰了起来呢?由于是滑动完成,那么就和Touch有关:
@Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_UP) { startScrollerTask(); } return super.onTouchEvent(ev); }在手指抬起之后变会调用startScrollerTask()方法,用大腿想想就知道此方法应该就是” 必须停留在一项上面“的收尾动作,没错!startScrollerTask会在50毫秒之后去执行scrollerTask,而scrollerTask的职责就是调用smoothScrollTo()滑动到某一个Child的位置,方法中顺手触发了onSeletedCallBack()回调供我们处理逻辑。
那么讲到这里就只剩下绘制的部分:因为界面会随着ScrollView 的滑动而变化,所以迅速定位到onScrollChanged()。
在代码在onScrollChanged()中调用了refreshItemView(int position) 这个根据名字就可以判断是根据一个位置参数来更新Child(一大堆的TextView)的样子。。。。剩下的事情就是根据界面需要修改其中内容。
如何去使用:
在布局中
<com.example.view.WheelView android:id="@+id/wheel" android:layout_width="match_parent" android:layout_height="wrap_content" />在代码中:
WheelView wheelMin = (WheelView) content.findViewById(R.id.wheel); wheelMin.setOffset(2); wheelMin.setItems(Arrays.asList(getWheelContent(0, 60))); wheelMin.setOnWheelViewListener(new OnWheelViewListener() { @Override public void onSelected(int selectedIndex, String item) { ...... } });
private String[] getWheelContent(int start, int num) { if (start < 0 || num < 0) { return null; } String[] content = new String[num]; for (int i = start; i < content.length; i++) { content[i] = (i < 10 ? "0" : "") + String.valueOf(i); } return content; }