/** * Invalidates items layout * * @param clearCaches if true then cached views will be cleared */ public void invalidateItemsLayout(boolean clearCaches) { if (clearCaches) { mRecycler.clearAll(); if (mItemsLayout != null) { mItemsLayout.removeAllViews(); } mScrollingOffset = 0; } else if (mItemsLayout != null) { // cache all items mRecycler.recycleItems(mItemsLayout, mFirstItemIdx, new ItemsRange()); } invalidate(); }
/** * 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 (mViewAdapter == null || mViewAdapter.getItemsCount() == 0) { return null; } int count = mViewAdapter.getItemsCount(); if (!isValidItemIndex(index)) { return mViewAdapter.getEmptyItem(mRecycler.getEmptyItem(), mItemsLayout); } else { while (index < 0) { index = count + index; } } index %= count; return mViewAdapter.getItem(index, mRecycler.getItem(), mItemsLayout); }
/** * Rebuilds spinnerwheel items if necessary. Caches all unused items. * * @return true if items are rebuilt */ protected boolean rebuildItems() { boolean updated; ItemsRange range = getItemsRange(); if (mItemsLayout != null) { int first = mRecycler.recycleItems(mItemsLayout, mFirstItemIdx, range); updated = mFirstItemIdx != first; mFirstItemIdx = first; } else { createItemsLayout(); updated = true; } if (!updated) { updated = mFirstItemIdx != range.getFirst() || mItemsLayout.getChildCount() != range.getCount(); } if (mFirstItemIdx > range.getFirst() && mFirstItemIdx <= range.getLast()) { for (int i = mFirstItemIdx - 1; i >= range.getFirst(); i--) { if (!addItemView(i, true)) { break; } mFirstItemIdx = i; } } else { mFirstItemIdx = range.getFirst(); } int first = mFirstItemIdx; for (int i = mItemsLayout.getChildCount(); i < range.getCount(); i++) { if (!addItemView(mFirstItemIdx + i, false) && mItemsLayout.getChildCount() == 0) { first++; } } mFirstItemIdx = first; return updated; }