diff --git a/layouts/src/main/java/org/lucasr/twowayview/widget/BaseLayoutManager.java b/layouts/src/main/java/org/lucasr/twowayview/widget/BaseLayoutManager.java index 4193cc6..8e05f84 100644 --- a/layouts/src/main/java/org/lucasr/twowayview/widget/BaseLayoutManager.java +++ b/layouts/src/main/java/org/lucasr/twowayview/widget/BaseLayoutManager.java @@ -204,9 +204,10 @@ Lanes getLanes() { } void setItemEntryForPosition(int position, ItemEntry entry) { - if (mItemEntries != null) { - mItemEntries.putItemEntry(position, entry); - } + if(mItemEntries == null) + mItemEntries = new ItemEntries(); + mItemEntries.putItemEntry(position, entry); + } ItemEntry getItemEntryForPosition(int position) { diff --git a/layouts/src/main/java/org/lucasr/twowayview/widget/ItemEntries.java b/layouts/src/main/java/org/lucasr/twowayview/widget/ItemEntries.java index 189e0c6..d965bd3 100644 --- a/layouts/src/main/java/org/lucasr/twowayview/widget/ItemEntries.java +++ b/layouts/src/main/java/org/lucasr/twowayview/widget/ItemEntries.java @@ -31,33 +31,19 @@ class ItemEntries { private static final int MIN_SIZE = 10; private ItemEntry[] mItemEntries; - private int mAdapterSize; + private int mAdapterSize = MIN_SIZE; private boolean mRestoringItem; - private int sizeForPosition(int position) { - int len = mItemEntries.length; - while (len <= position) { - len *= 2; - } - - // We don't apply any constraints while restoring - // item entries. - if (!mRestoringItem && len > mAdapterSize) { - len = mAdapterSize; - } - - return len; - } - private void ensureSize(int position) { if (mItemEntries == null) { - mItemEntries = new ItemEntry[Math.max(position, MIN_SIZE) + 1]; + mItemEntries = new ItemEntry[Math.max(position, mAdapterSize) + 1]; Arrays.fill(mItemEntries, null); } else if (position >= mItemEntries.length) { ItemEntry[] oldItemEntries = mItemEntries; - mItemEntries = new ItemEntry[sizeForPosition(position)]; + mItemEntries = new ItemEntry[position + 1]; System.arraycopy(oldItemEntries, 0, mItemEntries, 0, oldItemEntries.length); Arrays.fill(mItemEntries, oldItemEntries.length, mItemEntries.length, null); + mAdapterSize = position; } }