日期滚轮选择器

本文介绍了如何创建并使用一个自定义的日期滚轮选择器,详细讲解了滚轮视图的实现过程,包括各个步骤和关键代码,帮助读者理解如何在应用中实现日期选择功能。
摘要由CSDN通过智能技术生成

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
   p
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值