Groupie: Q: Show/Hide Section with CarouselItem

Created on 17 May 2017  路  3Comments  路  Source: lisawray/groupie

I have a Section, a section header and a CarouselItem.
I want this section to hide when CarouselItem is empty and show when Carousel has items.
How do i setup this behavior?

// onCreate
Section section = new Section(new HeaderItem("Header"));
section.setHideWhenEmpty(true);
CarouselItem carousel = new CarouselItem();
section.add(carousel);

// after network response 
carousel.addAll(cardItemList);
carousel.notifyChange(); // to trigger getItemCount()

I modify the CarouselItem to return 0 or 1 based on the carousel adapter.

public int getItemCount() {
    return (adapter == null || adapter.getItemCount() == 0) ? 0 : 1;
}

Result: The Carousel is displayed but section header is still hidden.

question

Most helpful comment

Hey @andhie, I added a PR adding a carousel section that hides itself when there are no carousel contents. I didn't get to debug your gist but I used a different approach, making a very simple Group wrapper (CarouselGroup) which reports a single CarouselItem added or removed. Placing this group inside a Section with setHideWhenEmpty produces the desired result.

https://github.com/lisawray/groupie/pull/97

sailfishn2g47olisa06142017001109

All 3 comments

You want to set an AdapterDataObserver on the CarouselItem's adapter. This will let you know when the carousel's contents change. When the carousel changes to empty or not empty, you need to notify the parent.

The main problem right now is that although you've changed what the method CarouselItem.getItemCount() would potentially return, no one is calling it, because no one knows the contents of the main list have changed.

I don't have access anymore to the code last time I did this, but I would extend the Section instead and have it set an AdapterDataObserver on the carousel (horizontal RV) itself. When the carousel's state changes to 0 or not-0, then add or remove the carousel item from the section using Section.add() or remove(). (keep a reference to it of course!) That should work as you expect.

I have a feeling you could also do this by overriding Section.isEmpty to depend on the carousel's contents and calling Section.refreshEmptyState when it changes. Let me know how it goes. Seems like a good candidate for adding to the example app at least!

I think something is missing. the code gist implements the Section#add and Section#remove approach. the header always appears

https://gist.github.com/andhie/a6d1841473ecd9586c4643f15c2a0648

Hey @andhie, I added a PR adding a carousel section that hides itself when there are no carousel contents. I didn't get to debug your gist but I used a different approach, making a very simple Group wrapper (CarouselGroup) which reports a single CarouselItem added or removed. Placing this group inside a Section with setHideWhenEmpty produces the desired result.

https://github.com/lisawray/groupie/pull/97

sailfishn2g47olisa06142017001109

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kawaiiDango picture kawaiiDango  路  5Comments

FabianTerhorst picture FabianTerhorst  路  6Comments

hardysim picture hardysim  路  3Comments

mattinger picture mattinger  路  3Comments

jordond picture jordond  路  6Comments