1212import androidx .fragment .app .Fragment ;
1313import androidx .recyclerview .widget .GridLayoutManager ;
1414import androidx .recyclerview .widget .RecyclerView ;
15+ import io .github .luizgrp .sectionedrecyclerviewadapter .Section ;
16+ import io .github .luizgrp .sectionedrecyclerviewadapter .SectionAdapter ;
1517import io .github .luizgrp .sectionedrecyclerviewadapter .SectionedRecyclerViewAdapter ;
1618import io .github .luizgrp .sectionedrecyclerviewadapter .demo .R ;
1719
@@ -33,10 +35,10 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
3335 glm .setSpanSizeLookup (new GridLayoutManager .SpanSizeLookup () {
3436 @ Override
3537 public int getSpanSize (int position ) {
36- if (sectionedAdapter .getSectionItemViewType (position ) == SectionedRecyclerViewAdapter .VIEW_TYPE_HEADER ) {
37- return 2 ;
38+ if (sectionedAdapter .getSectionItemViewType (position ) == SectionedRecyclerViewAdapter .VIEW_TYPE_ITEM_LOADED ) {
39+ return 1 ;
3840 }
39- return 1 ;
41+ return 2 ;
4042 }
4143 });
4244
@@ -48,7 +50,7 @@ public int getSpanSize(int position) {
4850
4951 addNewSectionToAdapter ();
5052
51- view .findViewById (R .id .btnAdd ).setOnClickListener (view1 -> addNewSectionToAdapter ());
53+ view .findViewById (R .id .fabAdd ).setOnClickListener (view1 -> addNewSectionToAdapter ());
5254
5355 return view ;
5456 }
@@ -84,24 +86,15 @@ public void onItemRootViewClicked(@NonNull final NameSection section, final int
8486 }
8587 }
8688
87- @ Override
88- public void onHeaderRootViewClicked (@ NonNull final NameSection section , final int itemAdapterPosition ) {
89- if (itemAdapterPosition != RecyclerView .NO_POSITION ) {
90- int sectionItemsTotal = section .getSectionItemsTotal ();
91-
92- sectionedAdapter .removeSection (section );
93-
94- sectionedAdapter .notifyItemRangeRemoved (itemAdapterPosition , sectionItemsTotal );
95- }
96- }
97-
9889 @ Override
9990 public void onHeaderAddButtonClicked (@ NonNull final NameSection section ) {
10091 final int positionToInsertItemAt = 0 ;
10192
10293 section .add (positionToInsertItemAt , createPerson ());
10394
104- sectionedAdapter .getAdapterForSection (section ).notifyItemInserted (positionToInsertItemAt );
95+ if (section .getState () == Section .State .LOADED ) {
96+ sectionedAdapter .getAdapterForSection (section ).notifyItemInserted (positionToInsertItemAt );
97+ }
10598 }
10699
107100 @ Override
@@ -110,7 +103,84 @@ public void onHeaderClearButtonClicked(@NonNull final NameSection section) {
110103
111104 section .clear ();
112105
113- sectionedAdapter .getAdapterForSection (section ).notifyItemRangeRemoved (0 , contentItemsTotal );
106+ if (section .getState () == Section .State .LOADED ) {
107+ sectionedAdapter .getAdapterForSection (section ).notifyItemRangeRemoved (0 , contentItemsTotal );
108+ }
109+ }
110+
111+ @ Override
112+ public void onHeaderRemoveButtonClicked (@ NonNull final NameSection section ) {
113+ final int sectionItemsTotal = section .getSectionItemsTotal ();
114+ final int sectionPosition = sectionedAdapter .getAdapterForSection (section ).getSectionPosition ();
115+
116+ sectionedAdapter .removeSection (section );
117+
118+ if (section .getState () == Section .State .LOADED ) {
119+ sectionedAdapter .notifyItemRangeRemoved (
120+ sectionPosition ,
121+ sectionItemsTotal
122+ );
123+ }
124+ }
125+
126+ @ Override
127+ public void onHeaderLoadedButtonClicked (@ NonNull final NameSection section ) {
128+ final Section .State previousState = section .getState ();
129+ if (previousState == Section .State .LOADED ) return ;
130+
131+ section .setState (Section .State .LOADED );
132+ sectionedAdapter .getAdapterForSection (section ).notifyStateChangedToLoaded (previousState );
133+ }
134+
135+ @ Override
136+ public void onHeaderLoadingButtonClicked (@ NonNull final NameSection section ) {
137+ final Section .State previousState = section .getState ();
138+ if (previousState == Section .State .LOADING ) return ;
139+
140+ final SectionAdapter sectionAdapter = sectionedAdapter .getAdapterForSection (section );
141+ final int previousItemsQty = section .getContentItemsTotal ();
142+
143+ section .setState (Section .State .LOADING );
144+
145+ if (previousState == Section .State .LOADED ) {
146+ sectionAdapter .notifyStateChangedFromLoaded (previousItemsQty );
147+ } else {
148+ sectionAdapter .notifyNotLoadedStateChanged (previousState );
149+ }
150+ }
151+
152+ @ Override
153+ public void onHeaderFailedButtonClicked (@ NonNull final NameSection section ) {
154+ final Section .State previousState = section .getState ();
155+ if (previousState == Section .State .FAILED ) return ;
156+
157+ final SectionAdapter sectionAdapter = sectionedAdapter .getAdapterForSection (section );
158+ final int previousItemsQty = section .getContentItemsTotal ();
159+
160+ section .setState (Section .State .FAILED );
161+
162+ if (previousState == Section .State .LOADED ) {
163+ sectionAdapter .notifyStateChangedFromLoaded (previousItemsQty );
164+ } else {
165+ sectionAdapter .notifyNotLoadedStateChanged (previousState );
166+ }
167+ }
168+
169+ @ Override
170+ public void onHeaderEmptyButtonClicked (@ NonNull final NameSection section ) {
171+ final Section .State previousState = section .getState ();
172+ if (previousState == Section .State .EMPTY ) return ;
173+
174+ final SectionAdapter sectionAdapter = sectionedAdapter .getAdapterForSection (section );
175+ final int previousItemsQty = section .getContentItemsTotal ();
176+
177+ section .setState (Section .State .EMPTY );
178+
179+ if (previousState == Section .State .LOADED ) {
180+ sectionAdapter .notifyStateChangedFromLoaded (previousItemsQty );
181+ } else {
182+ sectionAdapter .notifyNotLoadedStateChanged (previousState );
183+ }
114184 }
115185
116186 private Person createPerson () {
0 commit comments