質問するログイン新規登録

質問編集履歴

2

XMLファイルも追記しました

2020/08/21 12:32

投稿

youha
youha

スコア1

title CHANGED
File without changes
body CHANGED
@@ -104,7 +104,44 @@
104
104
  }
105
105
  }
106
106
  ```
107
+ ```activity_main.xml
108
+ <?xml version="1.0" encoding="utf-8"?>
109
+ <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
110
+ xmlns:app="http://schemas.android.com/apk/res-auto"
111
+ xmlns:tools="http://schemas.android.com/tools"
112
+ android:layout_width="match_parent"
113
+ android:layout_height="match_parent"
114
+ tools:context=".MainActivity">
107
115
 
116
+ <androidx.viewpager2.widget.ViewPager2
117
+ android:id="@+id/pager"
118
+ android:layout_width="0dp"
119
+ android:layout_height="0dp"
120
+ app:layout_constraintBottom_toBottomOf="parent"
121
+ app:layout_constraintEnd_toEndOf="parent"
122
+ app:layout_constraintStart_toStartOf="parent"
123
+ app:layout_constraintTop_toTopOf="parent" />
124
+ </androidx.constraintlayout.widget.ConstraintLayout>
125
+ ```
126
+ ```fragment_image.xml
127
+ <?xml version="1.0" encoding="utf-8"?>
128
+ <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
129
+ xmlns:app="http://schemas.android.com/apk/res-auto"
130
+ xmlns:tools="http://schemas.android.com/tools"
131
+ android:layout_width="match_parent"
132
+ android:layout_height="match_parent"
133
+ tools:context=".ImageFragment">
134
+
135
+ <ImageView
136
+ android:id="@+id/imageView"
137
+ android:layout_width="match_parent"
138
+ android:layout_height="match_parent"
139
+ android:scaleType="centerCrop"
140
+ tools:src="@tools:sample/backgrounds/scenic" />
141
+ </FrameLayout>
142
+ ```
143
+
144
+
108
145
  ### 試したこと
109
146
  AdapterにBEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENTというTagを付けました
110
147
 

1

ソースコードが足りなかったため

2020/08/21 12:32

投稿

youha
youha

スコア1

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,7 @@
4
4
  書籍の情報が古いのもあって、非推奨になっているものも多数ありました、現在の書き方等わからないことがたくさんあるため、どなたか教えていただけませんか?
5
5
 
6
6
  フラグメントを用いたスライドショーアプリを作っています
7
- ViewPagerとViewAdapterを関連付けるところでつまっています(一番下の箇所)
7
+ ViewPagerとViewAdapterを関連付けるところでつまっています(MainActivityの一番下の箇所)
8
8
 
9
9
  ### 発生している問題・エラーメッセージ
10
10
 
@@ -16,7 +16,7 @@
16
16
 
17
17
  ### 該当のソースコード
18
18
 
19
- ```Kotlin
19
+ ```MainActivity
20
20
  import androidx.appcompat.app.AppCompatActivity
21
21
  import android.os.Bundle
22
22
  import androidx.fragment.app.Fragment
@@ -53,6 +53,58 @@
53
53
  }
54
54
  ```
55
55
 
56
+ ```ImageFragment
57
+ package com.websarva.wings.android.myslidesshow
58
+
59
+ import android.os.Bundle
60
+ import androidx.fragment.app.Fragment
61
+ import android.view.LayoutInflater
62
+ import android.view.View
63
+ import android.view.ViewGroup
64
+ import kotlinx.android.synthetic.main.fragment_image.*
65
+
66
+ val IMG_RES_ID = "IMG_RES_ID"//Bundleクラスで値を保存するために使用するkey
67
+
68
+ class ImageFragment : Fragment() {
69
+
70
+ override fun onCreateView(
71
+ inflater: LayoutInflater, container: ViewGroup?,
72
+ savedInstanceState: Bundle?
73
+ ): View? {
74
+ // Inflate the layout for this fragment
75
+ return inflater.inflate(R.layout.fragment_image, container, false)
76
+ }
77
+
78
+ companion object {// Javaでいうstatic methodで、このクラスImageFragment内でのmethod
79
+ //ImageFragment内のImageViewに関するリソースIDを取得して、生成したimageFragmentのインスタンスを返す
80
+ fun newInstance(imageResourceID: Int) : ImageFragment {
81
+ val bundle = Bundle()
82
+ bundle.putInt(IMG_RES_ID, imageResourceID)//上で生成したBundleインスタンスにidを書き込んでいる
83
+ val imageFragment = ImageFragment()
84
+ imageFragment.arguments = bundle//フラグメントのデータをargumentsに保存する
85
+ return imageFragment
86
+ }
87
+ }
88
+
89
+ private var imgResID: Int? = null
90
+
91
+ override fun onCreate(savedInstanceState: Bundle?) {//Bundleから値を取り出す処理
92
+ super.onCreate(savedInstanceState)
93
+ arguments?.let{//安全よびだし演算子とスコープ関数()letを使用して、nullではないことを確認している
94
+ //letを使うことで、スコープ(特定の名前で参照される範囲を限定)でき、いちいちarguments?.getIntと書かずにit.getIntと書くことができる
95
+ imgResID = it.getInt(IMG_RES_ID)
96
+ }
97
+ }
98
+
99
+ override fun onActivityCreated(savedInstanceState: Bundle?) {
100
+ super.onActivityCreated(savedInstanceState)
101
+ imgResID?.let {
102
+ imageView.setImageResource(it)//imgResIdに保存しておいた画像リソースIDを使用して、フラグメント内のImageViewに画像を設定
103
+ }
104
+ }
105
+ }
106
+ ```
107
+
56
108
  ### 試したこと
57
109
  AdapterにBEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENTというTagを付けました
58
110