Skip to content

Commit 8fce644

Browse files
committed
Improve example 8
1 parent e12f384 commit 8fce644

File tree

11 files changed

+306
-47
lines changed

11 files changed

+306
-47
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencies {
2727
implementation fileTree(dir: 'libs', include: ['*.jar'])
2828
implementation project(':library')
2929
implementation 'androidx.appcompat:appcompat:1.1.0'
30-
implementation 'androidx.recyclerview:recyclerview:1.0.0'
30+
implementation 'androidx.recyclerview:recyclerview:1.1.0'
3131
implementation 'androidx.cardview:cardview:1.0.0'
3232
implementation 'androidx.navigation:navigation-fragment:2.1.0'
3333
implementation 'androidx.navigation:navigation-ui:2.1.0'

app/src/main/java/io/github/luizgrp/sectionedrecyclerviewadapter/demo/example8/Example8Fragment.java

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import androidx.fragment.app.Fragment;
1313
import androidx.recyclerview.widget.GridLayoutManager;
1414
import androidx.recyclerview.widget.RecyclerView;
15+
import io.github.luizgrp.sectionedrecyclerviewadapter.Section;
16+
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionAdapter;
1517
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;
1618
import 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() {

app/src/main/java/io/github/luizgrp/sectionedrecyclerviewadapter/demo/example8/HeaderViewHolder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@
1010

1111
class HeaderViewHolder extends RecyclerView.ViewHolder {
1212

13-
final View rootView;
1413
final TextView tvTitle;
1514
final Button btnAdd;
1615
final Button btnClear;
16+
final Button btnRemove;
17+
final Button btnStateLoaded;
18+
final Button btnStateLoading;
19+
final Button btnStateFailed;
20+
final Button btnStateEmpty;
1721

1822
HeaderViewHolder(@NonNull final View view) {
1923
super(view);
2024

21-
rootView = view;
2225
tvTitle = view.findViewById(R.id.tvTitle);
2326
btnAdd = view.findViewById(R.id.btnAdd);
2427
btnClear = view.findViewById(R.id.btnClear);
28+
btnRemove = view.findViewById(R.id.btnRemove);
29+
btnStateLoaded = view.findViewById(R.id.btnStateLoaded);
30+
btnStateLoading = view.findViewById(R.id.btnStateLoading);
31+
btnStateFailed = view.findViewById(R.id.btnStateFailed);
32+
btnStateEmpty = view.findViewById(R.id.btnStateEmpty);
2533
}
2634
}

app/src/main/java/io/github/luizgrp/sectionedrecyclerviewadapter/demo/example8/NameSection.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.github.luizgrp.sectionedrecyclerviewadapter.Section;
1111
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionParameters;
1212
import io.github.luizgrp.sectionedrecyclerviewadapter.demo.R;
13+
import io.github.luizgrp.sectionedrecyclerviewadapter.utils.EmptyViewHolder;
1314

1415
class NameSection extends Section {
1516

@@ -21,6 +22,9 @@ class NameSection extends Section {
2122
super(SectionParameters.builder()
2223
.itemResourceId(R.layout.section_ex8_item)
2324
.headerResourceId(R.layout.section_ex8_header)
25+
.loadingResourceId(R.layout.section_ex8_loading)
26+
.failedResourceId(R.layout.section_ex8_failed)
27+
.emptyResourceId(R.layout.section_ex8_empty)
2428
.build());
2529

2630
this.title = title;
@@ -64,12 +68,32 @@ public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
6468

6569
headerHolder.tvTitle.setText(title);
6670

67-
headerHolder.rootView.setOnClickListener(view ->
68-
clickListener.onHeaderRootViewClicked(this, headerHolder.getAdapterPosition()));
69-
7071
headerHolder.btnAdd.setOnClickListener(v -> clickListener.onHeaderAddButtonClicked(this));
7172

7273
headerHolder.btnClear.setOnClickListener(v -> clickListener.onHeaderClearButtonClicked(this));
74+
75+
headerHolder.btnRemove.setOnClickListener(view ->
76+
clickListener.onHeaderRemoveButtonClicked(this));
77+
78+
headerHolder.btnStateLoaded.setOnClickListener(v -> clickListener.onHeaderLoadedButtonClicked(this));
79+
headerHolder.btnStateLoading.setOnClickListener(v -> clickListener.onHeaderLoadingButtonClicked(this));
80+
headerHolder.btnStateFailed.setOnClickListener(v -> clickListener.onHeaderFailedButtonClicked(this));
81+
headerHolder.btnStateEmpty.setOnClickListener(v -> clickListener.onHeaderEmptyButtonClicked(this));
82+
}
83+
84+
@Override
85+
public RecyclerView.ViewHolder getLoadingViewHolder(final View view) {
86+
return new EmptyViewHolder(view);
87+
}
88+
89+
@Override
90+
public RecyclerView.ViewHolder getFailedViewHolder(final View view) {
91+
return new EmptyViewHolder(view);
92+
}
93+
94+
@Override
95+
public RecyclerView.ViewHolder getEmptyViewHolder(final View view) {
96+
return new EmptyViewHolder(view);
7397
}
7498

7599
void add(final int position, @NonNull final Person person) {
@@ -88,10 +112,18 @@ interface ClickListener {
88112

89113
void onItemRootViewClicked(@NonNull final NameSection section, final int itemAdapterPosition);
90114

91-
void onHeaderRootViewClicked(@NonNull final NameSection section, final int itemAdapterPosition);
92-
93115
void onHeaderAddButtonClicked(@NonNull final NameSection section);
94116

95117
void onHeaderClearButtonClicked(@NonNull final NameSection section);
118+
119+
void onHeaderRemoveButtonClicked(@NonNull final NameSection section);
120+
121+
void onHeaderLoadedButtonClicked(@NonNull final NameSection section);
122+
123+
void onHeaderLoadingButtonClicked(@NonNull final NameSection section);
124+
125+
void onHeaderFailedButtonClicked(@NonNull final NameSection section);
126+
127+
void onHeaderEmptyButtonClicked(@NonNull final NameSection section);
96128
}
97129
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24.0" android:viewportWidth="24.0"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="#FF000000" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
5+
</vector>
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:orientation="vertical"
4-
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
6-
7-
<Button
8-
android:id="@+id/btnAdd"
9-
android:layout_width="match_parent"
10-
android:layout_height="wrap_content"
11-
android:layout_margin="16dp"
12-
android:text="@string/add_group"
13-
android:textColor="@color/white"
14-
android:textStyle="bold"
15-
android:background="@drawable/selector_btn"/>
16-
17-
<View
18-
android:layout_width="match_parent"
19-
android:layout_height="1dp"
20-
android:background="@color/colorAccent"/>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
216

227
<androidx.recyclerview.widget.RecyclerView
238
android:id="@+id/recyclerview"
249
android:layout_width="match_parent"
25-
android:layout_height="match_parent"/>
10+
android:layout_height="match_parent" />
11+
12+
<com.google.android.material.floatingactionbutton.FloatingActionButton
13+
android:id="@+id/fabAdd"
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:layout_alignParentEnd="true"
17+
android:layout_alignParentRight="true"
18+
android:layout_alignParentBottom="true"
19+
android:layout_margin="16dp"
20+
android:contentDescription="@string/cd_add_group"
21+
android:src="@drawable/ic_add_white_24dp" />
2622

27-
</LinearLayout>
23+
</RelativeLayout>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
android:id="@+id/rootView"
4+
xmlns:android="http://schemas.android.com/apk/res/android"
5+
android:layout_width="match_parent"
6+
android:layout_height="240dp"
7+
android:background="@drawable/selector_item"
8+
android:clickable="true"
9+
android:focusable="true">
10+
11+
<TextView
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_centerVertical="true"
15+
android:layout_centerHorizontal="true"
16+
android:text="@string/empty_list"/>
17+
18+
</RelativeLayout>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/rootView"
4+
android:layout_width="match_parent"
5+
android:layout_height="240dp"
6+
android:background="@drawable/selector_item"
7+
android:clickable="true"
8+
android:focusable="true">
9+
10+
<ImageView
11+
android:id="@+id/imageView"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_centerHorizontal="true"
15+
android:layout_centerVertical="true"
16+
android:contentDescription="@string/cd_cloud_crossed"
17+
android:src="@drawable/ic_cloud_off_black_48dp" />
18+
19+
</RelativeLayout>

0 commit comments

Comments
 (0)