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

質問編集履歴

10

コード部分の改行を行いました。

2021/07/19 09:52

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -21,7 +21,6 @@
21
21
 
22
22
  ###コード
23
23
  画面遷移に関係する部分のみ記載します。
24
- 不慣れかつ現在編集しづらい環境にいるためコードの改行等が満足にできず、見づらいですがご了承ください(後ほど再度編集します)。
25
24
 
26
25
  フラグメント1とアクティビティ1が機能1、フラグメント2は機能2に該当します。
27
26
  アプリ起動時にフラグメント1が表示され、アクティビティ1に遷移する前はフラグメント2と横スワイプで移動できます。しかしアクティビティ1に遷移するとフラグメント2にスワイプで遷移できなくなります。
@@ -33,16 +32,16 @@
33
32
  class SamplePagerAdapter(fm: FragmentManager, private val fragmentList: List<Fragment>) :
34
33
  FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
35
34
 
36
- // 表示するフラグメントを制御する
35
+ // 表示するフラグメントを制御する
37
- override fun getItem(position: Int): Fragment {
36
+ override fun getItem(position: Int): Fragment {
38
- return fragmentList[position]
37
+ return fragmentList[position]
39
- }
38
+ }
40
39
 
41
- // viewPagerにセットするコンテンツ(フラグメントリスト)のサイズ
40
+ // viewPagerにセットするコンテンツ(フラグメントリスト)のサイズ
42
- override fun getCount(): Int {
41
+ override fun getCount(): Int {
43
- return fragmentList.size
42
+ return fragmentList.size
43
+ }
44
44
  }
45
- }
46
45
  ```
47
46
 
48
47
  MainActivity
@@ -50,54 +49,75 @@
50
49
  class MainActivity : AppCompatActivity() {
51
50
  override fun onCreate(savedInstanceState: Bundle?) {
52
51
 
53
- super.onCreate(savedInstanceState)
52
+ super.onCreate(savedInstanceState)
54
- setContentView(R.layout.activity_main)
53
+ setContentView(R.layout.activity_main)
55
54
 
56
- val fragmentList = arrayListOf<Fragment>(
55
+ val fragmentList = arrayListOf<Fragment>(
57
- view1Fragment(),
56
+ view1Fragment(),
58
- view2Fragment(),
57
+ view2Fragment(),
59
- )
58
+ )
60
59
 
61
- /// adapterのインスタンス生成
60
+ /// adapterのインスタンス生成
62
- val adapter = SamplePagerAdapter(supportFragmentManager, fragmentList)
61
+ val adapter = SamplePagerAdapter(supportFragmentManager, fragmentList)
63
- /// adapterをセット
62
+ /// adapterをセット
64
- viewPager.adapter = adapter
63
+ viewPager.adapter = adapter
65
- }
64
+ }
66
65
 
67
- fun moving(view: View){
66
+ fun moving(view: View){
68
- val intent = Intent(this,Activity1::class.java)
67
+ val intent = Intent(this,Activity1::class.java)
69
- startActivity(intent)
68
+ startActivity(intent)
70
- overridePendingTransition(0,0)
69
+ overridePendingTransition(0,0)
70
+ }
71
71
  }
72
- }
73
72
  ```
74
73
  MainActivity レイアウト
75
74
  ```Kotlin
76
75
 
77
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f9f0e2">
76
+ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
77
+ xmlns:app="http://schemas.android.com/apk/res-auto"
78
+ xmlns:tools="http://schemas.android.com/tools"
79
+ android:layout_width="match_parent"
80
+ android:layout_height="match_parent"
81
+ android:background="#f9f0e2">
78
82
 
79
- <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
83
+ <androidx.viewpager.widget.ViewPager
84
+ android:id="@+id/viewPager"
85
+ android:layout_width="match_parent"
86
+ android:layout_height="match_parent"
87
+ app:layout_constraintBottom_toBottomOf="parent"
88
+ app:layout_constraintEnd_toEndOf="parent"
89
+ app:layout_constraintStart_toStartOf="parent"
90
+ app:layout_constraintTop_toTopOf="parent" />
80
91
  </RelativeLayout>
81
92
  ```
82
93
 
83
94
  fragment1
84
95
  ```Kotlin
85
96
  class view1Fragment : Fragment() {
86
- override fun onCreateView(
97
+ override fun onCreateView(
87
- inflater: LayoutInflater, container: ViewGroup?,
98
+ inflater: LayoutInflater, container: ViewGroup?,
88
- savedInstanceState: Bundle?
99
+ savedInstanceState: Bundle?
89
- ): View? {
100
+ ): View? {
90
- // Inflate the layout for this fragment
101
+ // Inflate the layout for this fragment
91
- return inflater.inflate(R.layout.fragment_view1, container, false)
102
+ return inflater.inflate(R.layout.fragment_view1, container, false)
103
+ }
92
104
  }
93
- }
94
105
 
95
106
  ```
96
107
  fragment1レイアウト
97
108
  ```Kotlin
98
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
109
+ <androidx.constraintlayout.widget.ConstraintLayout
110
+ xmlns:android="http://schemas.android.com/apk/res/android"
111
+ xmlns:app="http://schemas.android.com/apk/res-auto"
112
+ xmlns:tools="http://schemas.android.com/tools"
113
+ android:layout_width="match_parent"
114
+ android:layout_height="match_parent">
99
115
 
116
+ <Button
117
+ android:id="@+id/first_button"
118
+ android:layout_width="wrap_content"
100
- <Button android:id="@+id/first_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="moving"/>
119
+ android:layout_height="wrap_content"
120
+ android:onClick="moving"/>
101
121
 
102
122
  </androidx.constraintlayout.widget.ConstraintLayout>
103
123
  ```
@@ -105,16 +125,16 @@
105
125
  fragment2
106
126
  ```Kotlin
107
127
  class view2Fragment : Fragment() {
108
- override fun onCreateView(
128
+ override fun onCreateView(
109
- inflater: LayoutInflater, container: ViewGroup?,
129
+ inflater: LayoutInflater, container: ViewGroup?,
110
- savedInstanceState: Bundle?
130
+ savedInstanceState: Bundle?
111
- ): View? {
131
+ ): View? {
112
- // Inflate the layout for this fragment
132
+ // Inflate the layout for this fragment
113
- val view = inflater.inflate(R.layout.fragment_view2, container, false)
133
+ val view = inflater.inflate(R.layout.fragment_view2, container, false)
114
134
 
115
- return view
135
+ return view
136
+ }
116
137
  }
117
- }
118
138
  ```
119
139
 
120
140
  フラグメント2のレイアウトとアクティビティ1については何も無い単なる画面ということで割愛します。

9

2021/07/19 09:52

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -71,7 +71,15 @@
71
71
  }
72
72
  }
73
73
  ```
74
+ MainActivity レイアウト
75
+ ```Kotlin
74
76
 
77
+ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f9f0e2">
78
+
79
+ <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
80
+ </RelativeLayout>
81
+ ```
82
+
75
83
  fragment1
76
84
  ```Kotlin
77
85
  class view1Fragment : Fragment() {

8

2021/07/19 04:35

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
File without changes

7

2021/07/19 04:10

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -26,6 +26,8 @@
26
26
  フラグメント1とアクティビティ1が機能1、フラグメント2は機能2に該当します。
27
27
  アプリ起動時にフラグメント1が表示され、アクティビティ1に遷移する前はフラグメント2と横スワイプで移動できます。しかしアクティビティ1に遷移するとフラグメント2にスワイプで遷移できなくなります。
28
28
 
29
+ 本質問はアクティビティ1に遷移したあともフラグメント2にスワイプで遷移できるようにするにはどういった改善が必要か、ということです。
30
+
29
31
  アダプター
30
32
  ```Kotlin
31
33
  class SamplePagerAdapter(fm: FragmentManager, private val fragmentList: List<Fragment>) :

6

2021/07/19 04:10

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -21,8 +21,11 @@
21
21
 
22
22
  ###コード
23
23
  画面遷移に関係する部分のみ記載します。
24
- 不慣れかつ編集しづらい環境にいるためコードが見づらいですがご了承ください。
24
+ 不慣れかつ現在編集しづらい環境にいるためコードの改行等満足にできず、見づらいですがご了承ください(後ほど再度編集します)
25
25
 
26
+ フラグメント1とアクティビティ1が機能1、フラグメント2は機能2に該当します。
27
+ アプリ起動時にフラグメント1が表示され、アクティビティ1に遷移する前はフラグメント2と横スワイプで移動できます。しかしアクティビティ1に遷移するとフラグメント2にスワイプで遷移できなくなります。
28
+
26
29
  アダプター
27
30
  ```Kotlin
28
31
  class SamplePagerAdapter(fm: FragmentManager, private val fragmentList: List<Fragment>) :
@@ -102,4 +105,6 @@
102
105
  return view
103
106
  }
104
107
  }
105
- ```
108
+ ```
109
+
110
+ フラグメント2のレイアウトとアクティビティ1については何も無い単なる画面ということで割愛します。

5

2021/07/19 04:05

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -21,6 +21,7 @@
21
21
 
22
22
  ###コード
23
23
  画面遷移に関係する部分のみ記載します。
24
+ 不慣れかつ編集しづらい環境にいるためコードが見づらいですがご了承ください。
24
25
 
25
26
  アダプター
26
27
  ```Kotlin
@@ -67,6 +68,7 @@
67
68
  ```
68
69
 
69
70
  fragment1
71
+ ```Kotlin
70
72
  class view1Fragment : Fragment() {
71
73
  override fun onCreateView(
72
74
  inflater: LayoutInflater, container: ViewGroup?,
@@ -77,15 +79,16 @@
77
79
  }
78
80
  }
79
81
 
82
+ ```
80
83
  fragment1レイアウト
84
+ ```Kotlin
81
85
  <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
82
86
 
83
87
  <Button android:id="@+id/first_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="moving"/>
84
88
 
85
89
  </androidx.constraintlayout.widget.ConstraintLayout>
90
+ ```
86
91
 
87
- Activity1
88
-
89
92
  fragment2
90
93
  ```Kotlin
91
94
  class view2Fragment : Fragment() {

4

2021/07/19 03:59

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -19,6 +19,9 @@
19
19
  Android Studio 4.2.2
20
20
  Android 11.0(API 30)
21
21
 
22
+ ###コード
23
+ 画面遷移に関係する部分のみ記載します。
24
+
22
25
  アダプター
23
26
  ```Kotlin
24
27
  class SamplePagerAdapter(fm: FragmentManager, private val fragmentList: List<Fragment>) :
@@ -54,5 +57,46 @@
54
57
  /// adapterをセット
55
58
  viewPager.adapter = adapter
56
59
  }
60
+
61
+ fun moving(view: View){
62
+ val intent = Intent(this,Activity1::class.java)
63
+ startActivity(intent)
64
+ overridePendingTransition(0,0)
57
65
  }
66
+ }
67
+ ```
68
+
69
+ fragment1
70
+ class view1Fragment : Fragment() {
71
+ override fun onCreateView(
72
+ inflater: LayoutInflater, container: ViewGroup?,
73
+ savedInstanceState: Bundle?
74
+ ): View? {
75
+ // Inflate the layout for this fragment
76
+ return inflater.inflate(R.layout.fragment_view1, container, false)
77
+ }
78
+ }
79
+
80
+ fragment1レイアウト
81
+ <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
82
+
83
+ <Button android:id="@+id/first_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="moving"/>
84
+
85
+ </androidx.constraintlayout.widget.ConstraintLayout>
86
+
87
+ Activity1
88
+
89
+ fragment2
90
+ ```Kotlin
91
+ class view2Fragment : Fragment() {
92
+ override fun onCreateView(
93
+ inflater: LayoutInflater, container: ViewGroup?,
94
+ savedInstanceState: Bundle?
95
+ ): View? {
96
+ // Inflate the layout for this fragment
97
+ val view = inflater.inflate(R.layout.fragment_view2, container, false)
98
+
99
+ return view
100
+ }
101
+ }
58
102
  ```

3

2021/07/19 03:57

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -19,6 +19,7 @@
19
19
  Android Studio 4.2.2
20
20
  Android 11.0(API 30)
21
21
 
22
+ アダプター
22
23
  ```Kotlin
23
24
  class SamplePagerAdapter(fm: FragmentManager, private val fragmentList: List<Fragment>) :
24
25
  FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
@@ -33,4 +34,25 @@
33
34
  return fragmentList.size
34
35
  }
35
36
  }
37
+ ```
38
+
39
+ MainActivity
40
+ ```Kotlin
41
+ class MainActivity : AppCompatActivity() {
42
+ override fun onCreate(savedInstanceState: Bundle?) {
43
+
44
+ super.onCreate(savedInstanceState)
45
+ setContentView(R.layout.activity_main)
46
+
47
+ val fragmentList = arrayListOf<Fragment>(
48
+ view1Fragment(),
49
+ view2Fragment(),
50
+ )
51
+
52
+ /// adapterのインスタンス生成
53
+ val adapter = SamplePagerAdapter(supportFragmentManager, fragmentList)
54
+ /// adapterをセット
55
+ viewPager.adapter = adapter
56
+ }
57
+ }
36
58
  ```

2

2021/07/19 03:42

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
File without changes
body CHANGED
@@ -17,4 +17,20 @@
17
17
  ### 補足情報(FW/ツールのバージョンなど)
18
18
 
19
19
  Android Studio 4.2.2
20
- Android 11.0(API 30)
20
+ Android 11.0(API 30)
21
+
22
+ ```Kotlin
23
+ class SamplePagerAdapter(fm: FragmentManager, private val fragmentList: List<Fragment>) :
24
+ FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
25
+
26
+ // 表示するフラグメントを制御する
27
+ override fun getItem(position: Int): Fragment {
28
+ return fragmentList[position]
29
+ }
30
+
31
+ // viewPagerにセットするコンテンツ(フラグメントリスト)のサイズ
32
+ override fun getCount(): Int {
33
+ return fragmentList.size
34
+ }
35
+ }
36
+ ```

1

2021/07/19 03:39

投稿

RgrayLemon
RgrayLemon

スコア2

title CHANGED
@@ -1,1 +1,1 @@
1
- Android Kotlin
1
+ Android Kotlin 横スワイプによる画面遷移について
body CHANGED
File without changes