forked from luizgrp/SectionedRecyclerViewAdapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example3Fragment.java
304 lines (240 loc) · 10 KB
/
Example3Fragment.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package io.github.luizgrp.sectionedrecyclerviewadapter.demo;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import io.github.luizgrp.sectionedrecyclerviewadapter.Section;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionParameters;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;
public class Example3Fragment extends Fragment {
private Handler mHandler = new Handler();
private SectionedRecyclerViewAdapter sectionAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ex3, container, false);
sectionAdapter = new SectionedRecyclerViewAdapter();
NewsSection worldNews = new NewsSection(NewsSection.WORLD);
NewsSection bizNews = new NewsSection(NewsSection.BUSINESS);
NewsSection techNews = new NewsSection(NewsSection.TECHNOLOGY);
NewsSection sportsNews = new NewsSection(NewsSection.SPORTS);
sectionAdapter.addSection(worldNews);
sectionAdapter.addSection(bizNews);
sectionAdapter.addSection(techNews);
sectionAdapter.addSection(sportsNews);
loadNews(worldNews);
loadNews(bizNews);
loadNews(techNews);
loadNews(sportsNews);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
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_example3);
}
}
@Override
public void onDetach() {
mHandler.removeCallbacksAndMessages(null);
super.onDetach();
}
private void loadNews(final NewsSection section) {
int timeInMills = new Random().nextInt((7000 - 3000) + 1) + 3000;
section.setState(Section.State.LOADING);
section.setHasFooter(false);
sectionAdapter.notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
int failed = new Random().nextInt((3 - 1) + 1) + 1;
if (failed == 1) {
section.setState(Section.State.FAILED);
}
else {
int arrayResource;
switch (section.getTopic()) {
case NewsSection.WORLD:
arrayResource = R.array.news_world;
break;
case NewsSection.BUSINESS:
arrayResource = R.array.news_biz;
break;
case NewsSection.TECHNOLOGY:
arrayResource = R.array.news_tech;
break;
case NewsSection.SPORTS:
arrayResource = R.array.news_sports;
break;
default:
throw new IllegalStateException("Invalid topic");
}
section.setList(getNews(arrayResource));
section.setState(Section.State.LOADED);
section.setHasFooter(true);
}
sectionAdapter.notifyDataSetChanged();
}
}, timeInMills);
}
private List<String> getNews(int arrayResource) {
return new ArrayList<>(Arrays.asList(getResources().getStringArray(arrayResource)));
}
private class NewsSection extends Section {
final static int WORLD = 0;
final static int BUSINESS = 1;
final static int TECHNOLOGY = 2;
final static int SPORTS = 3;
final int topic;
String title;
List<String> list;
int imgPlaceholderResId;
NewsSection(int topic) {
super(new SectionParameters.Builder(R.layout.section_ex3_item)
.headerResourceId(R.layout.section_ex3_header)
.footerResourceId(R.layout.section_ex3_footer)
.failedResourceId(R.layout.section_ex3_failed)
.loadingResourceId(R.layout.section_ex3_loading)
.build());
this.topic = topic;
this.list = Collections.emptyList();
switch (topic) {
case WORLD:
this.title = getString(R.string.world_topic);
this.imgPlaceholderResId = R.drawable.ic_public_black_48dp;
break;
case BUSINESS:
this.title = getString(R.string.biz_topic);
this.imgPlaceholderResId = R.drawable.ic_business_black_48dp;
break;
case TECHNOLOGY:
this.title = getString(R.string.tech_topic);
this.imgPlaceholderResId = R.drawable.ic_devices_other_black_48dp;
break;
case SPORTS:
this.title = getString(R.string.sports_topic);
this.imgPlaceholderResId = R.drawable.ic_directions_run_black_48dp;
break;
}
}
int getTopic() {
return topic;
}
public void setList(List<String> list) {
this.list = list;
}
@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[] item = list.get(position).split("\\|");
itemHolder.tvHeader.setText(item[0]);
itemHolder.tvDate.setText(item[1]);
itemHolder.imgItem.setImageResource(imgPlaceholderResId);
itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), String.format("Clicked on position #%s of Section %s", sectionAdapter.getPositionInSection(itemHolder.getAdapterPosition()), title), Toast.LENGTH_SHORT).show();
}
});
}
@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);
}
@Override
public RecyclerView.ViewHolder getFooterViewHolder(View view) {
return new FooterViewHolder(view);
}
@Override
public void onBindFooterViewHolder(RecyclerView.ViewHolder holder) {
FooterViewHolder footerHolder = (FooterViewHolder) holder;
footerHolder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), String.format("Clicked on footer of Section %s", title), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public RecyclerView.ViewHolder getFailedViewHolder(View view) {
return new FailedViewHolder(view);
}
@Override
public void onBindFailedViewHolder(RecyclerView.ViewHolder holder) {
FailedViewHolder footerHeader = (FailedViewHolder) holder;
footerHeader.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadNews(NewsSection.this);
}
});
}
}
private class HeaderViewHolder extends RecyclerView.ViewHolder {
private final TextView tvTitle;
HeaderViewHolder(View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
}
}
private class FooterViewHolder extends RecyclerView.ViewHolder {
private final View rootView;
FooterViewHolder(View view) {
super(view);
rootView = view;
}
}
private class FailedViewHolder extends RecyclerView.ViewHolder {
private final View rootView;
FailedViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.rootView);
}
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
private final View rootView;
private final ImageView imgItem;
private final TextView tvHeader;
private final TextView tvDate;
ItemViewHolder(View view) {
super(view);
rootView = view;
imgItem = (ImageView) view.findViewById(R.id.imgItem);
tvHeader = (TextView) view.findViewById(R.id.tvHeader);
tvDate = (TextView) view.findViewById(R.id.tvDate);
}
}
}