@Test public void cacheSpanIndices() throws Throwable { final RecyclerView rv = setupBasic(new Config(3, 100)); mGlm.mSpanSizeLookup.setSpanIndexCacheEnabled(true); waitForFirstLayout(rv); GridLayoutManager.SpanSizeLookup ssl = mGlm.mSpanSizeLookup; assertTrue("cache should be filled", mGlm.mSpanSizeLookup.mSpanIndexCache.size() > 0); assertEquals("item index 5 should be in span 2", 2, getLp(mGlm.findViewByPosition(5)).getSpanIndex()); mGlm.expectLayout(2); mAdapter.mFullSpanItems.add(4); mAdapter.changeAndNotify(4, 1); mGlm.waitForLayout(2); assertEquals("item index 5 should be in span 2", 0, getLp(mGlm.findViewByPosition(5)).getSpanIndex()); }
private int getSpanIndex(RecyclerView.Recycler recycler, RecyclerView.State state, int pos) { if (!state.isPreLayout()) { return mSpanSizeLookup.getCachedSpanIndex(pos, mSpanCount); } final int cached = mPreLayoutSpanIndexCache.get(pos, -1); if (cached != -1) { return cached; } final int adapterPosition = recycler.convertPreLayoutPositionToPostLayout(pos); if (adapterPosition == -1) { if (DEBUG) { throw new RuntimeException("Cannot find span index for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); } Log.w(TAG, "Cannot find span size for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); return 0; } return mSpanSizeLookup.getCachedSpanIndex(adapterPosition, mSpanCount); }
GridLayoutManager extends LinearLayoutManager { private int getSpanIndex(RecyclerView.Recycler recycler, RecyclerView.State state, int pos) { if (!state.isPreLayout()) { return mSpanSizeLookup.getCachedSpanIndex(pos, mSpanCount); } final int cached = mPreLayoutSpanIndexCache.get(pos, -1); if (cached != -1) { return cached; } final int adapterPosition = recycler.convertPreLayoutPositionToPostLayout(pos); if (adapterPosition == -1) { if (DEBUG) { throw new RuntimeException("Cannot find span index for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); } Log.w(TAG, "Cannot find span size for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); return 0; } return mSpanSizeLookup.getCachedSpanIndex(adapterPosition, mSpanCount); } }
GridLayoutManager extends LinearLayoutManager { private int getSpanIndex(RecyclerView.Recycler recycler, RecyclerView.State state, int pos) { if (!state.isPreLayout()) { return mSpanSizeLookup.getCachedSpanIndex(pos, mSpanCount); } final int cached = mPreLayoutSpanIndexCache.get(pos, -1); if (cached != -1) { return cached; } final int adapterPosition = recycler.convertPreLayoutPositionToPostLayout(pos); if (adapterPosition == -1) { if (DEBUG) { throw new RuntimeException("Cannot find span index for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); } Log.w(TAG, "Cannot find span size for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); return 0; } return mSpanSizeLookup.getCachedSpanIndex(adapterPosition, mSpanCount); } GridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
                             int defStyleRes);  GridLayoutManager(Context context, int spanCount);  GridLayoutManager(Context context, int spanCount,
            @RecyclerView.Orientation int orientation, boolean reverseLayout); }
GridLayoutManager extends LinearLayoutManager { private int getSpanIndex(RecyclerView.Recycler recycler, RecyclerView.State state, int pos) { if (!state.isPreLayout()) { return mSpanSizeLookup.getCachedSpanIndex(pos, mSpanCount); } final int cached = mPreLayoutSpanIndexCache.get(pos, -1); if (cached != -1) { return cached; } final int adapterPosition = recycler.convertPreLayoutPositionToPostLayout(pos); if (adapterPosition == -1) { if (DEBUG) { throw new RuntimeException("Cannot find span index for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); } Log.w(TAG, "Cannot find span size for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); return 0; } return mSpanSizeLookup.getCachedSpanIndex(adapterPosition, mSpanCount); } GridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
                             int defStyleRes);  GridLayoutManager(Context context, int spanCount);  GridLayoutManager(Context context, int spanCount,
            @RecyclerView.Orientation int orientation, boolean reverseLayout); @Override void setStackFromEnd(boolean stackFromEnd); @Override int getRowCountForAccessibility(RecyclerView.Recycler recycler,
            RecyclerView.State state); @Override int getColumnCountForAccessibility(RecyclerView.Recycler recycler,
            RecyclerView.State state); @Override void onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler,
            RecyclerView.State state, View host, AccessibilityNodeInfoCompat info); @Override void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state); @Override void onLayoutCompleted(RecyclerView.State state); @Override void onItemsAdded(RecyclerView recyclerView, int positionStart, int itemCount); @Override void onItemsChanged(RecyclerView recyclerView); @Override void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount); @Override void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount,
            Object payload); @Override void onItemsMoved(RecyclerView recyclerView, int from, int to, int itemCount); @Override RecyclerView.LayoutParams generateDefaultLayoutParams(); @Override RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs); @Override RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp); @Override boolean checkLayoutParams(RecyclerView.LayoutParams lp); void setSpanSizeLookup(SpanSizeLookup spanSizeLookup); SpanSizeLookup getSpanSizeLookup(); @Override void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec); @Override int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
            RecyclerView.State state); @Override int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
            RecyclerView.State state); int getSpanCount(); void setSpanCount(int spanCount); @Override View onFocusSearchFailed(View focused, int focusDirection,
            RecyclerView.Recycler recycler, RecyclerView.State state); @Override boolean supportsPredictiveItemAnimations(); }
GridLayoutManager extends LinearLayoutManager { private int getSpanIndex(RecyclerView.Recycler recycler, RecyclerView.State state, int pos) { if (!state.isPreLayout()) { return mSpanSizeLookup.getCachedSpanIndex(pos, mSpanCount); } final int cached = mPreLayoutSpanIndexCache.get(pos, -1); if (cached != -1) { return cached; } final int adapterPosition = recycler.convertPreLayoutPositionToPostLayout(pos); if (adapterPosition == -1) { if (DEBUG) { throw new RuntimeException("Cannot find span index for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); } Log.w(TAG, "Cannot find span size for pre layout position. It is" + " not cached, not in the adapter. Pos:" + pos); return 0; } return mSpanSizeLookup.getCachedSpanIndex(adapterPosition, mSpanCount); } GridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
                             int defStyleRes);  GridLayoutManager(Context context, int spanCount);  GridLayoutManager(Context context, int spanCount,
            @RecyclerView.Orientation int orientation, boolean reverseLayout); @Override void setStackFromEnd(boolean stackFromEnd); @Override int getRowCountForAccessibility(RecyclerView.Recycler recycler,
            RecyclerView.State state); @Override int getColumnCountForAccessibility(RecyclerView.Recycler recycler,
            RecyclerView.State state); @Override void onInitializeAccessibilityNodeInfoForItem(RecyclerView.Recycler recycler,
            RecyclerView.State state, View host, AccessibilityNodeInfoCompat info); @Override void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state); @Override void onLayoutCompleted(RecyclerView.State state); @Override void onItemsAdded(RecyclerView recyclerView, int positionStart, int itemCount); @Override void onItemsChanged(RecyclerView recyclerView); @Override void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount); @Override void onItemsUpdated(RecyclerView recyclerView, int positionStart, int itemCount,
            Object payload); @Override void onItemsMoved(RecyclerView recyclerView, int from, int to, int itemCount); @Override RecyclerView.LayoutParams generateDefaultLayoutParams(); @Override RecyclerView.LayoutParams generateLayoutParams(Context c, AttributeSet attrs); @Override RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp); @Override boolean checkLayoutParams(RecyclerView.LayoutParams lp); void setSpanSizeLookup(SpanSizeLookup spanSizeLookup); SpanSizeLookup getSpanSizeLookup(); @Override void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec); @Override int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
            RecyclerView.State state); @Override int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
            RecyclerView.State state); int getSpanCount(); void setSpanCount(int spanCount); @Override View onFocusSearchFailed(View focused, int focusDirection,
            RecyclerView.Recycler recycler, RecyclerView.State state); @Override boolean supportsPredictiveItemAnimations(); static final int DEFAULT_SPAN_COUNT; }