/** * simplify the "setDisplayedValue() + setMinValue() + setMaxValue()" process, * default minValue is 0, and make sure you do NOT change the minValue. * @param display new values to be displayed */ public void refreshByNewDisplayedValues(String[] display) { int minValue = getMinValue(); int oldMaxValue = getMaxValue(); int oldSpan = oldMaxValue - minValue + 1; int newMaxValue = display.length - 1; int newSpan = newMaxValue - minValue + 1; if (newSpan > oldSpan) { setDisplayedValues(display); setMaxValue(newMaxValue); } else { setMaxValue(newMaxValue); setDisplayedValues(display); } }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mViewWidth = w; mViewHeight = h; mItemHeight = mViewHeight / mShowCount; mViewCenterX = ((float)(mViewWidth + getPaddingLeft() - getPaddingRight()))/2; int defaultValue = 0; if(getOneRecycleSize() > 1){ if(mHasInit) { defaultValue = getValue() - mMinValue; }else if(mCurrentItemIndexEffect) { defaultValue = mCurrDrawFirstItemIndex + (mShowCount - 1) / 2; }else{ defaultValue = 0; } } correctPositionByDefaultValue(defaultValue, mWrapSelectorWheel && mWrapSelectorWheelCheck); updateFontAttr(); updateNotWrapYLimit(); updateDividerAttr(); mHasInit = true; }
private void setData(NumberPickerView picker, int minValue, int maxValue, int value){ picker.setMinValue(minValue); picker.setMaxValue(maxValue); picker.setValue(value); }
/** * get the "fromValue" by using getValue(), if your picker's minValue is not 0, * make sure you can get the accurate value by getValue(), or you can use * smoothScrollToValue(int fromValue, int toValue, boolean needRespond) * @param toValue the value you want picker to scroll to */ public void smoothScrollToValue(int toValue){ smoothScrollToValue(getValue(), toValue, true); }
int oldStart = pickerView.getMinValue(); int oldStop = pickerView.getMaxValue(); int oldSpan = oldStop - oldStart + 1; int fromValue = pickerView.getValue(); pickerView.setMinValue(newStart); if(newSpan > oldSpan){ pickerView.setDisplayedValues(newDisplayedVales); pickerView.setMaxValue(newStop); }else{ pickerView.setMaxValue(newStop); pickerView.setDisplayedValues(newDisplayedVales); fromValue = newStart; pickerView.smoothScrollToValue(fromValue, toValue, needRespond); }else{ pickerView.setValue(newSway);
mFlagMayPress = true; mHandlerInNewThread.removeMessages(HANDLER_WHAT_REFRESH); stopScrolling(); downY = currY; downYGlobal = mCurrDrawGlobalY; onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: mCurrDrawGlobalY = limitY((int)(downYGlobal + spanY)); calculateFirstItemParameterByGlobalY(); invalidate(); onScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL); break; case MotionEvent.ACTION_UP: if(mFlagMayPress){ click(event); }else { final VelocityTracker velocityTracker = mVelocityTracker; if (Math.abs(velocityY) > mMiniVelocityFling) { mScroller.fling(0, mCurrDrawGlobalY, 0, -velocityY, Integer.MIN_VALUE, Integer.MAX_VALUE, limitY(Integer.MIN_VALUE), limitY(Integer.MAX_VALUE)); invalidate(); onScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); mHandlerInNewThread.sendMessageDelayed(getMsg(HANDLER_WHAT_REFRESH), 0);
switch (v.getId()) { case R.id.button1: picker.setWrapSelectorWheel(!picker.getWrapSelectorWheelAbsolutely()); getWrapState(); break; case R.id.button2: String[] display_1 = getResources().getStringArray(R.array.test_display_1); picker.refreshByNewDisplayedValues(display_1); getWrapState(); break; case R.id.button3: String[] display_2 = getResources().getStringArray(R.array.test_display_2); picker.refreshByNewDisplayedValues(display_2); getWrapState(); break; case R.id.button4: int value = picker.getValue(); picker.smoothScrollToValue(value, value + 2); break; case R.id.button5:
private void getCurrentContent(){ String[] content = mNumberPickerView.getDisplayedValues(); if (content != null) Toast.makeText(mContext.getApplicationContext(), mContext.getString(R.string.picked_content_is) + content[mNumberPickerView.getValue() - mNumberPickerView.getMinValue()], Toast.LENGTH_SHORT) .show(); } }
/** * * @param fromValue need to set the fromValue, can be greater than mMaxValue or less than mMinValue * @param toValue the value you want picker to scroll to * @param needRespond need Respond to the ValueChange callback When Scrolling, default is false */ public void smoothScrollToValue(int fromValue, int toValue, boolean needRespond){ int deltaIndex; fromValue = refineValueByLimit(fromValue, mMinValue, mMaxValue, mWrapSelectorWheel && mWrapSelectorWheelCheck); toValue = refineValueByLimit(toValue, mMinValue, mMaxValue, mWrapSelectorWheel && mWrapSelectorWheelCheck); if(mWrapSelectorWheel && mWrapSelectorWheelCheck) { deltaIndex = toValue - fromValue; int halfOneRecycleSize = getOneRecycleSize() / 2; if(deltaIndex < -halfOneRecycleSize || halfOneRecycleSize < deltaIndex ){ deltaIndex = deltaIndex > 0 ? deltaIndex - getOneRecycleSize() : deltaIndex + getOneRecycleSize(); } }else{ deltaIndex = toValue - fromValue; } setValue(fromValue); if(fromValue == toValue) return; scrollByIndexSmoothly(deltaIndex, needRespond); }
public String getContentByCurrValue(){ return mDisplayedValues[getValue() - mMinValue]; }
@Override public void onValueChange(NumberPickerView picker, int oldVal, int newVal) { String[] content = picker.getDisplayedValues(); if (content != null) { Log.d(TAG,"onValueChange content : " + content[newVal - picker.getMinValue()]); Toast.makeText(mContext.getApplicationContext(), "oldVal : " + oldVal + " newVal : " + newVal + "\n" + mContext.getString(R.string.picked_content_is) + content[newVal - picker.getMinValue()], Toast.LENGTH_SHORT) .show(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); picker = (NumberPickerView) this.findViewById(R.id.picker); button1 = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); button3 = (Button) this.findViewById(R.id.button3); button4 = (Button) this.findViewById(R.id.button4); button5 = (Button) this.findViewById(R.id.button5); button6 = (Button) this.findViewById(R.id.button6); button7 = (Button) this.findViewById(R.id.button7); picker.setOnScrollListener(this); picker.setOnValueChangedListener(this); picker.setOnValueChangeListenerInScrolling(this); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); button5.setOnClickListener(this); button6.setOnClickListener(this); button7.setOnClickListener(this); String[] display_2 = getResources().getStringArray(R.array.test_display_2); picker.refreshByNewDisplayedValues(display_2); getWrapState(); }
@Override public void onClick(View v) { switch (v.getId()){ case R.id.button_get_info: String h = mPickerViewH.getContentByCurrValue(); String m = mPickerViewM.getContentByCurrValue(); String d = mPickerViewD.getContentByCurrValue(); Toast.makeText(getApplicationContext(),h + getString(R.string.hour_hint) + " " + m + getString(R.string.minute_hint) + " " + d,Toast.LENGTH_LONG).show(); break; case R.id.button4: Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD); mPickerViewH.setHintTextTypeface(font); mPickerViewH.invalidate(); break; } }
public void smoothScrollToValue(int fromValue, int toValue){ smoothScrollToValue(fromValue, toValue, true); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_npv); mNumberPickerView = (NumberPickerView) this.findViewById(R.id.picker); mNumberPickerView.setOnScrollListener(this); mNumberPickerView.setOnValueChangedListener(this); mDisplayValues = mContext.getResources().getStringArray(R.array.test_display_2); // mNumberPickerView.refreshByNewDisplayedValues(mDisplayValues); mButtonGetInfo = (Button) this.findViewById(R.id.button_get_info); mButtonGetInfo.setOnClickListener(this); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_time_picker); mPickerViewH = (NumberPickerView)this.findViewById(R.id.picker_hour); mPickerViewM = (NumberPickerView)this.findViewById(R.id.picker_minute); mPickerViewD = (NumberPickerView)this.findViewById(R.id.picker_half_day); mPickerViewH.setOnValueChangedListener(this); mPickerViewM.setOnValueChangedListener(this); mPickerViewD.setOnValueChangedListener(this); /*Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD); mPickerViewH.setHintTextTypeface(font);*/ mButtonInfo = (Button)this.findViewById(R.id.button_get_info); mButtonInfo2 = (Button)this.findViewById(R.id.show_info_button); mButton4 = (Button)this.findViewById(R.id.button4); mButtonInfo.setOnClickListener(this); mButton4.setOnClickListener(this); initTime(); }
private void getCurrentContent(){ String[] content = picker.getDisplayedValues(); if (content != null) Toast.makeText(getApplicationContext(), getString(R.string.picked_content_is) + content[picker.getValue() - picker.getMinValue()], Toast.LENGTH_SHORT) .show(); }
/** * get the "fromValue" by using getValue(), if your picker's minValue is not 0, * make sure you can get the accurate value by getValue(), or you can use * smoothScrollToValue(int fromValue, int toValue, boolean needRespond) * @param toValue the value you want picker to scroll to * @param needRespond set if you want picker to respond onValueChange listener */ public void smoothScrollToValue(int toValue, boolean needRespond){ smoothScrollToValue(getValue(), toValue, needRespond); }
@Override public void onValueChange(NumberPickerView picker, int oldVal, int newVal) { String[] content = picker.getDisplayedValues(); if (content != null) Toast.makeText(getApplicationContext(),"oldVal : " + oldVal + " newVal : " + newVal + "\n" + getString(R.string.picked_content_is) + content[newVal - picker.getMinValue()], Toast.LENGTH_SHORT) .show(); }