質問編集履歴

4

フラグメントのjavaコードを追記

2018/06/03 09:44

投稿

TA-KEY
TA-KEY

スコア21

test CHANGED
File without changes
test CHANGED
@@ -214,13 +214,9 @@
214
214
 
215
215
 
216
216
 
217
-
217
+ ```java
218
-
219
-
220
-
221
-
222
-
218
+
223
- ```MainActivity.java
219
+ Main.Activity.java
224
220
 
225
221
 
226
222
 
@@ -317,3 +313,235 @@
317
313
 
318
314
 
319
315
  ```
316
+
317
+
318
+
319
+ ```java
320
+
321
+ page1.java
322
+
323
+
324
+
325
+ package com.example.taiki.grasphand;
326
+
327
+
328
+
329
+ import android.content.Context;
330
+
331
+ import android.net.Uri;
332
+
333
+ import android.os.Bundle;
334
+
335
+ import android.support.v4.app.Fragment;
336
+
337
+ import android.view.LayoutInflater;
338
+
339
+ import android.view.View;
340
+
341
+ import android.view.ViewGroup;
342
+
343
+
344
+
345
+
346
+
347
+ /**
348
+
349
+ * A simple {@link Fragment} subclass.
350
+
351
+ * Activities that contain this fragment must implement the
352
+
353
+ * {@link page1.OnFragmentInteractionListener} interface
354
+
355
+ * to handle interaction events.
356
+
357
+ * Use the {@link page1#newInstance} factory method to
358
+
359
+ * create an instance of this fragment.
360
+
361
+ */
362
+
363
+ public class page1 extends Fragment {
364
+
365
+ // TODO: Rename parameter arguments, choose names that match
366
+
367
+ // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
368
+
369
+ private static final String ARG_PARAM1 = "param1";
370
+
371
+ private static final String ARG_PARAM2 = "param2";
372
+
373
+
374
+
375
+ // TODO: Rename and change types of parameters
376
+
377
+ private String mParam1;
378
+
379
+ private String mParam2;
380
+
381
+
382
+
383
+ private OnFragmentInteractionListener mListener;
384
+
385
+
386
+
387
+ public page1() {
388
+
389
+ // Required empty public constructor
390
+
391
+ }
392
+
393
+
394
+
395
+ /**
396
+
397
+ * Use this factory method to create a new instance of
398
+
399
+ * this fragment using the provided parameters.
400
+
401
+ *
402
+
403
+ * @param param1 Parameter 1.
404
+
405
+ * @param param2 Parameter 2.
406
+
407
+ * @return A new instance of fragment page1.
408
+
409
+ */
410
+
411
+ // TODO: Rename and change types and number of parameters
412
+
413
+ public static page1 newInstance(String param1, String param2) {
414
+
415
+ page1 fragment = new page1();
416
+
417
+ Bundle args = new Bundle();
418
+
419
+ args.putString(ARG_PARAM1, param1);
420
+
421
+ args.putString(ARG_PARAM2, param2);
422
+
423
+ fragment.setArguments(args);
424
+
425
+ return fragment;
426
+
427
+ }
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+ @Override
436
+
437
+ public void onCreate(Bundle savedInstanceState) {
438
+
439
+ super.onCreate(savedInstanceState);
440
+
441
+ if (getArguments() != null) {
442
+
443
+ mParam1 = getArguments().getString(ARG_PARAM1);
444
+
445
+ mParam2 = getArguments().getString(ARG_PARAM2);
446
+
447
+ }
448
+
449
+ }
450
+
451
+
452
+
453
+ @Override
454
+
455
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
456
+
457
+ Bundle savedInstanceState) {
458
+
459
+ // Inflate the layout for this fragment
460
+
461
+ return inflater.inflate(R.layout.fragment_page1, container, false);
462
+
463
+ }
464
+
465
+
466
+
467
+ // TODO: Rename method, update argument and hook method into UI event
468
+
469
+ public void onButtonPressed(Uri uri) {
470
+
471
+ if (mListener != null) {
472
+
473
+ mListener.onFragmentInteraction(uri);
474
+
475
+ }
476
+
477
+ }
478
+
479
+
480
+
481
+ @Override
482
+
483
+ public void onAttach(Context context) {
484
+
485
+ super.onAttach(context);
486
+
487
+ if (context instanceof OnFragmentInteractionListener) {
488
+
489
+ mListener = (OnFragmentInteractionListener) context;
490
+
491
+ } else {
492
+
493
+ throw new RuntimeException(context.toString()
494
+
495
+ + " must implement OnFragmentInteractionListener");
496
+
497
+ }
498
+
499
+ }
500
+
501
+
502
+
503
+ @Override
504
+
505
+ public void onDetach() {
506
+
507
+ super.onDetach();
508
+
509
+ mListener = null;
510
+
511
+ }
512
+
513
+
514
+
515
+ /**
516
+
517
+ * This interface must be implemented by activities that contain this
518
+
519
+ * fragment to allow an interaction in this fragment to be communicated
520
+
521
+ * to the activity and potentially other fragments contained in that
522
+
523
+ * activity.
524
+
525
+ * <p>
526
+
527
+ * See the Android Training lesson <a href=
528
+
529
+ * "http://developer.android.com/training/basics/fragments/communicating.html"
530
+
531
+ * >Communicating with Other Fragments</a> for more information.
532
+
533
+ */
534
+
535
+ public interface OnFragmentInteractionListener {
536
+
537
+ // TODO: Update argument type and name
538
+
539
+ void onFragmentInteraction(Uri uri);
540
+
541
+ }
542
+
543
+ }
544
+
545
+
546
+
547
+ ```

3

コードの一部を更新

2018/06/03 09:43

投稿

TA-KEY
TA-KEY

スコア21

test CHANGED
File without changes
test CHANGED
@@ -260,24 +260,26 @@
260
260
 
261
261
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
262
262
 
263
+ //Fragment selectedFragment = null;
264
+
263
265
  switch (item.getItemId()) {
264
266
 
265
267
  case R.id.navigation_home:
266
268
 
269
+ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
270
+
267
- mTextMessage.setText(R.string.title_home);
271
+ transaction.replace(R.id.container, page1.newInstance());
272
+
273
+ transaction.commit();
268
274
 
269
275
  return true;
270
276
 
271
277
  case R.id.navigation_dashboard:
272
278
 
273
- mTextMessage.setText(R.string.title_dashboard);
274
-
275
279
  return true;
276
280
 
277
281
  case R.id.navigation_notifications:
278
282
 
279
- mTextMessage.setText(R.string.title_notifications);
280
-
281
283
  return true;
282
284
 
283
285
  }

2

javaファイル追記しました。

2018/06/03 09:29

投稿

TA-KEY
TA-KEY

スコア21

test CHANGED
File without changes
test CHANGED
@@ -211,3 +211,107 @@
211
211
 
212
212
 
213
213
  ```
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+ ```MainActivity.java
224
+
225
+
226
+
227
+ package com.example.taiki.grasphand;
228
+
229
+
230
+
231
+ import android.os.Bundle;
232
+
233
+ import android.support.annotation.NonNull;
234
+
235
+ import android.support.design.widget.BottomNavigationView;
236
+
237
+ import android.support.v7.app.AppCompatActivity;
238
+
239
+ import android.view.MenuItem;
240
+
241
+ import android.widget.TextView;
242
+
243
+
244
+
245
+ public class MainActivity extends AppCompatActivity {
246
+
247
+
248
+
249
+ private TextView mTextMessage;
250
+
251
+
252
+
253
+ private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
254
+
255
+ = new BottomNavigationView.OnNavigationItemSelectedListener() {
256
+
257
+
258
+
259
+ @Override
260
+
261
+ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
262
+
263
+ switch (item.getItemId()) {
264
+
265
+ case R.id.navigation_home:
266
+
267
+ mTextMessage.setText(R.string.title_home);
268
+
269
+ return true;
270
+
271
+ case R.id.navigation_dashboard:
272
+
273
+ mTextMessage.setText(R.string.title_dashboard);
274
+
275
+ return true;
276
+
277
+ case R.id.navigation_notifications:
278
+
279
+ mTextMessage.setText(R.string.title_notifications);
280
+
281
+ return true;
282
+
283
+ }
284
+
285
+ return false;
286
+
287
+ }
288
+
289
+ };
290
+
291
+
292
+
293
+ @Override
294
+
295
+ protected void onCreate(Bundle savedInstanceState) {
296
+
297
+ super.onCreate(savedInstanceState);
298
+
299
+ setContentView(R.layout.activity_main);
300
+
301
+
302
+
303
+ mTextMessage = (TextView) findViewById(R.id.message);
304
+
305
+ BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
306
+
307
+ navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
308
+
309
+ }
310
+
311
+
312
+
313
+ }
314
+
315
+
316
+
317
+ ```

1

xmlファイルのコードを追記

2018/06/03 02:37

投稿

TA-KEY
TA-KEY

スコア21

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,193 @@
21
21
 
22
22
 
23
23
  ![イメージ説明](c39274c9a613927fb0b9ea0e6a7b2108.jpeg)
24
+
25
+
26
+
27
+
28
+
29
+ ```xml
30
+
31
+
32
+
33
+ <?xml version="1.0" encoding="utf-8"?>
34
+
35
+ <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
36
+
37
+ xmlns:app="http://schemas.android.com/apk/res-auto"
38
+
39
+ xmlns:tools="http://schemas.android.com/tools"
40
+
41
+ android:id="@+id/container"
42
+
43
+ android:layout_width="match_parent"
44
+
45
+ android:layout_height="match_parent"
46
+
47
+ tools:context="@android:string/VideoView_error_button">
48
+
49
+
50
+
51
+ <TextView
52
+
53
+ android:id="@+id/message"
54
+
55
+ android:layout_width="wrap_content"
56
+
57
+ android:layout_height="21dp"
58
+
59
+ android:layout_marginTop="12dp"
60
+
61
+ android:autoText="false"
62
+
63
+ android:text="@string/game_title"
64
+
65
+ android:textAppearance="@style/TextAppearance.AppCompat.Body2"
66
+
67
+ android:textSize="12sp"
68
+
69
+ app:fontFamily="serif"
70
+
71
+ app:layout_constraintTop_toTopOf="parent"
72
+
73
+ tools:layout_editor_absoluteX="16dp" />
74
+
75
+
76
+
77
+ <android.support.design.widget.BottomNavigationView
78
+
79
+ android:id="@+id/navigation"
80
+
81
+ android:layout_width="0dp"
82
+
83
+ android:layout_height="56dp"
84
+
85
+ android:layout_marginBottom="8dp"
86
+
87
+ android:addStatesFromChildren="true"
88
+
89
+ android:background="#733c5b81"
90
+
91
+ app:layout_constraintBottom_toBottomOf="parent"
92
+
93
+ app:layout_constraintEnd_toEndOf="parent"
94
+
95
+ app:layout_constraintLeft_toLeftOf="parent"
96
+
97
+ app:layout_constraintRight_toRightOf="parent"
98
+
99
+ app:menu="@menu/navigation">
100
+
101
+
102
+
103
+ </android.support.design.widget.BottomNavigationView>
104
+
105
+
106
+
107
+ <LinearLayout
108
+
109
+ android:layout_width="288dp"
110
+
111
+ android:layout_height="37dp"
112
+
113
+ android:orientation="horizontal"
114
+
115
+ tools:layout_editor_absoluteX="48dp"
116
+
117
+ tools:layout_editor_absoluteY="33dp">
118
+
119
+
120
+
121
+ <ImageView
122
+
123
+ android:id="@+id/imageView8"
124
+
125
+ android:layout_width="match_parent"
126
+
127
+ android:layout_height="match_parent"
128
+
129
+ android:layout_weight="1"
130
+
131
+ app:srcCompat="@drawable/hatena" />
132
+
133
+
134
+
135
+ <ImageView
136
+
137
+ android:id="@+id/imageView7"
138
+
139
+ android:layout_width="wrap_content"
140
+
141
+ android:layout_height="match_parent"
142
+
143
+ android:layout_weight="1"
144
+
145
+ app:srcCompat="@drawable/hatena" />
146
+
147
+
148
+
149
+ <ImageView
150
+
151
+ android:id="@+id/imageView6"
152
+
153
+ android:layout_width="match_parent"
154
+
155
+ android:layout_height="match_parent"
156
+
157
+ android:layout_weight="1"
158
+
159
+ app:srcCompat="@drawable/hatena" />
160
+
161
+
162
+
163
+ <ImageView
164
+
165
+ android:id="@+id/imageView5"
166
+
167
+ android:layout_width="match_parent"
168
+
169
+ android:layout_height="match_parent"
170
+
171
+ android:layout_weight="1"
172
+
173
+ app:srcCompat="@drawable/hatena"
174
+
175
+ tools:layout_editor_absoluteX="117dp"
176
+
177
+ tools:layout_editor_absoluteY="91dp" />
178
+
179
+
180
+
181
+ <Button
182
+
183
+ android:id="@+id/button"
184
+
185
+ android:layout_width="62dp"
186
+
187
+ android:layout_height="33dp"
188
+
189
+ android:text="On/Off"
190
+
191
+ android:textSize="10sp"
192
+
193
+ tools:layout_editor_absoluteX="306dp"
194
+
195
+ tools:layout_editor_absoluteY="37dp" />
196
+
197
+
198
+
199
+ </LinearLayout>
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ </android.support.constraint.ConstraintLayout>
210
+
211
+
212
+
213
+ ```