wheelview--->安卓滚轮控件
来源:互联网 发布:java国际象棋棋盘棋子 编辑:程序博客网 时间:2024/05/17 18:26
没有太多时间,直接上控件源码吧,直接从github上也可以找到,有很重量级的.包括三级联动的,全国省市,时间选择(貌似时间选择的用自带的 datepicker还是很不错的哦)
直接git clone https://github.com/wangjiegulu/WheelView
https://github.com/wangjiegulu/WheelView
package com.example.administrator.myapplication;
import android.app.Activity;
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.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Author: wangjie
* Email: tiantian.china.2@gmail.com
* Date: 7/1/14.
*/
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 ScrollView scrollView;
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);}// String[] items;List<String> items;private List<String> getItems() { return items;}public void setItems(List<String> list) { if (null == items) { items = new ArrayList<String>(); } 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;}public void setOffset(int offset) { this.offset = offset;}int displayItemCount; // 每页显示的数量int selectedIndex = 1;private void init(Context context) { this.context = context;
// scrollView = ((ScrollView)this.getParent());
// Log.d(TAG, “scrollview: ” + scrollView);
Log.d(TAG, “parent: ” + this.getParent());
// this.setOrientation(VERTICAL);
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;
// Log.d(TAG, “initialY: ” + initialY);
// Log.d(TAG, “remainder: ” + remainder + “, divided: ” + divided);
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;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;}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt);
// Log.d(TAG, “l: ” + l + “, t: ” + t + “, oldl: ” + oldl + “, oldt: ” + oldt);
// try {
// Field field = ScrollView.class.getDeclaredField(“mScroller”);
// field.setAccessible(true);
// OverScroller mScroller = (OverScroller) field.get(this);
//
//
// if(mScroller.isFinished()){
// Log.d(TAG, “isFinished…”);
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// }
refreshItemView(t); if (t > oldt) {
// Log.d(TAG, “向下滚动”);
scrollDirection = SCROLL_DIRECTION_DOWN;
} else {
// Log.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; }
// if(remainder > itemHeight / 2){
// if(scrollDirection == SCROLL_DIRECTION_DOWN){
// position = divided + offset;
// Log.d(TAG, “>down…position: ” + position);
// }else if(scrollDirection == SCROLL_DIRECTION_UP){
// position = divided + offset + 1;
// Log.d(TAG, “>up…position: ” + position);
// }
// }else{
//// position = y / itemHeight + offset;
// if(scrollDirection == SCROLL_DIRECTION_DOWN){
// position = divided + offset;
// Log.d(TAG, “
- wheelview--->安卓滚轮控件
- 安卓自定义滚轮WheelView效果
- Android滚轮控件WheelView
- WheelView Android滚轮控件
- 安卓底部滚轮控件
- Android自定义实现循环滚轮控件WheelView
- WheelView滚轮思路学习
- 自定义滚轮选择器Wheelview
- 一个文件实现安卓滚轮选择控件
- 一个文件实现安卓滚轮选择控件
- 一个文件实现安卓滚轮选择控件
- invalidate() 后 不走onDraw() 方法 WheelView滚轮控件问题
- 自定义WheelView UI 实现Ios滚轮效果
- 自定义滚轮WheelView源码全方位分析
- Android 自定义控件WheelView
- WheelView滚动选择控件
- 滚轮控件
- 时间日期滚轮(安卓)
- SDC时序约束(1)- create_clock
- 多态与多态案例
- 初学单片机时总结的想法及一些程序
- matplotlib 可视化 —— 移动坐标轴(中心位置)
- 自定义DatePickerDialog
- wheelview--->安卓滚轮控件
- 07盒子模型和结构样式
- ubuntu下7z文件的解压方法
- Win10 开发3
- 灰度发布
- Retrofit+RxJava 优雅的处理服务器返回异常、错误
- logback系列之六:在Test类中加载logback配置文件
- Intellij IDEA2016.2 创建web项目
- Bzoj[2023/1630]: [Usaco2005 Nov]Ant Counting 数蚂蚁 DP+滚动数组