日期滚轮选择器

日期滚轮选择器_第1张图片

1.自定义有个wheelview 

r(OnWheelScrollListener listener) {
      scrollingListeners.add(listener);
   }

   /**
    * Removes wheel scrolling listener
    * @param listener the listener
    */
   public void removeScrollingListener(OnWheelScrollListener listener) {
      scrollingListeners.remove(listener);
   }

   /**
    * Notifies listeners about starting scrolling
    */
   protected void notifyScrollingListenersAboutStart() {
      for (OnWheelScrollListener listener : scrollingListeners) {
         listener.onScrollingStarted(this);
      }
   }

   /**
    * Notifies listeners about ending scrolling
    */
   protected void notifyScrollingListenersAboutEnd() {
      for (OnWheelScrollListener listener : scrollingListeners) {
         listener.onScrollingFinished(this);
      }
   }

   /**
    * Adds wheel clicking listener
    * @param listener the listener
    */
   public void addClickingListener(OnWheelClickedListener listener) {
      clickingListeners.add(listener);
   }

   /**
    * Removes wheel clicking listener
    * @param listener the listener
    */
   public void removeClickingListener(OnWheelClickedListener listener) {
      clickingListeners.remove(listener);
   }

   /**
    * Notifies listeners about clicking
    */
   protected void notifyClickListenersAboutClick(int item) {
      for (OnWheelClickedListener listener : clickingListeners)
         listener.onItemClicked(this, item);
   }

   /**
    * Gets current value
    * 
    * @return the current value
    */
   public int getCurrentItem() {
      return currentItem;
   }

   /**
    * Sets the current item. Does nothing when index is wrong.
    * 
    * @param index the item index
    * @param animated the animation flag
    */
   public void setCurrentItem(int index, boolean animated) {
      if (viewAdapter == null || viewAdapter.getItemsCount() == 0) {
         return; // throw?
      }

      int itemCount = viewAdapter.getItemsCount();
      if (index < 0 || index >= itemCount) {
         if (isCyclic) {
            while (index < 0) {
               index += itemCount;
            }
            index %= itemCount;
         } else{
            return; // throw?
         }
      }
      if (index != currentItem) {
         if (animated) {
            int itemsToScroll = index - currentItem;
            if (isCyclic) {
               int scroll = itemCount + Math.min(index, currentItem) - Math.max(index, currentItem);
               if (scroll < Math.abs(itemsToScroll)) {
                  itemsToScroll = itemsToScroll < 0 ? scroll : -scroll;
               }
            }
            scroll(itemsToScroll, 0);
         } else {
            scrollingOffset = 0;

            int old = currentItem;
            currentItem = index;

            notifyChangingListeners(old, currentItem);

            invalidate();
         }
      }
   }

   /**
    * Sets the current item w/o animation. Does nothing when index is wrong.
    * 
    * @param index the item index
    */
   public void setCurrentItem(int index) {
      setCurrentItem(index, false);
   }

   /**
    * Tests if wheel is cyclic. That means before the 1st item there is shown the last one
    * @return true if wheel is cyclic
    */
   public boolean isCyclic() {
      return isCyclic;
   }

   /**
    * Set wheel cyclic flag
    * @param isCyclic the flag to set
    */
   public void setCyclic(boolean isCyclic) {
      this.isCyclic = isCyclic;
      invalidateWheel(false);
   }

   /**
    * Determine whether shadows are drawn
    * @return true is shadows are drawn
    */
   public boolean drawShadows() {
      return drawShadows;
   }

   /**
    * Set whether shadows should be drawn
    * @param drawShadows flag as true or false
    */
   public void setDrawShadows(boolean drawShadows) {
      this.drawShadows = drawShadows;
   }

   /**
    * Set the shadow gradient color
    * @param start
    * @param middle
    * @param end
    */
   public void setShadowColor(int start, int middle, int end) {
      SHADOWS_COLORS = new int[] {start, middle, end};
   }

   /**
    * Sets the drawable for the wheel background
    * @param resource
    */
   public void setWheelBackground(int resource) {
      wheelBackground = resource;
      setBackgroundResource(wheelBackground);
   }

   /**
    * Sets the drawable for the wheel foreground
    * @param resource
    */
   public void setWheelForeground(int resource) {
      wheelForeground = resource;
      centerDrawable = getContext().getResources().getDrawable(wheelForeground);
   }

   /**
    * Invalidates wheel
    * @param clearCaches if true then cached views will be clear
    */
   public void invalidateWheel(boolean clearCaches) {
      if (clearCaches) {
         mRecycle.clearAll();
         if (itemsLayout != null) {
            itemsLayout.removeAllViews();
         }
         scrollingOffset = 0;
      } else if (itemsLayout != null) {
         // cache all items
         mRecycle.recycleItems(itemsLayout, firstItem, new ItemsRange());
      }

      invalidate();
   }

   /**
    * Initializes resources
    */
   private void initResourcesIfNecessary() {
      if (centerDrawable == null) {
         centerDrawable = getContext().getResources().getDrawable(wheelForeground);
      }

      if (topShadow == null) {
         topShadow = new GradientDrawable(Orientation.TOP_BOTTOM, SHADOWS_COLORS);
      }

      if (bottomShadow == null) {
         bottomShadow = new GradientDrawable(Orientation.BOTTOM_TOP, SHADOWS_COLORS);
      }

      setBackgroundResource(wheelBackground);
   }

   /**
    * Calculates desired height for layout
    * 
    * @param layout
    *            the source layout
    * @return the desired layout height
    */
   private int getDesiredHeight(LinearLayout layout) {
      if (layout != null && layout.getChildAt(0) != null) {
         itemHeight = layout.getChildAt(0).getMeasuredHeight();
      }

      int desired = itemHeight * visibleItems - itemHeight * ITEM_OFFSET_PERCENT / 50;

      return Math.max(desired, getSuggestedMinimumHeight());
   }

   /**
    * Returns height of wheel item
    * @return the item height
    */
   private int getItemHeight() {
      if (itemHeight != 0) {
         return itemHeight;
      }

      if (itemsLayout != null && itemsLayout.getChildAt(0) != null) {
         itemHeight = itemsLayout.getChildAt(0).getHeight();
         return itemHeight;
      }

      return getHeight() / visibleItems;
   }

   /**
    * Calculates control width and creates text layouts
    * @param widthSize the input layout width
    * @param mode the layout mode
    * @return the calculated control width
    */
   private int calculateLayoutWidth(int widthSize, int mode) {
      initResourcesIfNecessary();

      // TODO: make it static
      itemsLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      itemsLayout.measure(MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
      int width = itemsLayout.getMeasuredWidth();

      if (mode == MeasureSpec.EXACTLY) {
         width = widthSize;
      } else {
         width += 2 * PADDING;

         // Check against our minimum width
         width = Math.max(width, getSuggestedMinimumWidth());

         if (mode == MeasureSpec.AT_MOST && widthSize < width) {
            width = widthSize;
         }
      }

      itemsLayout.measure(MeasureSpec.makeMeasureSpec(width - 2 * PADDING, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

      return width;
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      int widthMode = MeasureSpec.getMode(widthMeasureSpec);
      int heightMode = MeasureSpec.getMode(heightMeasureSpec);
      int widthSize = MeasureSpec.getSize(widthMeasureSpec);
      int heightSize = MeasureSpec.getSize(heightMeasureSpec);

      buildViewForMeasuring();

      int width = calculateLayoutWidth(widthSize, widthMode);

      int height;
      if (heightMode == MeasureSpec.EXACTLY) {
         height = heightSize;
      } else {
         height = getDesiredHeight(itemsLayout);

         if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(height, heightSize);
         }
      }

      setMeasuredDimension(width, height);
   }

   @Override
   protected void onLayout(boolean changed, int l, int t, int r, int b) {
      layout(r - l, b - t);
   }

   /**
    * Sets layouts width and height
    * @param width the layout width
    * @param height the layout height
    */
   private void layout(int width, int height) {
      int itemsWidth = width - 2 * PADDING;

      itemsLayout.layout(0, 0, itemsWidth, height);
   }

   @Override
   protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);

      if (viewAdapter != null && viewAdapter.getItemsCount() > 0) {
         updateView();

         drawItems(canvas);
         drawCenterRect(canvas);
      }

      if (drawShadows) drawShadows(canvas);
   }

   /**
    * Draws shadows on top and bottom of control
    * @param canvas the canvas for drawing
    */
   private void drawShadows(Canvas canvas) {
      /*/ Modified by wulianghuan 2014-11-25
      int height = (int)(1.5 * getItemHeight());
      //*/
      int height = (int)(2 * getItemHeight());
      //*/
      //topShadow.setBounds(0, 0, getWidth(), height);
      topShadow.setBounds(0, 0, getWidth(), getHeight());
      topShadow.draw(canvas);

      //bottomShadow.setBounds(0, getHeight() - height, getWidth(), getHeight());
      bottomShadow.setBounds(0, 0, getWidth(), getHeight());
      bottomShadow.draw(canvas);
   }

   /**
    * Draws items
    * @param canvas the canvas for drawing
    */
   private void drawItems(Canvas canvas) {
      canvas.save();

      int top = (currentItem - firstItem) * getItemHeight() + (getItemHeight() - getHeight()) / 2;
      canvas.translate(PADDING, - top + scrollingOffset);

      itemsLayout.draw(canvas);

      canvas.restore();
   }

   /**
    * Draws rect for current value
    * @param canvas the canvas for drawing
    */
   private void drawCenterRect(Canvas canvas) {
      int center = getHeight() / 2;
      int offset = (int) (getItemHeight() / 2 * 1.2);
//    int offset = 60;
      /*/ Remarked by wulianghuan 2014-11-27  使用自己的画线,而不是描边
      Rect rect = new Rect(left, top, right, bottom)
      centerDrawable.setBounds(bounds)
      centerDrawable.setBounds(0, center - offset, getWidth(), center + offset);
      centerDrawable.draw(canvas);
      //*/
      Paint paint = new Paint();
      paint.setColor(getResources().getColor(R.color.province_line_border));
      // 设置线宽
//    paint.setStrokeWidth((float) 3);
      paint.setStrokeWidth((float) 2);
      // 绘制上边直线
      canvas.drawLine(0, center - offset, getWidth(), center - offset, paint);
      // 绘制下边直线
      canvas.drawLine(0, center + offset, getWidth(), center + offset, paint);
      //*/
   }

   @Override
   public boolean onTouchEvent(MotionEvent event) {
      if (!isEnabled() || getViewAdapter() == null) {
         return true;
      }

      switch (event.getAction()) {
         case MotionEvent.ACTION_MOVE:
            if (getParent() != null) {
               getParent().requestDisallowInterceptTouchEvent(true);
            }
            break;

         case MotionEvent.ACTION_UP:
            if (!isScrollingPerformed) {
               int distance = (int) event.getY() - getHeight() / 2;
               if (distance > 0) {
                  distance += getItemHeight() / 2;
               } else {
                  distance -= getItemHeight() / 2;
               }
               int items = distance / getItemHeight();
               if (items != 0 && isValidItemIndex(currentItem + items)) {
                  notifyClickListenersAboutClick(currentItem + items);
               }
            }
            break;
      }

      return scroller.onTouchEvent(event);
   }

   /**
    * Scrolls the wheel
    * @param delta the scrolling value
    */
   private void doScroll(int delta) {
      scrollingOffset += delta;

      int itemHeight = getItemHeight();
      int count = scrollingOffset / itemHeight;

      int pos = currentItem - count;
      int itemCount = viewAdapter.getItemsCount();

      int fixPos = scrollingOffset % itemHeight;
      if (Math.abs(fixPos) <= itemHeight / 2) {
         fixPos = 0;
      }
      if (isCyclic && itemCount > 0) {
         if (fixPos > 0) {
            pos--;
            count++;
         } else if (fixPos < 0) {
            pos++;
            count--;
         }
         // fix position by rotating
         while (pos < 0) {
            pos += itemCount;
         }
         pos %= itemCount;
      } else {
         //
         if (pos < 0) {
            count = currentItem;
            pos = 0;
         } else if (pos >= itemCount) {
            count = currentItem - itemCount + 1;
            pos = itemCount - 1;
         } else if (pos > 0 && fixPos > 0) {
            pos--;
            count++;
         } else if (pos < itemCount - 1 && fixPos < 0) {
            pos++;
            count--;
         }
      }

      int offset = scrollingOffset;
      if (pos != currentItem) {
         setCurrentItem(pos, false);
      } else {
         invalidate();
      }

      // update offset
      scrollingOffset = offset - count * itemHeight;
      if (scrollingOffset > getHeight()) {
         scrollingOffset = scrollingOffset % getHeight() + getHeight();
      }
   }

   /**
    * Scroll the wheel
    * @param itemsToScroll items to scroll
    * @param time scrolling duration
    */
   public void scroll(int itemsToScroll, int time) {
      int distance = itemsToScroll * getItemHeight() - scrollingOffset;
      scroller.scroll(distance, time);
   }

   /**
    * Calculates range for wheel items
    * @return the items range
    */
   private ItemsRange getItemsRange() {
      if (getItemHeight() == 0) {
         return null;
      }

      int first = currentItem;
      int count = 1;

      while (count * getItemHeight() < getHeight()) {
         first--;
         count += 2; // top + bottom items
      }

      if (scrollingOffset != 0) {
         if (scrollingOffset > 0) {
            first--;
         }
         count++;

         // process empty items above the first or below the second
         int emptyItems = scrollingOffset / getItemHeight();
         first -= emptyItems;
         count += Math.asin(emptyItems);
      }
      return new ItemsRange(first, count);
   }

   /**
    * Rebuilds wheel items if necessary. Caches all unused items.
    * 
    * @return true if items are rebuilt
    */
   private boolean rebuildItems() {
      boolean updated = false;
      ItemsRange range = getItemsRange();
      if (itemsLayout != null) {
         int first = mRecycle.recycleItems(itemsLayout, firstItem, range);
         updated = firstItem != first;
         firstItem = first;
      } else {
         createItemsLayout();
         updated = true;
      }

      if (!updated) {
         updated = firstItem != range.getFirst() || itemsLayout.getChildCount() != range.getCount();
      }

      if (firstItem > range.getFirst() && firstItem <= range.getLast()) {
         for (int i = firstItem - 1; i >= range.getFirst(); i--) {
            if (!addViewItem(i, true)) {
               break;
            }
            firstItem = i;
         }
      } else {
         firstItem = range.getFirst();
      }

      int first = firstItem;
      for (int i = itemsLayout.getChildCount(); i < range.getCount(); i++) {
         if (!addViewItem(firstItem + i, false) && itemsLayout.getChildCount() == 0) {
            first++;
         }
      }
      firstItem = first;

      return updated;
   }

   /**
    * Updates view. Rebuilds items and label if necessary, recalculate items sizes.
    */
   private void updateView() {
      if (rebuildItems()) {
         calculateLayoutWidth(getWidth(), MeasureSpec.EXACTLY);
         layout(getWidth(), getHeight());
      }
   }

   /**
    * Creates item layouts if necessary
    */
   private void createItemsLayout() {
      if (itemsLayout == null) {
         itemsLayout = new LinearLayout(getContext());
         itemsLayout.setOrientation(LinearLayout.VERTICAL);
      }
   }

   /**
    * Builds view for measuring
    */
   private void buildViewForMeasuring() {
      // clear all items
      if (itemsLayout != null) {
         mRecycle.recycleItems(itemsLayout, firstItem, new ItemsRange());
      } else {
         createItemsLayout();
      }

      // add views
      int addItems = visibleItems / 2;
      for (int i = currentItem + addItems; i >= currentItem - addItems; i--) {
         if (addViewItem(i, true)) {
            firstItem = i;
         }
      }
   }

   /**
    * Adds view for item to items layout
    * @param index the item index
    * @param first the flag indicates if view should be first
    * @return true if corresponding item exists and is added
    */
   private boolean addViewItem(int index, boolean first) {
      View view = getItemView(index);
      if (view != null) {
         if (first) {
            itemsLayout.addView(view, 0);
         } else {
            itemsLayout.addView(view);
         }

         return true;
      }

      return false;
   }

   /**
    * Checks whether intem index is valid
    * @param index the item index
    * @return true if item index is not out of bounds or the wheel is cyclic
    */
   private boolean isValidItemIndex(int index) {
      return viewAdapter != null && viewAdapter.getItemsCount() > 0 &&
            (isCyclic || index >= 0 && index < viewAdapter.getItemsCount());
   }

   /**
    * Returns view for specified item
    * @param index the item index
    * @return item view or empty view if index is out of bounds
    */
   private View getItemView(int index) {
      if (viewAdapter == null || viewAdapter.getItemsCount() == 0) {
         return null;
      }
      int count = viewAdapter.getItemsCount();
      if (!isValidItemIndex(index)) {
         return viewAdapter.getEmptyItem(mRecycle.getEmptyItem(), itemsLayout);
      } else {
         while (index < 0) {
            index = count + index;
         }
      }

      index %= count;
      return viewAdapter.getItem(index, mRecycle.getItem(), itemsLayout);
   }

   /**
    * Stops scrolling
    */
   public void stopScrolling() {
      scroller.stopScrolling();
   }
}

 

2.

public interface WheelViewAdapter {
   
   /**
    * Gets items count
    * @return the count of wheel items
    */
   public int getItemsCount();
   
   /**
    * Get a View that displays the data at the specified position in the data set
    * 
    * @param index the item index
    * @param convertView the old view to reuse if possible
    * @param parent the parent that this view will eventually be attached to
    * @return the wheel item View
    */
   public View getItem(int index, View convertView, ViewGroup parent);

   /**
    * Get a View that displays an empty wheel item placed before the first or after
    * the last wheel item.
    * 
    * @param convertView the old view to reuse if possible
     * @param parent the parent that this view will eventually be attached to
    * @return the empty item View
    */
   public View getEmptyItem(View convertView, ViewGroup parent);
   
   /**
    * Register an observer that is called when changes happen to the data used by this adapter.
    * @param observer the observer to be registered
    */
   public void registerDataSetObserver(DataSetObserver observer);
   
   /**
    * Unregister an observer that has previously been registered
    * @param observer the observer to be unregistered
    */
   void unregisterDataSetObserver(DataSetObserver observer);
   
}

3.

public class WheelScroller {
    /**
     * Scrolling listener interface
     */
    public interface ScrollingListener {
        /**
         * Scrolling callback called when scrolling is performed.
         *
         * @param distance the distance to scroll
         */
        void onScroll(int distance);

        /**
         * Starting callback called when scrolling is started
         */
        void onStarted();

        /**
         * Finishing callback called after justifying
         */
        void onFinished();

        /**
         * Justifying callback called to justify a view when scrolling is ended
         */
        void onJustify();
    }

    /**
     * Scrolling duration
     */
    private static final int SCROLLING_DURATION = 400;

    /**
     * Minimum delta for scrolling
     */
    public static final int MIN_DELTA_FOR_SCROLLING = 1;

    // Listener
    private ScrollingListener listener;

    // Context
    private Context context;

    // Scrolling
    private GestureDetector gestureDetector;
    private Scroller scroller;
    private int lastScrollY;
    private float lastTouchedY;
    private boolean isScrollingPerformed;

    /**
     * Constructor
     *
     * @param context  the current context
     * @param listener the scrolling listener
     */
    public WheelScroller(Context context, ScrollingListener listener) {
        gestureDetector = new GestureDetector(context, gestureListener);
        gestureDetector.setIsLongpressEnabled(false);

        scroller = new Scroller(context);

        this.listener = listener;
        this.context = context;
    }

    /**
     * Set the the specified scrolling interpolator
     *
     * @param interpolator the interpolator
     */
    public void setInterpolator(Interpolator interpolator) {
        scroller.forceFinished(true);
        scroller = new Scroller(context, interpolator);
    }

    /**
     * Scroll the wheel
     *
     * @param distance the scrolling distance
     * @param time     the scrolling duration
     */
    public void scroll(int distance, int time) {
        scroller.forceFinished(true);

        lastScrollY = 0;

        scroller.startScroll(0, 0, 0, distance, time != 0 ? time : SCROLLING_DURATION);
        setNextMessage(MESSAGE_SCROLL);

        startScrolling();
    }

    /**
     * Stops scrolling
     */
    public void stopScrolling() {
        scroller.forceFinished(true);
    }

    /**
     * Handles Touch event
     *
     * @param event the motion event
     * @return
     */
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastTouchedY = event.getY();
                scroller.forceFinished(true);
                clearMessages();
                break;

            case MotionEvent.ACTION_MOVE:
                // perform scrolling
                int distanceY = (int) (event.getY() - lastTouchedY);
                if (distanceY != 0) {
                    startScrolling();
                    listener.onScroll(distanceY);
                    lastTouchedY = event.getY();
                }
                break;
        }

        if (!gestureDetector.onTouchEvent(event) && event.getAction() == MotionEvent.ACTION_UP) {
            justify();
        }

        return true;
    }

    // gesture listener
    private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // Do scrolling in onTouchEvent() since onScroll() are not call immediately
            //  when user touch and move the wheel
            return true;
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            lastScrollY = 0;
            final int maxY = 0x7FFFFFFF;
            final int minY = -maxY;
            scroller.fling(0, lastScrollY, 0, (int) -velocityY, 0, 0, minY, maxY);
            setNextMessage(MESSAGE_SCROLL);
            return true;
        }
    };

    // Messages
    private final int MESSAGE_SCROLL = 0;
    private final int MESSAGE_JUSTIFY = 1;

    /**
     * Set next message to queue. Clears queue before.
     *
     * @param message the message to set
     */
    private void setNextMessage(int message) {
        clearMessages();
        animationHandler.sendEmptyMessage(message);
    }

    /**
     * Clears messages from queue
     */
    private void clearMessages() {
        animationHandler.removeMessages(MESSAGE_SCROLL);
        animationHandler.removeMessages(MESSAGE_JUSTIFY);
    }

    // animation handler
    private Handler animationHandler = new Handler() {
        public void handleMessage(Message msg) {
            scroller.computeScrollOffset();
            int currY = scroller.getCurrY();
            int delta = lastScrollY - currY;
            lastScrollY = currY;
            if (delta != 0) {
                listener.onScroll(delta);
            }

            // scrolling is not finished when it comes to final Y
            // so, finish it manually 
            if (Math.abs(currY - scroller.getFinalY()) < MIN_DELTA_FOR_SCROLLING) {
                currY = scroller.getFinalY();
                scroller.forceFinished(true);
            }
            if (!scroller.isFinished()) {
                animationHandler.sendEmptyMessage(msg.what);
            } else if (msg.what == MESSAGE_SCROLL) {
                justify();
            } else {
                finishScrolling();
            }
        }
    };

    /**
     * Justifies wheel
     */
    private void justify() {
        listener.onJustify();
        setNextMessage(MESSAGE_JUSTIFY);
    }

    /**
     * Starts scrolling
     */
    private void startScrolling() {
        if (!isScrollingPerformed) {
            isScrollingPerformed = true;
            listener.onStarted();
        }
    }

    /**
     * Finishes scrolling
     */
    void finishScrolling() {
        if (isScrollingPerformed) {
            listener.onFinished();
            isScrollingPerformed = false;
        }
    }
}

4.

public class WheelRecycle {
   // Cached items
   private List items;
   
   // Cached empty items
   private List emptyItems;
   
   // Wheel view
   private WheelView wheel;
   
   /**
    * Constructor
    * @param wheel the wheel view
    */
   public WheelRecycle(WheelView wheel) {
      this.wheel = wheel;
   }

   /**
    * Recycles items from specified layout.
    * There are saved only items not included to specified range.
    * All the cached items are removed from original layout.
    * 
    * @param layout the layout containing items to be cached
    * @param firstItem the number of first item in layout
    * @param range the range of current wheel items 
    * @return the new value of first item number
    */
   public int recycleItems(LinearLayout layout, int firstItem, ItemsRange range) {
      int index = firstItem;
      for (int i = 0; i < layout.getChildCount();) {
         if (!range.contains(index)) {
            recycleView(layout.getChildAt(i), index);
            layout.removeViewAt(i);
            if (i == 0) { // first item
               firstItem++;
            }
         } else {
            i++; // go to next item
         }
         index++;
      }
      return firstItem;
   }
   
   /**
    * Gets item view
    * @return the cached view
    */
   public View getItem() {
      return getCachedView(items);
   }

   /**
    * Gets empty item view
    * @return the cached empty view
    */
   public View getEmptyItem() {
      return getCachedView(emptyItems);
   }
   
   /**
    * Clears all views 
    */
   public void clearAll() {
      if (items != null) {
         items.clear();
      }
      if (emptyItems != null) {
         emptyItems.clear();
      }
   }

   /**
    * Adds view to specified cache. Creates a cache list if it is null.
    * @param view the view to be cached
    * @param cache the cache list
    * @return the cache list
    */
   private List addView(View view, List cache) {
      if (cache == null) {
         cache = new LinkedList();
      }
      
      cache.add(view);
      return cache;
   }

   /**
    * Adds view to cache. Determines view type (item view or empty one) by index.
    * @param view the view to be cached
    * @param index the index of view
    */
   private void recycleView(View view, int index) {
      int count = wheel.getViewAdapter().getItemsCount();

      if ((index < 0 || index >= count) && !wheel.isCyclic()) {
         // empty view
         emptyItems = addView(view, emptyItems);
      } else {
         while (index < 0) {
            index = count + index;
         }
         index %= count;
         items = addView(view, items);
      }
   }
   
   /**
    * Gets view from specified cache.
    * @param cache the cache
    * @return the first view from cache.
    */
   private View getCachedView(List cache) {
      if (cache != null && cache.size() > 0) {
         View view = cache.get(0);
         cache.remove(0);
         return view;
      }
      return null;
   }

}

5.

ingFinished(WheelView wheel) {
                // TODO Auto-generated method stub
                String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, mYearAdapter);
            }
        });

        wvMonth.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
                selectMonth = currentText;
                setTextviewSize(currentText, mMonthAdapter);
                setMonth(currentText.substring(0, 1));
                initDays(Integer.parseInt(day));
                mDaydapter = new CalendarTextAdapter(context, arry_days, 0, maxTextSize, minTextSize);
                wvDay.setVisibleItems(5);
                wvDay.setViewAdapter(mDaydapter);

                if (selectYear.equals(getYear() + "年") && selectMonth.equals(getMonth() + "月")) {
                    wvDay.setCurrentItem(Calendar.getInstance().get(Calendar.DAY_OF_MONTH) - 1);
                } else {
                    wvDay.setCurrentItem(0);
                }


                calDays(currentYear, month);
            }
        });

        wvMonth.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                // TODO Auto-generated method stub
                String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, mMonthAdapter);
            }
        });

        wvDay.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                // TODO Auto-generated method stub
                String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, mDaydapter);
                selectDay = currentText;
            }
        });

        wvDay.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                // TODO Auto-generated method stub
                String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());
                setTextviewSize(currentText, mDaydapter);
            }
        });
    }


    public void initYears() {
        for (int i = Integer.parseInt(getYear()); i > 1989; i--) {
            arry_years.add(i + "年");
        }
    }

    public void initMonths(int months) {
        arry_months.clear();
        for (int i = 1; i <= months; i++) {
            arry_months.add(i + "月");
        }
    }

    public void initDays(int days) {
        arry_days.clear();
        if (getYear().equals(currentYear) && selectMonth.equals(currentMonth+"月")) {
            for (int i = 1; i <= Calendar.getInstance().get(Calendar.DAY_OF_MONTH); i++) {
                arry_days.add(i + "日");
            }
        } else {
            for (int i = 1; i <= days; i++) {
                arry_days.add(i + "日");
            }
        }


    }

    private class CalendarTextAdapter extends AbstractWheelTextAdapter {
        ArrayList list;

        protected CalendarTextAdapter(Context context, ArrayList list, int currentItem, int maxsize, int minsize) {
            super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
            this.list = list;
            setItemTextResource(R.id.tempValue);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            return view;
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index) + "";
        }
    }

    public void setBirthdayListener(OnBirthListener onBirthListener) {
        this.onBirthListener = onBirthListener;
    }

    @Override
    public void onClick(View v) {

        if (v == btnSure) {
            if (onBirthListener != null) {
                onBirthListener.onClick(selectYear, selectMonth, selectDay);
                Log.d("cy", "" + selectYear + "" + selectMonth + "" + selectDay);
            }
        } else if (v == btnSure) {

        } else {
            dismiss();
        }
        dismiss();

    }

    public interface OnBirthListener {
        public void onClick(String year, String month, String day);
    }

    /**
     * 设置字体大小
     *
     * @param curriteItemText
     * @param adapter
     */
    public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) {
        ArrayList arrayList = adapter.getTestViews();
        int size = arrayList.size();
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            currentText = textvew.getText().toString();
            if (curriteItemText.equals(currentText)) {
                textvew.setTextSize(maxTextSize);
            } else {
                textvew.setTextSize(minTextSize);
            }
        }
    }

    public String getYear() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR) + "";
    }

    public String getMonth() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH) + 1 + "";
    }

    public String getDay() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DAY_OF_MONTH) + "";
    }

    public void initData() {
        setDate(getYear(), getMonth(), getDay());
        this.currentDay = 1 + "";
        this.currentMonth = 1 + "";
    }

    /**
     * 设置年月日
     *
     * @param year
     * @param month
     * @param day
     */
    public void setDate(String year, String month, String day) {
        selectYear = year + "年";
        selectMonth = month + "月";
        selectDay = day + "日";
        issetdata = true;
        this.currentYear = year;
        this.currentMonth = month;
        this.currentDay = day;
        if (year == getYear()) {
            this.month = getMonth();
        } else {
            this.month = 12 + "";
        }
        calDays(year, month);
    }

    /**
     * 设置年份
     *
     * @param year
     */
    public int setYear(String year) {
        int yearIndex = 0;
        if (!year.equals(getYear())) {
            this.month = 12 + "";
        } else {
            this.month = getMonth();
        }
        for (int i = Integer.parseInt(getYear()); i > 1989; i--) {
            if (i == Integer.parseInt(year)) {
                return yearIndex;
            }
            yearIndex++;
        }
        return yearIndex;
    }

    /**
     * 设置月份
     *
     * @param month
     * @param month
     * @return
     */
    public int setMonth(String month) {
        int monthIndex = 0;
        calDays(currentYear, month);
        for (int i = 1; i < Integer.parseInt(this.month); i++) {
            if (Integer.parseInt(month) == i) {
                return monthIndex;
            } else {
                monthIndex++;
            }
        }
        return monthIndex;
    }

    /**
     * 计算每月多少天
     *
     * @param month
     * @param year
     */
    public void calDays(String year, String month) {
        boolean leayyear = false;
        if (Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0) {
            leayyear = true;
        } else {
            leayyear = false;
        }
        for (int i = 1; i <= 12; i++) {
            switch (Integer.parseInt(month)) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    this.day = 31 + "";

                    break;
                case 2:
                    if (leayyear) {
                        this.day = 29 + "";
                    } else {
                        this.day = 28 + "";
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    this.day = 30 + "";
                    break;
            }
        }
        if (year.equals(getYear()) && month.equals(getMonth())) {
            this.day = getDay();
        }
    }


}

 

 

 

你可能感兴趣的:(日期滚轮选择器)