一、定义WheelView
融合网友的杰作,实现滑动选择效果。
二、WheelView的使用
public class MainActivity extends Activity {
private Button mainBt;
private static final String TAG = MainActivity.class.getSimpleName();
private static final String[] PLANETS = new String[] { "赵云", "关羽", "张飞",
"吕布", "典韦", "马超", "黄忠", "诸葛亮" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBt = (Button) findViewById(R.id.main_bt);
mainBt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 构建弹出框View
View outerView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.wheel_view, null);
WheelView wv = (WheelView) outerView
.findViewById(R.id.wheel_view_wv);
// wv.setOffset(0);// 偏移量
wv.setOffset(2);
wv.setItems(Arrays.asList(PLANETS));// 实际内容
wv.setSeletion(0);// 设置默认被选中的项目
// wv.setSeletion(3);
wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@Override
public void onSelected(int selectedIndex, String item) {
// 选中后的处理事件
Log.d(TAG, "[Dialog]selectedIndex: " + selectedIndex
+ ", item: " + item);
}
});
// 展示弹出框
new AlertDialog.Builder(MainActivity.this)
.setTitle("WheelView in Dialog").setView(outerView)
.setPositiveButton("OK", null).show();
}
});
}
}
展示最终效果:
三、自定义控件源码【复制可直接使用】
是的,自定义控件源码才是最宝贝的。重走Android路,希望以后的自己,能够构造出来更多炫酷的控件。
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 WheelVi