@Override public void onClick(View view) { expandableGroup.onToggleExpanded(); bindIcon(viewBinding); } });
@Override public void onItemChanged(@NonNull Group group, int position) { if (dispatchChildChanges(group)) { super.onItemChanged(group, position); } }
public void onToggleExpanded() { int oldSize = getItemCount(); isExpanded = !isExpanded; int newSize = getItemCount(); if (oldSize > newSize) { notifyItemRangeRemoved(newSize, oldSize - newSize); } else { notifyItemRangeInserted(oldSize, newSize - oldSize); } }
@Override public void addAll(@NonNull Collection<? extends Group> groups) { if (groups.isEmpty()) { return; } super.addAll(groups); if (isExpanded) { int itemCount = getItemCount(); this.children.addAll(groups); notifyItemRangeInserted(itemCount, getItemCount(groups)); } else { this.children.addAll(groups); } }
@Override public void remove(@NonNull Group group) { if (!this.children.contains(group)) return; super.remove(group); if (isExpanded) { int position = getItemCountBeforeGroup(group); children.remove(group); notifyItemRangeRemoved(position, group.getItemCount()); } else { children.remove(group); } }
@Test public void testExpandedGroupCountForAdd() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); DummyItem item = new DummyItem(); expandableGroup.add(item); expandableGroup.registerGroupDataObserver(groupAdapter); expandableGroup.onToggleExpanded(); assertEquals(2, expandableGroup.getGroupCount()); }
@Test public void testExpandedGroupCountForAddAll() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); List<DummyItem> items = new ArrayList<>(); int itemsCount = 5; for (int i = 0; i < itemsCount; i++) { items.add(new DummyItem()); } expandableGroup.addAll(items); expandableGroup.registerGroupDataObserver(groupAdapter); expandableGroup.onToggleExpanded(); assertEquals(6, expandableGroup.getGroupCount()); }
@Test public void testGetGroup() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); Section section = new Section(); int sectionSize = 5; for (int i = 0; i < sectionSize; i++) { section.add(new DummyItem()); } expandableGroup.add(section); Item lastItem = new DummyItem(); expandableGroup.add(lastItem); expandableGroup.onToggleExpanded(); assertEquals(lastItem, expandableGroup.getGroup(2)); }
@Test public void noAddNotificationWhenCollapsed() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); expandableGroup.registerGroupDataObserver(groupAdapter); expandableGroup.add(new DummyItem()); Mockito.verify(groupAdapter, Mockito.never()).onItemRangeInserted(expandableGroup, 1, 1); }
@Test public void collapsedByDefault() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); assertFalse(expandableGroup.isExpanded()); }
@Test public void testGetPosition() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); Section section = new Section(); expandableGroup.add(section); Section notAddedSection = new Section(); assertEquals(1, expandableGroup.getPosition(section)); assertEquals(-1, expandableGroup.getPosition(notAddedSection)); }
@Test public void testUnexpandedGroupCount() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); Section section = new Section(); int sectionSize = 5; for (int i = 0; i < sectionSize; i++) { section.add(new DummyItem()); } expandableGroup.add(section); Item lastItem = new DummyItem(); expandableGroup.add(lastItem); assertEquals(1, expandableGroup.getGroupCount()); }
ExpandableGroup expandableGroup = new ExpandableGroup(expandableHeaderItem); for (int i = 0; i < 2; i++) { expandableGroup.add(new CardItem(rainbow200[1]));
@Test public void testGetHeaderPosition() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); assertEquals(0, expandableGroup.getPosition(parent)); }
@Override public void bind(@NonNull final ItemHeaderBinding viewBinding, int position) { super.bind(viewBinding, position); // Initial icon state -- not animated. viewBinding.icon.setVisibility(View.VISIBLE); viewBinding.icon.setImageResource(expandableGroup.isExpanded() ? R.drawable.collapse : R.drawable.expand); viewBinding.icon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { expandableGroup.onToggleExpanded(); bindIcon(viewBinding); } }); }
@Test public void addNotificationWhenExpanded() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); expandableGroup.onToggleExpanded(); expandableGroup.registerGroupDataObserver(groupAdapter); expandableGroup.add(new DummyItem()); Mockito.verify(groupAdapter).onItemRangeInserted(expandableGroup, 1, 1); }
@Test public void testGroupCountForRemoveAll() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent); List<DummyItem> items = new ArrayList<>(); int itemsCount = 5; for (int i = 0; i < itemsCount; i++) { items.add(new DummyItem()); } expandableGroup.addAll(items); expandableGroup.registerGroupDataObserver(groupAdapter); expandableGroup.removeAll(items); expandableGroup.onToggleExpanded(); assertEquals(1, expandableGroup.getGroupCount()); } }
@Test public void noChildAddNotificationWhenCollapsed() { ExpandableGroup expandableGroup = new ExpandableGroup(parent); expandableGroup.registerGroupDataObserver(groupAdapter); Section section = new Section(); DummyItem item = new DummyItem(); expandableGroup.add(section); section.add(item); Mockito.verify(groupAdapter, Mockito.never()).onItemRangeInserted(expandableGroup, 1, 1); }
@Test public void expandedByDefault() throws Exception { ExpandableGroup expandableGroup = new ExpandableGroup(parent, true); assertTrue(expandableGroup.isExpanded()); }
@Override public void addAll(int position, @NonNull Collection<? extends Group> groups) { if (groups.isEmpty()) { return; } super.addAll(position, groups); if (isExpanded) { int itemCount = getItemCount(); this.children.addAll(position, groups); notifyItemRangeInserted(itemCount, getItemCount(groups)); } else { this.children.addAll(position, groups); } }