Skip to content

Commit

Permalink
Add animation to "setSelectedItemPosition" method
Browse files Browse the repository at this point in the history
  • Loading branch information
nimatrueway committed Apr 15, 2017
1 parent 99276c2 commit 9f5a2f8
Showing 1 changed file with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public class WheelPicker extends View implements IDebug, IWheelPicker, Runnable
private Paint mPaint;
private Scroller mScroller;
private VelocityTracker mTracker;
/**
* Determines whether the current scrolling animation is triggered by touchEvent or setSelectedItemPosition.
* User added eventListeners will only be fired after touchEvents.
*/
private boolean isTouchTriggered;

/**
* 相关监听器
Expand Down Expand Up @@ -676,6 +681,7 @@ private int computeDepth(int degree) {
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isTouchTriggered = true;
if (null != getParent())
getParent().requestDisallowInterceptTouchEvent(true);
if (null == mTracker)
Expand Down Expand Up @@ -775,9 +781,9 @@ public void run() {
if (isDebug)
Log.i(TAG, position + ":" + mData.get(position) + ":" + mScrollOffsetY);
mCurrentItemPosition = position;
if (null != mOnItemSelectedListener)
if (null != mOnItemSelectedListener && isTouchTriggered)
mOnItemSelectedListener.onItemSelected(this, mData.get(position), position);
if (null != mOnWheelChangeListener) {
if (null != mOnWheelChangeListener && isTouchTriggered) {
mOnWheelChangeListener.onWheelSelected(position);
mOnWheelChangeListener.onWheelScrollStateChanged(SCROLL_STATE_IDLE);
}
Expand Down Expand Up @@ -832,6 +838,24 @@ public int getSelectedItemPosition() {

@Override
public void setSelectedItemPosition(int position) {
setSelectedItemPosition(position, true);
}

public void setSelectedItemPosition(int position, final boolean animated) {
isTouchTriggered = false;
if (animated && mScroller.isFinished()) { // We go non-animated regardless of "animated" parameter if scroller is in motion
int length = getData().size();
int itemDifference = position - mCurrentItemPosition;
if (itemDifference == 0)
return;
if (isCyclic && Math.abs(itemDifference) > (length / 2)) { // Find the shortest path if it's cyclic
itemDifference += (itemDifference > 0) ? -length : length;
}
mScroller.startScroll(0, mScroller.getCurrY(), 0, (-itemDifference) * mItemHeight);
mHandler.post(this);
} else {
if (!mScroller.isFinished())
mScroller.abortAnimation();
position = Math.min(position, mData.size() - 1);
position = Math.max(position, 0);
mSelectedItemPosition = position;
Expand All @@ -840,6 +864,7 @@ public void setSelectedItemPosition(int position) {
computeFlingLimitY();
requestLayout();
invalidate();
}
}

@Override
Expand Down

1 comment on commit 9f5a2f8

@yeltayev22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this update, the initial setting the date to the current date doesn't work, as here itemHeight will be 0 and will not scroll to desired position.

Please sign in to comment.