forked from luizgrp/SectionedRecyclerViewAdapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example8Fragment.java
252 lines (190 loc) · 7.79 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package io.github.luizgrp.sectionedrecyclerviewadapter.demo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;
import io.github.luizgrp.sectionedrecyclerviewadapter.StatelessSection;
public class Example8Fragment extends Fragment {
private static final Random RANDOM = new Random();
private SectionedRecyclerViewAdapter sectionAdapter;
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ex8, container, false);
sectionAdapter = new SectionedRecyclerViewAdapter();
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(sectionAdapter.getSectionItemViewType(position)) {
case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
return 2;
default:
return 1;
}
}
});
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionAdapter);
addNewSectionToAdapter();
addNewSectionToAdapter();
view.findViewById(R.id.btnAdd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNewSectionToAdapter();
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
if (getActivity() instanceof AppCompatActivity) {
AppCompatActivity activity = ((AppCompatActivity) getActivity());
if (activity.getSupportActionBar() != null)
activity.getSupportActionBar().setTitle(R.string.nav_example8);
}
}
private void addNewSectionToAdapter() {
String randomNumber = getRandomStringNumber();
String sectionTag = String.format("section%sTag", randomNumber);
NameSection section = new NameSection(sectionTag,
getString(R.string.group_title, randomNumber));
sectionAdapter.addSection(sectionTag, section);
int sectionPos = sectionAdapter.getSectionPosition(sectionTag);
sectionAdapter.notifyItemInserted(sectionPos);
recyclerView.smoothScrollToPosition(sectionPos);
}
@NonNull
private String getRandomStringNumber() {
return String.valueOf(RANDOM.nextInt(100000));
}
private Person getRandomName() {
String[] names = getResources().getStringArray(R.array.names);
String[] randomName = names[RANDOM.nextInt(names.length)].split("\\|");
return new Person(randomName[0], "ID #" + getRandomStringNumber());
}
class NameSection extends StatelessSection {
final String TAG;
String title;
List<Person> list;
public NameSection(String tag, String title) {
super(R.layout.section_ex8_header, R.layout.section_ex8_item);
this.TAG = tag;
this.title = title;
this.list = new ArrayList<>();
}
@Override
public int getContentItemsTotal() {
return list.size();
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new ItemViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
final ItemViewHolder itemHolder = (ItemViewHolder) holder;
String name = list.get(position).getName();
String category = list.get(position).getId();
itemHolder.tvItem.setText(name);
itemHolder.tvSubItem.setText(category);
itemHolder.imgItem.setImageResource(name.hashCode() % 2 == 0 ? R.drawable.ic_face_black_48dp : R.drawable.ic_tag_faces_black_48dp);
itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int adapterPosition = itemHolder.getAdapterPosition();
if (adapterPosition != RecyclerView.NO_POSITION) {
int positionInSection = sectionAdapter.getPositionInSection(adapterPosition);
list.remove(positionInSection);
sectionAdapter.notifyItemRemovedFromSection(TAG, positionInSection);
}
}
});
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.tvTitle.setText(title);
headerHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int positionToInsertItemAt = 0;
list.add(positionToInsertItemAt, getRandomName());
sectionAdapter.notifyItemInsertedInSection(TAG, positionToInsertItemAt);
}
});
headerHolder.btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int contentItemsTotal = getContentItemsTotal();
list.clear();
sectionAdapter.notifyItemRangeRemovedFromSection(TAG, 0, contentItemsTotal);
}
});
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
private final TextView tvTitle;
private final Button btnAdd;
private final Button btnClear;
public HeaderViewHolder(View view) {
super(view);
tvTitle = (TextView) view.findViewById(R.id.tvTitle);
btnAdd = (Button) view.findViewById(R.id.btnAdd);
btnClear = (Button) view.findViewById(R.id.btnClear);
}
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private final View rootView;
private final ImageView imgItem;
private final TextView tvItem;
private final TextView tvSubItem;
public ItemViewHolder(View view) {
super(view);
rootView = view;
imgItem = (ImageView) view.findViewById(R.id.imgItem);
tvItem = (TextView) view.findViewById(R.id.tvItem);
tvSubItem = (TextView) view.findViewById(R.id.tvSubItem);
}
}
class Person {
String name;
String id;
public Person(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
}