/** * 浣胯疆瀛愬け鏁� * * @param clearCaches if true then cached views will be clear */ public void invalidateWheel(boolean clearCaches) { if (clearCaches) { recycle.clearAll(); if (itemsLayout != null) { itemsLayout.removeAllViews(); } scrollingOffset = 0; } else if (itemsLayout != null) { // cache all items recycle.recycleItems(itemsLayout, firstItem, new ItemsRange()); } invalidate(); }
/** * 杩斿洖鐗瑰畾鐨刬tem * * @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(recycle.getEmptyItem(), itemsLayout); } else { while (index < 0) { index = count + index; } } index %= count; return viewAdapter.getItem(index, recycle.getItem(), itemsLayout); }
/** 閲嶅缓鏂扮殑璁$畻鏂规硶 */ private void buildViewForMeasuring() { // clear all items if (itemsLayout != null) { recycle.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; } } }
/** * 濡傛灉鏈夊繀瑕侀噸鏂伴噸寤鸿疆瀛愮殑item鍜岀紦瀛樻病鏈夌敤鐨刬tem * * @return true if items are rebuilt */ private boolean rebuildItems() { boolean updated = false; ItemsRange range = getItemsRange(); if (itemsLayout != null) { int first = recycle.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; }