回答編集履歴
2
コード修正
test
CHANGED
@@ -108,20 +108,11 @@
|
|
108
108
|
```
|
109
109
|
UpperFragment.java
|
110
110
|
```java
|
111
|
-
import android.os.Bundle;
|
112
|
-
import android.view.View;
|
113
|
-
|
114
|
-
import androidx.annotation.*;
|
115
111
|
import androidx.fragment.app.Fragment;
|
116
112
|
|
117
113
|
public class UpperFragment extends Fragment {
|
118
114
|
public UpperFragment() {
|
119
115
|
super(R.layout.fragment_upper);
|
120
|
-
}
|
121
|
-
|
122
|
-
@Override
|
123
|
-
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
124
|
-
super.onViewCreated(view, savedInstanceState);
|
125
116
|
}
|
126
117
|
}
|
127
118
|
```
|
1
動作する内容に変更
test
CHANGED
@@ -1,67 +1,140 @@
|
|
1
|
+
appBar とか FAB は要らなそうでしたので、画像通り(と思われる)だけでレイアウト・動作させてみました。
|
1
|
-
|
2
|
+
TabPagerManager はレイアウト質問の例としてはやり過ぎかもしれませんが…。
|
2
|
-
(幾つかリソースが参照できなかったので消しています。)
|
3
3
|
|
4
|
+
MainActivity.java
|
5
|
+
```java
|
6
|
+
import androidx.annotation.NonNull;
|
7
|
+
import androidx.appcompat.app.AppCompatActivity;
|
8
|
+
import androidx.fragment.app.*;
|
9
|
+
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
10
|
+
import androidx.viewpager2.widget.ViewPager2;
|
11
|
+
|
12
|
+
import android.os.Bundle;
|
13
|
+
|
14
|
+
import com.google.android.material.tabs.*;
|
15
|
+
|
16
|
+
import java.util.*;
|
17
|
+
import java.util.function.Supplier;
|
18
|
+
|
19
|
+
public class MainActivity extends AppCompatActivity {
|
20
|
+
@Override
|
21
|
+
protected void onCreate(Bundle savedInstanceState) {
|
22
|
+
super.onCreate(savedInstanceState);
|
23
|
+
setContentView(R.layout.activity_main);
|
24
|
+
|
25
|
+
FragmentManager fm = getSupportFragmentManager();
|
26
|
+
fm.beginTransaction().replace(R.id.upper_container, new UpperFragment()).commit();
|
27
|
+
|
28
|
+
TabLayout tabLayout = findViewById(R.id.tabLayout);
|
29
|
+
ViewPager2 viewPager = findViewById(R.id.viewPager);
|
30
|
+
new TabPagerManager(this)
|
31
|
+
.add("tab1", ()-> new TabFragment1())
|
32
|
+
.add("tab2", ()-> new TabFragment2())
|
33
|
+
.add("tab3", ()-> new TabFragment3())
|
34
|
+
.attach(tabLayout, viewPager);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
class TabPagerManager {
|
39
|
+
private static class Entry {
|
40
|
+
final String title;
|
41
|
+
final Supplier<Fragment> supplier;
|
42
|
+
Entry(String title, Supplier<Fragment> supplier) {
|
43
|
+
this.title = title;
|
44
|
+
this.supplier = supplier;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
private List<Entry> entries = new ArrayList<>();
|
49
|
+
private FragmentStateAdapter adapter;
|
50
|
+
|
51
|
+
TabPagerManager(@NonNull FragmentActivity activity) {
|
52
|
+
adapter = new FragmentStateAdapter(activity) {
|
53
|
+
@NonNull
|
54
|
+
@Override
|
55
|
+
public Fragment createFragment(int position) { return entries.get(position).supplier.get(); }
|
56
|
+
@Override
|
57
|
+
public int getItemCount() { return entries.size(); }
|
58
|
+
};
|
59
|
+
}
|
60
|
+
|
61
|
+
TabPagerManager add(@NonNull String title, @NonNull Supplier<Fragment> supplier) {
|
62
|
+
entries.add(new Entry(title, supplier));
|
63
|
+
return this;
|
64
|
+
}
|
65
|
+
|
66
|
+
void attach(@NonNull TabLayout tabLayout, @NonNull ViewPager2 viewPager) {
|
67
|
+
viewPager.setAdapter(adapter);
|
68
|
+
new TabLayoutMediator(tabLayout, viewPager, (tab,position)->tab.setText(entries.get(position).title)).attach();
|
69
|
+
}
|
70
|
+
}
|
71
|
+
```
|
72
|
+
res/layout/activity_main.xml
|
4
73
|
```xml
|
5
74
|
<?xml version="1.0" encoding="utf-8"?>
|
6
|
-
<androidx.co
|
75
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
7
76
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
8
77
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
78
|
+
xmlns:tools="http://schemas.android.com/tools"
|
9
79
|
android:layout_width="match_parent"
|
10
|
-
android:layout_height="match_parent"
|
80
|
+
android:layout_height="match_parent"
|
81
|
+
tools:context=".MainActivity">
|
11
82
|
|
12
|
-
<
|
83
|
+
<FrameLayout
|
13
|
-
android:id="@+id/
|
84
|
+
android:id="@+id/upper_container"
|
14
|
-
android:layout_width="
|
85
|
+
android:layout_width="0dp"
|
15
|
-
android:layout_height="
|
86
|
+
android:layout_height="0dp"
|
87
|
+
app:layout_constraintBottom_toTopOf="@id/tabLayout"
|
16
88
|
app:layout_constraintEnd_toEndOf="parent"
|
17
89
|
app:layout_constraintStart_toStartOf="parent"
|
18
|
-
app:layout_constraintTop_toTopOf="parent">
|
90
|
+
app:layout_constraintTop_toTopOf="parent" />
|
19
|
-
|
91
|
+
<com.google.android.material.tabs.TabLayout
|
20
|
-
|
92
|
+
android:id="@+id/tabLayout"
|
21
|
-
|
93
|
+
android:layout_width="0dp"
|
22
|
-
|
94
|
+
android:layout_height="wrap_content"
|
95
|
+
app:layout_constraintBottom_toTopOf="@id/viewPager"
|
23
|
-
|
96
|
+
app:layout_constraintEnd_toEndOf="parent"
|
24
|
-
|
97
|
+
app:layout_constraintStart_toStartOf="parent"
|
98
|
+
app:layout_constraintTop_toBottomOf="@id/upper_container" />
|
99
|
+
<androidx.viewpager2.widget.ViewPager2
|
25
|
-
|
100
|
+
android:id="@+id/viewPager"
|
101
|
+
android:layout_width="0dp"
|
102
|
+
android:layout_height="0dp"
|
103
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
104
|
+
app:layout_constraintEnd_toEndOf="parent"
|
105
|
+
app:layout_constraintStart_toStartOf="parent"
|
26
|
-
|
106
|
+
app:layout_constraintTop_toBottomOf="@id/tabLayout" />
|
27
|
-
|
107
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
108
|
+
```
|
109
|
+
UpperFragment.java
|
110
|
+
```java
|
111
|
+
import android.os.Bundle;
|
112
|
+
import android.view.View;
|
28
113
|
|
29
|
-
<androidx.constraintlayout.widget.ConstraintLayout
|
30
|
-
android:layout_width="match_parent"
|
31
|
-
android:layout_height="match_parent">
|
32
|
-
<FrameLayout
|
33
|
-
android:id="@+id/upper"
|
34
|
-
android:layout_width="0dp"
|
35
|
-
android:layout_height="0dp"
|
36
|
-
app:layout_constraintBottom_toTopOf="@id/tabLayout"
|
37
|
-
app:layout_constraintEnd_toEndOf="parent"
|
38
|
-
app:layout_constraintStart_toStartOf="parent"
|
39
|
-
app:layout_constraintTop_toTopOf="parent" />
|
40
|
-
|
114
|
+
import androidx.annotation.*;
|
41
|
-
android:id="@+id/tabLayout"
|
42
|
-
android:layout_width="0dp"
|
43
|
-
android:layout_height="wrap_content"
|
44
|
-
app:layout_constraintBottom_toTopOf="@id/viewPager"
|
45
|
-
app:layout_constraintEnd_toEndOf="parent"
|
46
|
-
app:layout_constraintStart_toStartOf="parent"
|
47
|
-
app:layout_constraintTop_toBottomOf="@id/upper" />
|
48
|
-
|
115
|
+
import androidx.fragment.app.Fragment;
|
49
|
-
android:id="@+id/viewPager"
|
50
|
-
android:layout_width="0dp"
|
51
|
-
android:layout_height="0dp"
|
52
|
-
app:layout_constraintBottom_toBottomOf="parent"
|
53
|
-
app:layout_constraintEnd_toEndOf="parent"
|
54
|
-
app:layout_constraintStart_toStartOf="parent"
|
55
|
-
app:layout_constraintTop_toBottomOf="@id/tabLayout" />
|
56
|
-
</androidx.constraintlayout.widget.ConstraintLayout>
|
57
116
|
|
58
|
-
|
117
|
+
public class UpperFragment extends Fragment {
|
59
|
-
|
118
|
+
public UpperFragment() {
|
60
|
-
|
119
|
+
super(R.layout.fragment_upper);
|
120
|
+
}
|
121
|
+
|
122
|
+
@Override
|
61
|
-
|
123
|
+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
62
|
-
|
124
|
+
super.onViewCreated(view, savedInstanceState);
|
63
|
-
|
125
|
+
}
|
64
|
-
app:srcCompat="@android:drawable/ic_dialog_email" />
|
65
|
-
|
126
|
+
}
|
66
127
|
```
|
128
|
+
res/layout/fragment_upper.xml
|
129
|
+
```xml
|
130
|
+
<?xml version="1.0" encoding="utf-8"?>
|
131
|
+
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
132
|
+
android:layout_width="match_parent"
|
133
|
+
android:layout_height="match_parent"
|
134
|
+
android:text="upper"
|
135
|
+
android:textSize="30dp"
|
136
|
+
android:background="#c0c0c0" />
|
137
|
+
```
|
138
|
+
(Tab1Fragment.java/Tab2Fragment.java/Tab3Fragment.java とそのレイアウトは、 UpperFragment の色と文字列を変えただけなので省略)
|
139
|
+
|
67
|
-
![
|
140
|
+
![実行時スクリーンショット](https://ddjkaamml8q8x.cloudfront.net/questions/2022-11-21/5e57fe05-8688-4f4d-946d-6fc0da9b57fb.png)
|