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