回答編集履歴
2
修正
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
回答にしちゃったので、ついでに AndroidStudio のタブのプロジェクト雛形から、タイトルを修正・保存 (SharedPreferences) するコードを作りました。
|
10
10
|
|
11
|
-
タブは TagLayout 、ビューページャは ViewPager2 、バインディングは嫌いなので外しています^^;
|
11
|
+
タブは TagLayout 、ビューページャは ViewPager2 、ダイアログはフラグメント、バインディングは嫌いなので外しています^^;
|
12
12
|
|
13
13
|
|
14
14
|
|
1
コード追加
test
CHANGED
@@ -1 +1,511 @@
|
|
1
1
|
変更をどこかに保存して、再起動時にそれを読んで設定すれば良いのではと思います。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
----
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
回答にしちゃったので、ついでに AndroidStudio のタブのプロジェクト雛形から、タイトルを修正・保存 (SharedPreferences) するコードを作りました。
|
10
|
+
|
11
|
+
タブは TagLayout 、ビューページャは ViewPager2 、バインディングは嫌いなので外しています^^;
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
MainActivity.java
|
16
|
+
|
17
|
+
```java
|
18
|
+
|
19
|
+
package com.teratail.q375388;
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
import android.content.SharedPreferences;
|
24
|
+
|
25
|
+
import android.os.Bundle;
|
26
|
+
|
27
|
+
import android.view.*;
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
import com.google.android.material.tabs.*;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
import androidx.appcompat.app.*;
|
36
|
+
|
37
|
+
import androidx.lifecycle.ViewModelProvider;
|
38
|
+
|
39
|
+
import androidx.viewpager2.widget.ViewPager2;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
import com.teratail.q375388.ui.main.*;
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener, TabTitleChangeDialogFragment.Listener {
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
private SharedPreferences sharedPreferences;
|
52
|
+
|
53
|
+
private TabLayout tabLayout;
|
54
|
+
|
55
|
+
private PageViewModel pageViewModel;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
@Override
|
60
|
+
|
61
|
+
protected void onCreate(Bundle savedInstanceState) {
|
62
|
+
|
63
|
+
super.onCreate(savedInstanceState);
|
64
|
+
|
65
|
+
setContentView(R.layout.activity_main);
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
sharedPreferences = getSharedPreferences("TabTitles", MODE_PRIVATE);
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
|
74
|
+
|
75
|
+
pageViewModel.restoreTabTitles(sharedPreferences, new String[]{
|
76
|
+
|
77
|
+
getString(R.string.tab_text_1), getString(R.string.tab_text_2)
|
78
|
+
|
79
|
+
});
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
ViewPager2 viewPager = findViewById(R.id.pager);
|
84
|
+
|
85
|
+
viewPager.setAdapter(new SectionsStateAdapter(this));
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
tabLayout = findViewById(R.id.tablayout);
|
90
|
+
|
91
|
+
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> tab.setText(pageViewModel.getTabTitle(position))).attach();
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
for(int i=0; i<tabLayout.getTabCount(); i++) {
|
96
|
+
|
97
|
+
View view = tabLayout.getTabAt(i).view;
|
98
|
+
|
99
|
+
view.setTag(i);
|
100
|
+
|
101
|
+
view.setOnLongClickListener(this);
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
@Override
|
110
|
+
|
111
|
+
public boolean onLongClick(View v) {
|
112
|
+
|
113
|
+
int tabIndex = (Integer)v.getTag();
|
114
|
+
|
115
|
+
String title = pageViewModel.getTabTitle(tabIndex);
|
116
|
+
|
117
|
+
TabTitleChangeDialogFragment dialog = TabTitleChangeDialogFragment.newInstance(tabIndex, title);
|
118
|
+
|
119
|
+
dialog.show(getSupportFragmentManager(), "TabTitleChangeDialogFragment");
|
120
|
+
|
121
|
+
return true;
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
@Override
|
128
|
+
|
129
|
+
public void onTabTitleChange(TabTitleChangeDialogFragment dialog) {
|
130
|
+
|
131
|
+
String title = dialog.getTitle();
|
132
|
+
|
133
|
+
int tabIndex = dialog.getTabIndex();
|
134
|
+
|
135
|
+
tabLayout.getTabAt(tabIndex).setText(title); //ここでタブの名前を変更
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
pageViewModel.setTabTitle(tabIndex, title);
|
140
|
+
|
141
|
+
pageViewModel.saveTabTitles(sharedPreferences);
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
res/layout/activity_main.xml
|
150
|
+
|
151
|
+
```xml
|
152
|
+
|
153
|
+
<?xml version="1.0" encoding="utf-8"?>
|
154
|
+
|
155
|
+
<androidx.coordinatorlayout.widget.CoordinatorLayout
|
156
|
+
|
157
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
158
|
+
|
159
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
160
|
+
|
161
|
+
xmlns:tools="http://schemas.android.com/tools"
|
162
|
+
|
163
|
+
android:layout_width="match_parent"
|
164
|
+
|
165
|
+
android:layout_height="match_parent"
|
166
|
+
|
167
|
+
tools:context=".MainActivity">
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
<com.google.android.material.appbar.AppBarLayout
|
172
|
+
|
173
|
+
android:layout_width="match_parent"
|
174
|
+
|
175
|
+
android:layout_height="wrap_content"
|
176
|
+
|
177
|
+
android:theme="@style/Theme.Q375388.AppBarOverlay">
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
<TextView
|
182
|
+
|
183
|
+
android:id="@+id/title"
|
184
|
+
|
185
|
+
android:layout_width="wrap_content"
|
186
|
+
|
187
|
+
android:layout_height="wrap_content"
|
188
|
+
|
189
|
+
android:gravity="center"
|
190
|
+
|
191
|
+
android:minHeight="?actionBarSize"
|
192
|
+
|
193
|
+
android:padding="@dimen/appbar_padding"
|
194
|
+
|
195
|
+
android:text="@string/app_name"
|
196
|
+
|
197
|
+
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
<com.google.android.material.tabs.TabLayout
|
202
|
+
|
203
|
+
android:id="@+id/tablayout"
|
204
|
+
|
205
|
+
android:layout_width="match_parent"
|
206
|
+
|
207
|
+
android:layout_height="wrap_content" />
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
</com.google.android.material.appbar.AppBarLayout>
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
<androidx.viewpager2.widget.ViewPager2
|
216
|
+
|
217
|
+
android:id="@+id/pager"
|
218
|
+
|
219
|
+
android:layout_width="match_parent"
|
220
|
+
|
221
|
+
android:layout_height="match_parent" />
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
226
|
+
|
227
|
+
```
|
228
|
+
|
229
|
+
ui.main.PageViewModel.java
|
230
|
+
|
231
|
+
```java
|
232
|
+
|
233
|
+
package com.teratail.q375388.ui.main;
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
import android.content.SharedPreferences;
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
import androidx.lifecycle.ViewModel;
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
public class PageViewModel extends ViewModel {
|
246
|
+
|
247
|
+
private String[] tabTitles;
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
public String getTabTitle(int index) { return tabTitles[index]; }
|
252
|
+
|
253
|
+
public void setTabTitle(int index, String title) { tabTitles[index] = title; }
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
public void restoreTabTitles(SharedPreferences sharedPreferences, String[] defaultTitles) {
|
258
|
+
|
259
|
+
tabTitles = new String[defaultTitles.length];
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if(sharedPreferences == null) {
|
264
|
+
|
265
|
+
for(int i=0; i<tabTitles.length; i++) {
|
266
|
+
|
267
|
+
tabTitles[i] = defaultTitles[i];
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
} else {
|
272
|
+
|
273
|
+
for(int i=0; i<tabTitles.length; i++) {
|
274
|
+
|
275
|
+
tabTitles[i] = sharedPreferences.getString(getPreferencesKey(i), defaultTitles[i]);
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
public void saveTabTitles(SharedPreferences sharedPreferences) {
|
284
|
+
|
285
|
+
SharedPreferences.Editor editor = sharedPreferences.edit();
|
286
|
+
|
287
|
+
editor.clear();
|
288
|
+
|
289
|
+
for(int i=0; i<tabTitles.length; i++) {
|
290
|
+
|
291
|
+
editor.putString(getPreferencesKey(i), tabTitles[i]);
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
editor.commit();
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
private String getPreferencesKey(int i) { return "tab"+(i+1); }
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
```
|
306
|
+
|
307
|
+
ui.main.TabTitleChangeDialogFragment.java
|
308
|
+
|
309
|
+
```java
|
310
|
+
|
311
|
+
package com.teratail.q375388.ui.main;
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
import android.app.Dialog;
|
316
|
+
|
317
|
+
import android.content.*;
|
318
|
+
|
319
|
+
import android.os.Bundle;
|
320
|
+
|
321
|
+
import android.view.*;
|
322
|
+
|
323
|
+
import android.widget.EditText;
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
import androidx.annotation.Nullable;
|
328
|
+
|
329
|
+
import androidx.appcompat.app.AlertDialog;
|
330
|
+
|
331
|
+
import androidx.fragment.app.*;
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
import com.teratail.q375388.R;
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
public class TabTitleChangeDialogFragment extends DialogFragment {
|
340
|
+
|
341
|
+
public interface Listener {
|
342
|
+
|
343
|
+
void onTabTitleChange(TabTitleChangeDialogFragment dialog);
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
private static final String ARGS_TAB_INDEX = "TabIndex";
|
350
|
+
|
351
|
+
private static final String ARGS_TITLE = "Title";
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
public static TabTitleChangeDialogFragment newInstance(int tabIndex, String title) {
|
356
|
+
|
357
|
+
TabTitleChangeDialogFragment dialog = new TabTitleChangeDialogFragment();
|
358
|
+
|
359
|
+
Bundle args = new Bundle();
|
360
|
+
|
361
|
+
args.putInt(ARGS_TAB_INDEX, tabIndex);
|
362
|
+
|
363
|
+
args.putString(ARGS_TITLE, title);
|
364
|
+
|
365
|
+
dialog.setArguments(args);
|
366
|
+
|
367
|
+
return dialog;
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
private int tabIndex;
|
374
|
+
|
375
|
+
private String title;
|
376
|
+
|
377
|
+
private Listener listener;
|
378
|
+
|
379
|
+
private EditText editText;
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
@Override
|
384
|
+
|
385
|
+
public void onCreate(@Nullable Bundle savedInstanceState) {
|
386
|
+
|
387
|
+
super.onCreate(savedInstanceState);
|
388
|
+
|
389
|
+
Bundle args = getArguments();
|
390
|
+
|
391
|
+
tabIndex = args.getInt(ARGS_TAB_INDEX);
|
392
|
+
|
393
|
+
title = args.getString(ARGS_TITLE);
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
public int getTabIndex() { return tabIndex; }
|
400
|
+
|
401
|
+
public String getTitle() { return editText.getText().toString(); }
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
@Override
|
406
|
+
|
407
|
+
public void onAttach(Context context) {
|
408
|
+
|
409
|
+
super.onAttach(context);
|
410
|
+
|
411
|
+
try {
|
412
|
+
|
413
|
+
listener = (Listener)context;
|
414
|
+
|
415
|
+
} catch (ClassCastException e) {
|
416
|
+
|
417
|
+
throw new ClassCastException(context.toString() + " must implement TabTitleChangeDialogListener");
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
@Override
|
426
|
+
|
427
|
+
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
428
|
+
|
429
|
+
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
430
|
+
|
431
|
+
LayoutInflater inflater = requireActivity().getLayoutInflater();
|
432
|
+
|
433
|
+
View inputView = inflater.inflate(R.layout.fragment_tab_title_change_dialog, null);
|
434
|
+
|
435
|
+
editText = inputView.findViewById(R.id.editText1);
|
436
|
+
|
437
|
+
editText.setText(title);
|
438
|
+
|
439
|
+
editText.requestFocus();
|
440
|
+
|
441
|
+
editText.setSelection(0, title.length());
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
builder.setView(inputView)
|
446
|
+
|
447
|
+
.setTitle("カテゴリ名変更")
|
448
|
+
|
449
|
+
.setPositiveButton("変更", new DialogInterface.OnClickListener() {
|
450
|
+
|
451
|
+
public void onClick(DialogInterface dialog, int id) {
|
452
|
+
|
453
|
+
listener.onTabTitleChange(TabTitleChangeDialogFragment.this);
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
})
|
458
|
+
|
459
|
+
.setNegativeButton("キャンセル", (dialog, id) -> TabTitleChangeDialogFragment.this.getDialog().cancel());
|
460
|
+
|
461
|
+
return builder.create();
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
```
|
468
|
+
|
469
|
+
res/layout/fragment_tab_title_change_dialog.xml
|
470
|
+
|
471
|
+
```xml
|
472
|
+
|
473
|
+
<?xml version="1.0" encoding="utf-8"?>
|
474
|
+
|
475
|
+
<androidx.constraintlayout.widget.ConstraintLayout
|
476
|
+
|
477
|
+
xmlns:android="http://schemas.android.com/apk/res/android"
|
478
|
+
|
479
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
480
|
+
|
481
|
+
xmlns:tools="http://schemas.android.com/tools"
|
482
|
+
|
483
|
+
android:layout_width="match_parent"
|
484
|
+
|
485
|
+
android:layout_height="match_parent"
|
486
|
+
|
487
|
+
tools:context=".ui.main.TabTitleChangeDialogFragment">
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
<EditText
|
492
|
+
|
493
|
+
android:id="@+id/editText1"
|
494
|
+
|
495
|
+
android:layout_width="wrap_content"
|
496
|
+
|
497
|
+
android:layout_height="wrap_content"
|
498
|
+
|
499
|
+
android:ems="16"
|
500
|
+
|
501
|
+
android:singleLine="true"
|
502
|
+
|
503
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
504
|
+
|
505
|
+
app:layout_constraintTop_toTopOf="parent" />
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
510
|
+
|
511
|
+
```
|