This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
/
Example8Fragment.java
217 lines (162 loc) · 7.93 KB
/
Example8Fragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package io.github.luizgrp.sectionedrecyclerviewadapter.demo.example8;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionAdapterListUpdateCallback;
import io.github.luizgrp.sectionedrecyclerviewadapter.Section;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionAdapter;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;
import io.github.luizgrp.sectionedrecyclerviewadapter.demo.R;
public class Example8Fragment extends Fragment implements NameSection.ClickListener {
private static final Random RANDOM = new Random();
private SectionedRecyclerViewAdapter sectionedAdapter;
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_ex8, container, false);
sectionedAdapter = new SectionedRecyclerViewAdapter();
final GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (sectionedAdapter.getSectionItemViewType(position) == SectionedRecyclerViewAdapter.VIEW_TYPE_ITEM_LOADED) {
return 1;
}
return 2;
}
});
recyclerView = view.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionedAdapter);
addNewSectionToAdapter();
addNewSectionToAdapter();
view.findViewById(R.id.fabAdd).setOnClickListener(view1 -> addNewSectionToAdapter());
return view;
}
private void addNewSectionToAdapter() {
final String randomNumber = getRandomStringNumber();
final NameSection section = new NameSection(getString(R.string.group_title, randomNumber),
this);
sectionedAdapter.addSection(section);
final int sectionPos = sectionedAdapter.getAdapterForSection(section).getSectionPosition();
sectionedAdapter.notifyItemInserted(sectionPos);
recyclerView.smoothScrollToPosition(sectionPos);
}
@NonNull
private String getRandomStringNumber() {
return String.valueOf(RANDOM.nextInt(100000));
}
@Override
public void onItemRootViewClicked(@NonNull final NameSection section, final int itemAdapterPosition) {
if (itemAdapterPosition != RecyclerView.NO_POSITION) {
int positionInSection = sectionedAdapter.getPositionInSection(itemAdapterPosition);
section.remove(positionInSection);
sectionedAdapter.getAdapterForSection(section).notifyItemRemoved(positionInSection);
}
}
@Override
public void onHeaderAddButtonClicked(@NonNull final NameSection section) {
final int positionToInsertItemAt = 0;
section.add(positionToInsertItemAt, createPerson());
if (section.getState() == Section.State.LOADED) {
sectionedAdapter.getAdapterForSection(section).notifyItemInserted(positionToInsertItemAt);
}
}
@Override
public void onHeaderClearButtonClicked(@NonNull final NameSection section) {
int contentItemsTotal = section.getContentItemsTotal();
section.clear();
if (section.getState() == Section.State.LOADED) {
sectionedAdapter.getAdapterForSection(section).notifyItemRangeRemoved(0, contentItemsTotal);
}
}
@Override
public void onHeaderShuffleButtonClicked(@NonNull final NameSection section) {
final SectionAdapterListUpdateCallback adapterListUpdateCallback = new SectionAdapterListUpdateCallback(sectionedAdapter.getAdapterForSection(section));
final List<Person> oldList = section.getList();
final List<Person> newList = new ArrayList<>(oldList);
Collections.shuffle(newList);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new PersonListDiffCallback(oldList, newList));
section.clear();
section.addAll(newList);
diffResult.dispatchUpdatesTo(adapterListUpdateCallback);
}
@Override
public void onHeaderRemoveButtonClicked(@NonNull final NameSection section) {
final int sectionItemsTotal = section.getSectionItemsTotal();
final int sectionPosition = sectionedAdapter.getAdapterForSection(section).getSectionPosition();
sectionedAdapter.removeSection(section);
if (section.getState() == Section.State.LOADED) {
sectionedAdapter.notifyItemRangeRemoved(
sectionPosition,
sectionItemsTotal
);
}
}
@Override
public void onHeaderLoadedButtonClicked(@NonNull final NameSection section) {
final Section.State previousState = section.getState();
if (previousState == Section.State.LOADED) return;
section.setState(Section.State.LOADED);
sectionedAdapter.getAdapterForSection(section).notifyStateChangedToLoaded(previousState);
}
@Override
public void onHeaderLoadingButtonClicked(@NonNull final NameSection section) {
final Section.State previousState = section.getState();
if (previousState == Section.State.LOADING) return;
final SectionAdapter sectionAdapter = sectionedAdapter.getAdapterForSection(section);
final int previousItemsQty = section.getContentItemsTotal();
section.setState(Section.State.LOADING);
if (previousState == Section.State.LOADED) {
sectionAdapter.notifyStateChangedFromLoaded(previousItemsQty);
} else {
sectionAdapter.notifyNotLoadedStateChanged(previousState);
}
}
@Override
public void onHeaderFailedButtonClicked(@NonNull final NameSection section) {
final Section.State previousState = section.getState();
if (previousState == Section.State.FAILED) return;
final SectionAdapter sectionAdapter = sectionedAdapter.getAdapterForSection(section);
final int previousItemsQty = section.getContentItemsTotal();
section.setState(Section.State.FAILED);
if (previousState == Section.State.LOADED) {
sectionAdapter.notifyStateChangedFromLoaded(previousItemsQty);
} else {
sectionAdapter.notifyNotLoadedStateChanged(previousState);
}
}
@Override
public void onHeaderEmptyButtonClicked(@NonNull final NameSection section) {
final Section.State previousState = section.getState();
if (previousState == Section.State.EMPTY) return;
final SectionAdapter sectionAdapter = sectionedAdapter.getAdapterForSection(section);
final int previousItemsQty = section.getContentItemsTotal();
section.setState(Section.State.EMPTY);
if (previousState == Section.State.LOADED) {
sectionAdapter.notifyStateChangedFromLoaded(previousItemsQty);
} else {
sectionAdapter.notifyNotLoadedStateChanged(previousState);
}
}
private Person createPerson() {
String[] names = getResources().getStringArray(R.array.names);
String randomName = names[RANDOM.nextInt(names.length)];
return new Person(randomName, "ID #" + getRandomStringNumber(), getRandomImage(randomName));
}
private int getRandomImage(final String name) {
return name.hashCode() % 2 == 0 ? R.drawable.ic_face_black_48dp : R.drawable.ic_tag_faces_black_48dp;
}
}