adapter.notifyItemRangeChanged(counter, 1);
private int notify(@Nonnull ChangesAdapter adapter, int counter, int toRemove, int itemsInserted) { final int itemsChanged = Math.min(itemsInserted, toRemove); toRemove -= itemsChanged; itemsInserted -= itemsChanged; if (itemsChanged > 0) { adapter.notifyItemRangeChanged(counter, itemsChanged); counter += itemsChanged; } if (toRemove > 0) { adapter.notifyItemRangeRemoved(counter, toRemove); } if (itemsInserted > 0) { adapter.notifyItemRangeInserted(counter, itemsInserted); counter += itemsInserted; } return counter; }
@Test public void testItemSwapped() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(3, "tree")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(2, "two"), new Cat(3, "tree")), false); verify(mAdapter).notifyItemRangeChanged(0, 1); }
@Test public void testAddItemInTheMiddle() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(3, "tree")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two"), new Cat(3, "tree")), false); verify(mAdapter).notifyItemRangeInserted(1, 1); }
@Test public void testAddItemAtTheBegining() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(2, "two")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two")), false); verify(mAdapter).notifyItemRangeInserted(0, 1); }
@Test public void testItemDeleted1() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two"), new Cat(3, "tree")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(2, "two"), new Cat(3, "tree")), false); verify(mAdapter).notifyItemRangeRemoved(0, 1); }
@Test public void testForce2() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two")), true); verify(mAdapter).notifyItemRangeChanged(0, 1); verify(mAdapter).notifyItemRangeInserted(1, 1); }
@Test public void testItemDeleted3() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two"), new Cat(3, "tree")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two")), false); verify(mAdapter).notifyItemRangeRemoved(2, 1); }
@Test public void testItemChanged() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(3, "tree")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one1"), new Cat(3, "tree")), false); verify(mAdapter).notifyItemRangeChanged(0, 1); }
@Test public void testStart() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one")), false); verify(mAdapter).notifyItemRangeInserted(0, 1); }
@Test public void testItemDeleted2() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two"), new Cat(3, "tree")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(3, "tree")), false); verify(mAdapter).notifyItemRangeRemoved(1, 1); }
@Test public void testAddItemAtTheEnd() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(2, "two")), false); verify(mAdapter).notifyItemRangeInserted(1, 1); }
@Test public void testForce() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one")), true); verify(mAdapter).notifyItemRangeChanged(0, 1); }
@Test public void testItemRemovedAndAdded() throws Exception { mDetector.newData(mAdapter, ImmutableList.of(new Cat(1, "one"), new Cat(4, "four")), false); reset(mAdapter); mDetector.newData(mAdapter, ImmutableList.of(new Cat(2, "two"), new Cat(3, "tree"), new Cat(4, "four")), false); verify(mAdapter).notifyItemRangeChanged(0, 1); verify(mAdapter).notifyItemRangeInserted(1, 1); } }