Android 时间滚动控件

记录平时的知识点积累。多数参考网络他人资源,已经无法查证,在这感谢原作者。

Android中自定义时间控件。

private  WheelView mHourView;
		
mHourView = (WheelView) this.findViewById(R.id.time_wheel_hour);
mHourView.setAdapter(new NumberAdapter(mHourData));
mHourView.setScrollCycle(true);
mHourView.setSelection(hour);

其中NumberAdapter如下,

	private class NumberAdapter extends BaseAdapter {
        int mHeight = 50;
        int[] mData;

        public NumberAdapter(int[] data) {
            mHeight = (int) Utils.pixelToDp(mParent, mHeight);
            mData = data;
        }

        @Override
        public int getCount() {
            return (null != mData) ? mData.length : 0;
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @SuppressLint("DefaultLocale")
		@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView txtView = null;

            if (null == convertView) {
                convertView = new TextView(mParent);
                convertView.setLayoutParams(new TosGallery.LayoutParams(-1, mHeight));

                txtView = (TextView) convertView;
                txtView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18); // 字体大小
                txtView.setTextColor(Color.BLACK);
                txtView.setGravity(Gravity.CENTER);
            }

            String text = String.format("%02d", mData[position]);
            if (null == txtView) {
                txtView = (TextView) convertView;
            }
            
            txtView.setText(text);

            return convertView;
        }
    }

控件库连接: 点击打开链接


你可能感兴趣的:(java,android)