回答編集履歴

3

2022/01/15 17:37

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -1,83 +1,48 @@
1
1
  メソッドにする、itemId と各リソースIDとの関連をテーブル化する等、やり方は色々と思います。
2
2
 
3
+ ```kotlin
4
+ class ChampionViews(val imageView: ImageView, val textView: TextView) {
5
+ private class Preset(val imageResId: Int, val stringResId: Int, val text: String?)
6
+ private val presetMap: MutableMap<Int, Preset> = HashMap()
3
7
 
8
+ fun putPreset(key: Int, imageResId: Int, strResId: Int): ChampionViews {
9
+ presetMap[key] = Preset(imageResId, strResId, null)
10
+ return this
11
+ }
12
+ fun putPreset(key: Int, imageResId: Int, str: String): ChampionViews {
13
+ presetMap[key] = Preset(imageResId, 0, str)
14
+ return this
15
+ }
16
+ fun selectPreset(key: Int): Boolean {
17
+ val preset = presetMap[key] ?: return false
18
+ imageView.setImageResource(preset.imageResId)
19
+ if(preset.stringResId == 0) {
20
+ textView.text = preset.text
21
+ } else {
22
+ textView.setText(preset.stringResId)
23
+ }
24
+ return true
25
+ }
26
+ }
4
27
 
28
+ class MainActivity : AppCompatActivity() {
5
- View も纏めて管理するならこんな風にも。
29
+ lateinit var championViews: ChampionViews
6
30
 
31
+ override fun onCreate(savedInstanceState: Bundle?) {
32
+ super.onCreate(savedInstanceState)
7
- ```kotlin
33
+ setContentView(R.layout.activity_main)
8
34
 
9
- class ChampionViews(private val imageView: ImageView, private val textView: TextView) {
35
+ championViews = ChampionViews(findViewById(R.id.championsImage), findViewById(R.id.championsText))
10
-
36
+ .putPreset(R.id.home, R.drawable.icon, "")
37
+ .putPreset(R.id.Aatrox, R.drawable.aatrox, R.string.Aatrox_text)
38
+ .putPreset(R.id.Ahri, R.drawable.ahri, R.string.Ahri_text)
11
- private class MenuValue(val imageId: Int, val stringId: Int)
39
+ .putPreset(R.id.Akali, R.drawable.akali, R.string.Akali_text)
12
-
13
- private val menuMap: MutableMap<Int, MenuValue> = HashMap()
14
-
15
-
16
-
17
- fun putMenuValue(menuId: Int, imageId: Int, stringId: Int): ChampionViews {
18
-
19
- menuMap[menuId] = MenuValue(imageId, stringId)
20
-
21
- return this
40
+ //...
22
-
23
41
  }
24
42
 
25
-
26
-
27
- fun setViews(menuId: Int): Boolean {
43
+ override fun onOptionsItemSelected(item: MenuItem): Boolean {
28
-
29
- val value = menuMap[menuId] ?: return false
30
-
31
- imageView.setImageResource(value.imageId)
44
+ return if(championViews.selectPreset(item.itemId)) true
32
-
33
- textView.setText(value.stringId)
45
+ else super.onOptionsItemSelected(item)
34
-
35
- return true
36
-
37
46
  }
38
-
39
47
  }
40
-
41
-
42
-
43
- class MainActivity : AppCompatActivity() {
44
-
45
- lateinit var championViews: ChampionViews
46
-
47
-
48
-
49
- override fun onCreate(savedInstanceState: Bundle?) {
50
-
51
- super.onCreate(savedInstanceState)
52
-
53
- setContentView(R.layout.activity_main)
54
-
55
-
56
-
57
- championViews = ChampionViews(findViewById(R.id.championsImage), findViewById(R.id.championsText))
58
-
59
- .putMenuValue(R.id.home, R.drawable.icon, R.string.home_text) //R.string.home_text を "" として定義
60
-
61
- .putMenuValue(R.id.Aatrox, R.drawable.aatrox, R.string.Aatrox_text)
62
-
63
- .putMenuValue(R.id.Ahri, R.drawable.ahri, R.string.Ahri_text)
64
-
65
- .putMenuValue(R.id.Akali, R.drawable.akali, R.string.Akali_text)
66
-
67
- //...
68
-
69
- }
70
-
71
-
72
-
73
- override fun onOptionsItemSelected(item: MenuItem): Boolean {
74
-
75
- return if(championViews.setViews(item.itemId)) true
76
-
77
- else super.onOptionsItemSelected(item)
78
-
79
- }
80
-
81
- }
82
-
83
48
  ```

2

追記

2022/01/04 09:58

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -1,6 +1,8 @@
1
1
  メソッドにする、itemId と各リソースIDとの関連をテーブル化する等、やり方は色々と思います。
2
2
 
3
3
 
4
+
5
+ View も纏めて管理するならこんな風にも。
4
6
 
5
7
  ```kotlin
6
8
 

1

コード追加

2022/01/04 09:58

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -1 +1,81 @@
1
1
  メソッドにする、itemId と各リソースIDとの関連をテーブル化する等、やり方は色々と思います。
2
+
3
+
4
+
5
+ ```kotlin
6
+
7
+ class ChampionViews(private val imageView: ImageView, private val textView: TextView) {
8
+
9
+ private class MenuValue(val imageId: Int, val stringId: Int)
10
+
11
+ private val menuMap: MutableMap<Int, MenuValue> = HashMap()
12
+
13
+
14
+
15
+ fun putMenuValue(menuId: Int, imageId: Int, stringId: Int): ChampionViews {
16
+
17
+ menuMap[menuId] = MenuValue(imageId, stringId)
18
+
19
+ return this
20
+
21
+ }
22
+
23
+
24
+
25
+ fun setViews(menuId: Int): Boolean {
26
+
27
+ val value = menuMap[menuId] ?: return false
28
+
29
+ imageView.setImageResource(value.imageId)
30
+
31
+ textView.setText(value.stringId)
32
+
33
+ return true
34
+
35
+ }
36
+
37
+ }
38
+
39
+
40
+
41
+ class MainActivity : AppCompatActivity() {
42
+
43
+ lateinit var championViews: ChampionViews
44
+
45
+
46
+
47
+ override fun onCreate(savedInstanceState: Bundle?) {
48
+
49
+ super.onCreate(savedInstanceState)
50
+
51
+ setContentView(R.layout.activity_main)
52
+
53
+
54
+
55
+ championViews = ChampionViews(findViewById(R.id.championsImage), findViewById(R.id.championsText))
56
+
57
+ .putMenuValue(R.id.home, R.drawable.icon, R.string.home_text) //R.string.home_text を "" として定義
58
+
59
+ .putMenuValue(R.id.Aatrox, R.drawable.aatrox, R.string.Aatrox_text)
60
+
61
+ .putMenuValue(R.id.Ahri, R.drawable.ahri, R.string.Ahri_text)
62
+
63
+ .putMenuValue(R.id.Akali, R.drawable.akali, R.string.Akali_text)
64
+
65
+ //...
66
+
67
+ }
68
+
69
+
70
+
71
+ override fun onOptionsItemSelected(item: MenuItem): Boolean {
72
+
73
+ return if(championViews.setViews(item.itemId)) true
74
+
75
+ else super.onOptionsItemSelected(item)
76
+
77
+ }
78
+
79
+ }
80
+
81
+ ```