質問編集履歴

1

タイトル、コード修正

2018/02/08 06:20

投稿

nobu09
nobu09

スコア34

test CHANGED
@@ -1 +1 @@
1
- RecyclerViewのFragmentの基本実装方法について
1
+ RecyclerViewのFragmentの表示方法について
test CHANGED
@@ -20,6 +20,8 @@
20
20
 
21
21
  2.File->new->Fragment->Fragment(List)を追加
22
22
 
23
+ (自動作成されるDummyContent.java,ItemFragment.java,MyItemRecycleViewAdapter.java,fragment_item.xml,fragment_item_list.xmlはそのまま使用しています)
24
+
23
25
 
24
26
 
25
27
  MainActivity.java
@@ -38,10 +40,6 @@
38
40
 
39
41
  setContentView(R.layout.activity_main);
40
42
 
41
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
42
-
43
- setSupportActionBar(toolbar);
44
-
45
43
 
46
44
 
47
45
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
@@ -58,261 +56,7 @@
58
56
 
59
57
  ```
60
58
 
61
- ItemFragment.java
62
59
 
63
- ```
64
-
65
- public class ItemFragment extends Fragment {
66
-
67
- private static final String ARG_COLUMN_COUNT = "column-count";
68
-
69
- private int mColumnCount = 1;
70
-
71
- private OnListFragmentInteractionListener mListener;
72
-
73
-
74
-
75
- public ItemFragment() {
76
-
77
- }
78
-
79
-
80
-
81
- @SuppressWarnings("unused")
82
-
83
- public static ItemFragment newInstance(int columnCount) {
84
-
85
- ItemFragment fragment = new ItemFragment();
86
-
87
- Bundle args = new Bundle();
88
-
89
- args.putInt(ARG_COLUMN_COUNT, columnCount);
90
-
91
- fragment.setArguments(args);
92
-
93
- return fragment;
94
-
95
- }
96
-
97
-
98
-
99
- @Override
100
-
101
- public void onCreate(Bundle savedInstanceState) {
102
-
103
- super.onCreate(savedInstanceState);
104
-
105
-
106
-
107
- if (getArguments() != null) {
108
-
109
- mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
110
-
111
- }
112
-
113
- }
114
-
115
-
116
-
117
- @Override
118
-
119
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
120
-
121
- Bundle savedInstanceState) {
122
-
123
- View view = inflater.inflate(R.layout.fragment_item_list, container, false);
124
-
125
-
126
-
127
- if (view instanceof RecyclerView) {
128
-
129
- Context context = view.getContext();
130
-
131
- RecyclerView recyclerView = (RecyclerView) view;
132
-
133
- if (mColumnCount <= 1) {
134
-
135
- recyclerView.setLayoutManager(new LinearLayoutManager(context));
136
-
137
- } else {
138
-
139
- recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
140
-
141
- }
142
-
143
- recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
144
-
145
- }
146
-
147
- return view;
148
-
149
- }
150
-
151
-
152
-
153
- @Override
154
-
155
- public void onAttach(Context context) {
156
-
157
- super.onAttach(context);
158
-
159
- if (context instanceof OnListFragmentInteractionListener) {
160
-
161
- mListener = (OnListFragmentInteractionListener) context;
162
-
163
- } else {
164
-
165
- throw new RuntimeException(context.toString()
166
-
167
- + " must implement OnListFragmentInteractionListener");
168
-
169
- }
170
-
171
- }
172
-
173
-
174
-
175
- @Override
176
-
177
- public void onDetach() {
178
-
179
- super.onDetach();
180
-
181
- mListener = null;
182
-
183
- }
184
-
185
-
186
-
187
- public interface OnListFragmentInteractionListener {
188
-
189
- void onListFragmentInteraction(DummyItem item);
190
-
191
- }
192
-
193
- }
194
-
195
- ```
196
-
197
- MyItemRecyclerViewAdapter.java
198
-
199
- ```
200
-
201
- public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
202
-
203
-
204
-
205
- private final List<DummyItem> mValues;
206
-
207
- private final OnListFragmentInteractionListener mListener;
208
-
209
-
210
-
211
- public MyItemRecyclerViewAdapter(List<DummyItem> items, OnListFragmentInteractionListener listener) {
212
-
213
- mValues = items;
214
-
215
- mListener = listener;
216
-
217
- }
218
-
219
-
220
-
221
- @Override
222
-
223
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
224
-
225
- View view = LayoutInflater.from(parent.getContext())
226
-
227
- .inflate(R.layout.fragment_item, parent, false);
228
-
229
- return new ViewHolder(view);
230
-
231
- }
232
-
233
-
234
-
235
- @Override
236
-
237
- public void onBindViewHolder(final ViewHolder holder, int position) {
238
-
239
- holder.mItem = mValues.get(position);
240
-
241
- holder.mIdView.setText(mValues.get(position).id);
242
-
243
- holder.mContentView.setText(mValues.get(position).content);
244
-
245
-
246
-
247
- holder.mView.setOnClickListener(new View.OnClickListener() {
248
-
249
- @Override
250
-
251
- public void onClick(View v) {
252
-
253
- if (null != mListener) {
254
-
255
- mListener.onListFragmentInteraction(holder.mItem);
256
-
257
- }
258
-
259
- }
260
-
261
- });
262
-
263
- }
264
-
265
-
266
-
267
- @Override
268
-
269
- public int getItemCount() {
270
-
271
- return mValues.size();
272
-
273
- }
274
-
275
-
276
-
277
- public class ViewHolder extends RecyclerView.ViewHolder {
278
-
279
- public final View mView;
280
-
281
- public final TextView mIdView;
282
-
283
- public final TextView mContentView;
284
-
285
- public DummyItem mItem;
286
-
287
-
288
-
289
- public ViewHolder(View view) {
290
-
291
- super(view);
292
-
293
- mView = view;
294
-
295
- mIdView = (TextView) view.findViewById(R.id.id);
296
-
297
- mContentView = (TextView) view.findViewById(R.id.content);
298
-
299
- }
300
-
301
-
302
-
303
- @Override
304
-
305
- public String toString() {
306
-
307
- return super.toString() + " '" + mContentView.getText() + "'";
308
-
309
- }
310
-
311
- }
312
-
313
- }
314
-
315
- ```
316
60
 
317
61
  activity_main.xml
318
62
 
@@ -334,51 +78,13 @@
334
78
 
335
79
 
336
80
 
81
+ <FrameLayout
82
+
337
- <android.support.design.widget.AppBarLayout
83
+ android:id="@+id/sample_content_fragment"
338
84
 
339
85
  android:layout_width="match_parent"
340
86
 
341
- android:layout_height="wrap_content"
342
-
343
- android:theme="@style/AppTheme.AppBarOverlay">
344
-
345
-
346
-
347
- <android.support.v7.widget.Toolbar
348
-
349
- android:id="@+id/toolbar"
350
-
351
- android:layout_width="match_parent"
87
+ android:layout_height="match_parent" />
352
-
353
- android:layout_height="?attr/actionBarSize"
354
-
355
- android:background="?attr/colorPrimary"
356
-
357
- app:popupTheme="@style/AppTheme.PopupOverlay" />
358
-
359
-
360
-
361
- </android.support.design.widget.AppBarLayout>
362
-
363
-
364
-
365
- <include layout="@layout/fragment_item_list" />
366
-
367
-
368
-
369
- <android.support.design.widget.FloatingActionButton
370
-
371
- android:id="@+id/fab"
372
-
373
- android:layout_width="wrap_content"
374
-
375
- android:layout_height="wrap_content"
376
-
377
- android:layout_gravity="bottom|end"
378
-
379
- android:layout_margin="@dimen/fab_margin"
380
-
381
- app:srcCompat="@android:drawable/ic_dialog_email" />
382
88
 
383
89
 
384
90
 
@@ -386,121 +92,9 @@
386
92
 
387
93
  ```
388
94
 
389
- content_main.xml
390
-
391
- ```
392
-
393
- <?xml version="1.0" encoding="utf-8"?>
394
-
395
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
396
-
397
- xmlns:app="http://schemas.android.com/apk/res-auto"
398
-
399
- xmlns:tools="http://schemas.android.com/tools"
400
-
401
- android:layout_width="match_parent"
402
-
403
- android:layout_height="match_parent"
404
-
405
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
406
-
407
- tools:context="com.example.test.recycleviewtest.MainActivity"
408
-
409
- tools:showIn="@layout/activity_main">
410
95
 
411
96
 
412
97
 
413
- <FrameLayout
414
-
415
- android:id="@+id/sample_content_fragment"
416
-
417
- android:layout_width="match_parent"
418
-
419
- android:layout_height="wrap_content" />
420
-
421
-
422
-
423
- </android.support.constraint.ConstraintLayout>
424
-
425
- ```
426
-
427
- fragment_item.xml
428
-
429
- ```
430
-
431
- <?xml version="1.0" encoding="utf-8"?>
432
-
433
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
434
-
435
- android:layout_width="wrap_content"
436
-
437
- android:layout_height="wrap_content"
438
-
439
- android:orientation="horizontal">
440
-
441
-
442
-
443
- <TextView
444
-
445
- android:id="@+id/id"
446
-
447
- android:layout_width="wrap_content"
448
-
449
- android:layout_height="wrap_content"
450
-
451
- android:layout_margin="@dimen/text_margin"
452
-
453
- android:textAppearance="?attr/textAppearanceListItem" />
454
-
455
-
456
-
457
- <TextView
458
-
459
- android:id="@+id/content"
460
-
461
- android:layout_width="wrap_content"
462
-
463
- android:layout_height="wrap_content"
464
-
465
- android:layout_margin="@dimen/text_margin"
466
-
467
- android:textAppearance="?attr/textAppearanceListItem" />
468
-
469
- </LinearLayout>
470
-
471
- ```
472
-
473
- fragment_item_list.xml
474
-
475
- ```
476
-
477
- <?xml version="1.0" encoding="utf-8"?>
478
-
479
- <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
480
-
481
- xmlns:app="http://schemas.android.com/apk/res-auto"
482
-
483
- xmlns:tools="http://schemas.android.com/tools"
484
-
485
- android:id="@+id/list"
486
-
487
- android:name="com.example.test.recycleviewtest.ItemFragment"
488
-
489
- android:layout_width="match_parent"
490
-
491
- android:layout_height="match_parent"
492
-
493
- android:layout_marginLeft="16dp"
494
-
495
- android:layout_marginRight="16dp"
496
-
497
- app:layoutManager="LinearLayoutManager"
498
-
499
- tools:context="com.example.test.recycleviewtest.ItemFragment"
500
-
501
- tools:listitem="@layout/fragment_item" />
502
-
503
- ```
504
98
 
505
99
  ### 補足情報
506
100