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