質問編集履歴

2

該当箇所と思われるコードの編集

2019/08/19 12:35

投稿

kitarou
kitarou

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,8 @@
1
+ ```ここに言語を入力
2
+
3
+ コード
4
+
1
- Android Studio で、言語はKotlinを使用して開発をしています。
5
+ ```Android Studio で、言語はKotlinを使用して開発をしています。
2
6
 
3
7
  Listviewに配置したswitchの動きがリスナーでとれないので困っています。
4
8
 
@@ -88,11 +92,407 @@
88
92
 
89
93
  }'`````````
90
94
 
95
+
96
+
97
+
98
+
99
+
100
+
101
+ これがListの1行レイアウトです。
102
+
103
+ ```
104
+
105
+ <?xml version="1.0" encoding="utf-8"?>
106
+
107
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
108
+
109
+ xmlns:app="http://schemas.android.com/apk/res-auto"
110
+
111
+ android:orientation="horizontal"
112
+
113
+ android:layout_width="match_parent"
114
+
115
+ android:layout_height="match_parent">
116
+
117
+ <ImageView
118
+
119
+ android:id="@+id/image"
120
+
121
+ android:layout_width="54dp"
122
+
123
+ android:layout_height="50dp"/>
124
+
125
+ <TextView
126
+
127
+ android:text="TextView"
128
+
129
+ android:layout_width="161dp"
130
+
131
+ android:layout_height="46dp"
132
+
133
+ android:id="@+id/title"
134
+
135
+ android:layout_weight="1"/>
136
+
137
+ <Switch
138
+
139
+ android:text="Switch"
140
+
141
+ android:layout_width="164dp"
142
+
143
+ android:layout_height="53dp"
144
+
145
+ android:id="@+id/switch_1"
146
+
147
+ android:layout_weight="1"
148
+
149
+ android:checked="true"
150
+
151
+ android:focusable="false"
152
+
153
+ android:layout_marginRight="30dp"/>
154
+
155
+ </LinearLayout>
156
+
157
+ ```
158
+
159
+
160
+
161
+ ここがlistを表示するActivityの 全文です。
162
+
163
+
164
+
165
+ 本来setOnItemClickListener にタップされた時の動きをかくと思うのですが、switchだとadapterに書いている記事を見かけたので、同じくそちらに書いているのですが間違えているでしょうか。
166
+
167
+
168
+
91
- ここに言語を入力
169
+ ```ここに言語を入力
170
+
92
-
171
+ class Webview_indexActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+ override fun onCreate(savedInstanceState: Bundle?) {
186
+
187
+ super.onCreate(savedInstanceState)
188
+
189
+ setContentView(R.layout.activity_webview_index)
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+ val switch = Switch(this)
202
+
203
+
204
+
205
+
206
+
207
+ switch.setOnCheckedChangeListener({ button, isChecked ->
208
+
209
+
210
+
211
+ val text = if (isChecked) "checked" else "unchecked"
212
+
213
+ Toast.makeText(this, text, Toast.LENGTH_LONG).show()
214
+
215
+ })
216
+
217
+
218
+
219
+
220
+
221
+ //val switchmenu = nav_view.menu.findItem(R.id.navigation_notifications)
222
+
223
+ //
224
+
225
+ // switchmenu.actionView = switch
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+ mutableListOf<MenuItem>()
240
+
241
+
242
+
243
+ val menuItem_right = mutableListOf<MenuItem>()
244
+
245
+ menuItem_right.add(MenuItem("設定",R.drawable.button_category_clothes_icon))
246
+
247
+ menuItem_right.add(MenuItem("設定",R.drawable.button_category_food_icon))
248
+
249
+ menuItem_right.add(MenuItem("設定",R.drawable.button_category_hobby_icon))
250
+
251
+
252
+
253
+ nav_menu_items.adapter = MenuItemAdapter(this,menuItem_right)
254
+
255
+
256
+
257
+ nav_menu_items.setOnItemClickListener {parent, view, position, id ->
258
+
259
+
260
+
261
+ Toast.makeText(this, "Clicked: ${id}", Toast.LENGTH_SHORT).show()
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+ drawer_layout.closeDrawer(Gravity.LEFT)
270
+
271
+ drawer_layout.closeDrawer(Gravity.RIGHT)
272
+
273
+
274
+
275
+ }
276
+
277
+
278
+
279
+ //左側の switchのlistの設定
280
+
281
+
282
+
283
+ mutableListOf<MenuItem_switch>()
284
+
285
+ val menuItem_left_switch = mutableListOf<MenuItem_switch>()
286
+
287
+
288
+
289
+ menuItem_left_switch.add(MenuItem_switch("設定",switch))
290
+
291
+ menuItem_left_switch.add(MenuItem_switch("設定",switch))
292
+
293
+ menuItem_left_switch.add(MenuItem_switch("設定",switch))
294
+
295
+
296
+
297
+ nav_menu_items_switch.setOnItemClickListener { parent, view, position, id ->
298
+
299
+
300
+
301
+ drawer_layout.closeDrawer(Gravity.LEFT)
302
+
303
+ drawer_layout.closeDrawer(Gravity.RIGHT)
304
+
305
+
306
+
307
+ }
308
+
309
+
310
+
311
+
312
+
313
+ //左側の通常のlistの設定
314
+
315
+ mutableListOf<MenuItem_normal>()
316
+
317
+ val menuItem_left_normal = mutableListOf<MenuItem_normal>()
318
+
319
+
320
+
321
+ menuItem_left_normal.add(MenuItem_normal("設定",R.drawable.button_category_clothes_icon))
322
+
323
+ menuItem_left_normal.add(MenuItem_normal("設定",R.drawable.button_category_clothes_icon))
324
+
325
+ menuItem_left_normal.add(MenuItem_normal("設定",R.drawable.button_category_clothes_icon))
326
+
327
+
328
+
329
+ nav_menu_items_switch.setOnItemClickListener { parent, view, position, id ->
330
+
331
+
332
+
333
+ Toast.makeText(this, "Clicked: ${id}", Toast.LENGTH_SHORT).show()
334
+
335
+
336
+
337
+ drawer_layout.closeDrawer(Gravity.LEFT)
338
+
339
+ drawer_layout.closeDrawer(Gravity.RIGHT)
340
+
341
+
342
+
343
+ }
344
+
345
+
346
+
347
+
348
+
349
+ nav_view.setNavigationItemSelectedListener(this)
350
+
351
+ nav_view_right.setNavigationItemSelectedListener(this)
352
+
353
+
354
+
355
+
356
+
357
+
358
+
359
+ right_btn.setOnClickListener {
360
+
361
+ nav_view_right.setBackgroundResource(R.drawable.pic_rightbar_bg2)
362
+
363
+ this.drawer_layout.openDrawer(nav_view_right)
364
+
365
+
366
+
367
+ }
368
+
369
+
370
+
371
+ menuBtn.setOnClickListener {
372
+
373
+ nav_view.setBackgroundResource(R.drawable.pic_category_bg)
374
+
375
+ nav_menu_items_switch.adapter = MenuItem_switchAdapter(this,menuItem_left_switch)
376
+
377
+
378
+
379
+ this.drawer_layout.openDrawer(nav_view)
380
+
381
+
382
+
383
+
384
+
385
+ }
386
+
387
+
388
+
389
+ menu2Btn.setOnClickListener {
390
+
391
+ nav_view.setBackgroundResource(R.drawable.pic_detailed_search_bg)
392
+
393
+ nav_menu_items_switch.adapter = MenuItem_normalAdapter(this,menuItem_left_normal)
394
+
395
+ this.drawer_layout.openDrawer(nav_view)
396
+
397
+ }
398
+
399
+ }
400
+
401
+
402
+
403
+ override fun onNavigationItemSelected(item: android.view.MenuItem): Boolean {
404
+
405
+
406
+
407
+ when (item.itemId) {
408
+
409
+ R.id.navigation_home -> Toast.makeText(this,"テスト表示1",Toast.LENGTH_LONG).show()
410
+
411
+ R.id.navigation_dashboard -> Toast.makeText(this,"テスト表示2",Toast.LENGTH_LONG).show()
412
+
413
+ R.id.navigation_notifications -> Toast.makeText(this,"テスト表示3",Toast.LENGTH_LONG).show()
414
+
415
+ R.id.config1 -> Toast.makeText(this,"テスト表示4",Toast.LENGTH_LONG).show()
416
+
417
+ R.id.config2 -> Toast.makeText(this,"テスト表示5",Toast.LENGTH_LONG).show()
418
+
419
+ }
420
+
421
+
422
+
423
+ drawer_layout.closeDrawer(Gravity.LEFT)
424
+
425
+ drawer_layout.closeDrawer(Gravity.RIGHT)
426
+
427
+ return true
428
+
429
+
430
+
431
+ }
432
+
433
+
434
+
435
+ override fun onCreateOptionsMenu(menu: Menu?): Boolean {
436
+
437
+
438
+
439
+ menuInflater.inflate(R.menu.bottom_navigation_item,menu)
440
+
441
+ return super.onCreateOptionsMenu(menu)
442
+
443
+ }
444
+
445
+
446
+
447
+
448
+
449
+ }
450
+
451
+
452
+
453
+
454
+
455
+ ```
456
+
457
+
458
+
459
+ 1行のモデルクラス
460
+
461
+
462
+
463
+ ```
464
+
465
+ import android.media.Image
466
+
467
+ import android.widget.ImageView
468
+
469
+ import android.widget.Switch
470
+
471
+
472
+
93
- Kotlin これがLIstAdapterです。
473
+ class MenuItem_switch (
474
+
94
-
475
+ val title2: String,
476
+
477
+ val switch2: Switch
478
+
479
+ )
480
+
481
+
482
+
483
+
484
+
485
+ ```
486
+
487
+
488
+
489
+
490
+
491
+
492
+
493
+
494
+
95
- ここに言語を入力
495
+ adapterの全文
96
496
 
97
497
  ```
98
498
 
@@ -126,6 +526,10 @@
126
526
 
127
527
 
128
528
 
529
+
530
+
531
+
532
+
129
533
  override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
130
534
 
131
535
 
@@ -136,17 +540,19 @@
136
540
 
137
541
  view_switch.title.text = items[position].title2
138
542
 
543
+
544
+
139
545
  view_switch.switch_1.setOnCheckedChangeListener{ buttonView, isChecked ->
140
546
 
141
547
  if (isChecked){
142
548
 
143
- Toast.makeText(context, "ON: ${items[position].title2}", Toast.LENGTH_SHORT).show()
549
+ Toast.makeText(context, "ON: $", Toast.LENGTH_SHORT).show()
144
550
 
145
551
 
146
552
 
147
553
  }else{
148
554
 
149
- Toast.makeText(context, "OFF: ${items[position].title2}", Toast.LENGTH_SHORT).show()
555
+ Toast.makeText(context, "OFF: ${items[position].switch2}", Toast.LENGTH_SHORT).show()
150
556
 
151
557
 
152
558
 
@@ -164,148 +570,6 @@
164
570
 
165
571
  }
166
572
 
573
+
574
+
167
- ```
575
+ ```
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
- これがListの1行レイアウトです。
178
-
179
- ```
180
-
181
- <?xml version="1.0" encoding="utf-8"?>
182
-
183
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
184
-
185
- xmlns:app="http://schemas.android.com/apk/res-auto"
186
-
187
- android:orientation="horizontal"
188
-
189
- android:layout_width="match_parent"
190
-
191
- android:layout_height="match_parent">
192
-
193
- <ImageView
194
-
195
- android:id="@+id/image"
196
-
197
- android:layout_width="54dp"
198
-
199
- android:layout_height="50dp"/>
200
-
201
- <TextView
202
-
203
- android:text="TextView"
204
-
205
- android:layout_width="161dp"
206
-
207
- android:layout_height="46dp"
208
-
209
- android:id="@+id/title"
210
-
211
- android:layout_weight="1"/>
212
-
213
- <Switch
214
-
215
- android:text="Switch"
216
-
217
- android:layout_width="164dp"
218
-
219
- android:layout_height="53dp"
220
-
221
- android:id="@+id/switch_1"
222
-
223
- android:layout_weight="1"
224
-
225
- android:checked="true"
226
-
227
- android:focusable="false"
228
-
229
- android:layout_marginRight="30dp"/>
230
-
231
- </LinearLayout>
232
-
233
- ```
234
-
235
-
236
-
237
- ここがlistを表示するActivityの onCreate() に記述したListViewのタップメソッドです。
238
-
239
- 本来setOnItemClickListener にタップされた時の動きをかくと思うのですが、switchだとadapterに書いている記事を見かけたので、同じくそちらに書いているのですが間違えているでしょうか。
240
-
241
-
242
-
243
- ```ここに言語を入力
244
-
245
-
246
-
247
-
248
-
249
- mutableListOf<MenuItem_switch>()
250
-
251
- val menuItem_left_switch = mutableListOf<MenuItem_switch>()
252
-
253
-
254
-
255
- menuItem_left_switch.add(MenuItem_switch("設定",switch_1))
256
-
257
- menuItem_left_switch.add(MenuItem_switch("設定",switch_1))
258
-
259
- menuItem_left_switch.add(MenuItem_switch("設定",switch_1))
260
-
261
-
262
-
263
- nav_menu_items_switch.setOnItemClickListener { parent, view, position, id ->
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
- drawer_layout.closeDrawer(Gravity.LEFT)
272
-
273
- drawer_layout.closeDrawer(Gravity.RIGHT)
274
-
275
-
276
-
277
- }
278
-
279
- ```
280
-
281
-
282
-
283
- 1行のモデルクラス
284
-
285
-
286
-
287
- ```
288
-
289
- import android.media.Image
290
-
291
- import android.widget.ImageView
292
-
293
- import android.widget.Switch
294
-
295
-
296
-
297
- class MenuItem_switch (
298
-
299
- val title2: String,
300
-
301
- val switch2: Switch
302
-
303
- )
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
- ```

1

必要なファイルの追加

2019/08/19 12:35

投稿

kitarou
kitarou

スコア8

test CHANGED
File without changes
test CHANGED
@@ -277,3 +277,35 @@
277
277
  }
278
278
 
279
279
  ```
280
+
281
+
282
+
283
+ 1行のモデルクラス
284
+
285
+
286
+
287
+ ```
288
+
289
+ import android.media.Image
290
+
291
+ import android.widget.ImageView
292
+
293
+ import android.widget.Switch
294
+
295
+
296
+
297
+ class MenuItem_switch (
298
+
299
+ val title2: String,
300
+
301
+ val switch2: Switch
302
+
303
+ )
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+ ```