回答編集履歴
1
コード追加
test
CHANGED
@@ -1,3 +1,168 @@
|
|
1
1
|
> どこが足りないのか
|
2
2
|
|
3
3
|
要所にログの出力を入れる等をし、何が起きているのか、どこまで想定通りに動作しているか、変数等がどこまで想定通りの値になっているか等の情報を集め、想定通りになっていない個所を見つけては修正もしくは作り変えることを繰り替えしていくこと(=デバッグ)が足りていないものと思います。
|
4
|
+
|
5
|
+
---
|
6
|
+
※クラス名等変更しています。
|
7
|
+
|
8
|
+
MainActivity.java
|
9
|
+
```java
|
10
|
+
import android.os.Bundle;
|
11
|
+
|
12
|
+
import androidx.appcompat.app.AppCompatActivity;
|
13
|
+
|
14
|
+
public class MainActivity extends AppCompatActivity {
|
15
|
+
@Override
|
16
|
+
protected void onCreate(Bundle savedInstanceState) {
|
17
|
+
super.onCreate(savedInstanceState);
|
18
|
+
setContentView(R.layout.activity_main);
|
19
|
+
|
20
|
+
//Fragmentを作成・設定する
|
21
|
+
getSupportFragmentManager().beginTransaction()
|
22
|
+
.replace(R.id.fragment_container, new MainFragment())
|
23
|
+
.commit();
|
24
|
+
}
|
25
|
+
}
|
26
|
+
```
|
27
|
+
res/layout/activity_main.xml
|
28
|
+
```xml
|
29
|
+
<?xml version="1.0" encoding="utf-8"?>
|
30
|
+
<LinearLayout
|
31
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
32
|
+
android:id="@+id/fragment_container"
|
33
|
+
android:layout_width="match_parent"
|
34
|
+
android:layout_height="match_parent"
|
35
|
+
android:orientation="vertical" />
|
36
|
+
```
|
37
|
+
MainFragment.java
|
38
|
+
```java
|
39
|
+
import android.os.Bundle;
|
40
|
+
import android.view.*;
|
41
|
+
|
42
|
+
import androidx.annotation.*;
|
43
|
+
import androidx.fragment.app.Fragment;
|
44
|
+
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
45
|
+
import androidx.viewpager2.widget.ViewPager2;
|
46
|
+
|
47
|
+
import com.google.android.material.tabs.*;
|
48
|
+
|
49
|
+
public class MainFragment extends Fragment {
|
50
|
+
public MainFragment() {
|
51
|
+
super(R.layout.fragment_main);
|
52
|
+
}
|
53
|
+
|
54
|
+
@Override
|
55
|
+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
56
|
+
FragmentsAdapter fragmentsAdapter = new FragmentsAdapter();
|
57
|
+
|
58
|
+
ViewPager2 viewPager2 = view.findViewById(R.id.view_pager);
|
59
|
+
viewPager2.setAdapter(fragmentsAdapter);
|
60
|
+
|
61
|
+
// タブを追加する
|
62
|
+
TabLayout tabLayout = view.findViewById(R.id.tabs);
|
63
|
+
new TabLayoutMediator(tabLayout, viewPager2, fragmentsAdapter).attach();
|
64
|
+
}
|
65
|
+
|
66
|
+
public class FragmentsAdapter extends FragmentStateAdapter implements TabLayoutMediator.TabConfigurationStrategy {
|
67
|
+
public FragmentsAdapter() {
|
68
|
+
super(MainFragment.this);
|
69
|
+
}
|
70
|
+
|
71
|
+
@NonNull
|
72
|
+
@Override
|
73
|
+
public Fragment createFragment(int position) {
|
74
|
+
return ContentsFragment.newInstance(position + 1);
|
75
|
+
}
|
76
|
+
|
77
|
+
@Override
|
78
|
+
public int getItemCount() {
|
79
|
+
return 10;
|
80
|
+
}
|
81
|
+
|
82
|
+
@Override
|
83
|
+
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
|
84
|
+
tab.setText("OBJECT " + (position + 1));
|
85
|
+
}
|
86
|
+
}
|
87
|
+
}
|
88
|
+
```
|
89
|
+
res/layout/fragment_main.xml
|
90
|
+
```xml
|
91
|
+
<?xml version="1.0" encoding="utf-8"?>
|
92
|
+
<LinearLayout
|
93
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
94
|
+
xmlns:tools="http://schemas.android.com/tools"
|
95
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
96
|
+
android:layout_width="match_parent"
|
97
|
+
android:layout_height="match_parent"
|
98
|
+
android:orientation="vertical"
|
99
|
+
tools:context=".MainFragment">
|
100
|
+
|
101
|
+
<com.google.android.material.tabs.TabLayout
|
102
|
+
android:id="@+id/tabs"
|
103
|
+
android:layout_width="match_parent"
|
104
|
+
android:layout_height="wrap_content"
|
105
|
+
app:tabMode="scrollable" />
|
106
|
+
|
107
|
+
<androidx.viewpager2.widget.ViewPager2
|
108
|
+
android:id="@+id/view_pager"
|
109
|
+
android:layout_width="match_parent"
|
110
|
+
android:layout_height="0dp"
|
111
|
+
android:layout_weight="1" />
|
112
|
+
|
113
|
+
</LinearLayout>
|
114
|
+
```
|
115
|
+
ContentsFragment.java
|
116
|
+
```java
|
117
|
+
import android.os.Bundle;
|
118
|
+
import android.view.*;
|
119
|
+
import android.widget.TextView;
|
120
|
+
|
121
|
+
import androidx.annotation.*;
|
122
|
+
import androidx.fragment.app.Fragment;
|
123
|
+
|
124
|
+
public class ContentsFragment extends Fragment {
|
125
|
+
private static final String ARG_OBJECT = "object";
|
126
|
+
|
127
|
+
public static ContentsFragment newInstance(int object) {
|
128
|
+
ContentsFragment fragment = new ContentsFragment();
|
129
|
+
Bundle args = new Bundle();
|
130
|
+
args.putInt(ContentsFragment.ARG_OBJECT, object);
|
131
|
+
fragment.setArguments(args);
|
132
|
+
return fragment;
|
133
|
+
}
|
134
|
+
|
135
|
+
public ContentsFragment() {
|
136
|
+
super(R.layout.fragment_contents);
|
137
|
+
}
|
138
|
+
|
139
|
+
@Override
|
140
|
+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
141
|
+
Bundle args = getArguments();
|
142
|
+
((TextView)view.findViewById(R.id.textView)).setText(""+args.getInt(ARG_OBJECT));
|
143
|
+
}
|
144
|
+
}
|
145
|
+
```
|
146
|
+
res/layout/fragment_contents.xml
|
147
|
+
```xml
|
148
|
+
<?xml version="1.0" encoding="utf-8"?>
|
149
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
150
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
151
|
+
xmlns:tools="http://schemas.android.com/tools"
|
152
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
153
|
+
android:layout_width="match_parent"
|
154
|
+
android:layout_height="match_parent"
|
155
|
+
tools:context=".ContentsFragment">
|
156
|
+
|
157
|
+
<TextView
|
158
|
+
android:id="@+id/textView"
|
159
|
+
android:layout_width="match_parent"
|
160
|
+
android:layout_height="match_parent"
|
161
|
+
android:text="TextView"
|
162
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
163
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
164
|
+
app:layout_constraintRight_toRightOf="parent"
|
165
|
+
app:layout_constraintTop_toTopOf="parent" />
|
166
|
+
|
167
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
168
|
+
```
|