質問編集履歴
5
mainactivityの再現に不要なdestroyは削除しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -92,27 +92,6 @@
|
|
92
92
|
public int getItemPosition(Object object) {
|
93
93
|
return POSITION_NONE;
|
94
94
|
}
|
95
|
-
public void destroyAllItem(ViewPager pager) {
|
96
|
-
for (int i = 0; i < getCount() - 1; i++) {
|
97
|
-
try {
|
98
|
-
Object obj = this.instantiateItem(pager, i);
|
99
|
-
if (obj != null)
|
100
|
-
destroyItem(pager, i, obj);
|
101
|
-
} catch (Exception e) {
|
102
|
-
}
|
103
|
-
}
|
104
|
-
}
|
105
|
-
@Override
|
106
|
-
public void destroyItem(ViewGroup container, int position, Object object) {
|
107
|
-
super.destroyItem(container, position, object);
|
108
|
-
|
109
|
-
if (position <= getCount()) {
|
110
|
-
FragmentManager manager = ((Fragment) object).getFragmentManager();
|
111
|
-
FragmentTransaction trans = manager.beginTransaction();
|
112
|
-
trans.remove((Fragment) object);
|
113
|
-
trans.commit();
|
114
|
-
}
|
115
|
-
}
|
116
95
|
}
|
117
96
|
public void button1(View v) {
|
118
97
|
viewFlag = 1;
|
4
customtabを削除
title
CHANGED
File without changes
|
body
CHANGED
@@ -214,21 +214,4 @@
|
|
214
214
|
android:onClick="button2"/>
|
215
215
|
|
216
216
|
</RelativeLayout>
|
217
|
-
```
|
218
|
-
|
219
|
-
customtabを追記します
|
220
|
-
```xml
|
221
|
-
<?xml version="1.0" encoding="utf-8"?>
|
222
|
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
223
|
-
android:layout_width="75dp"
|
224
|
-
android:layout_height="match_parent">
|
225
|
-
|
226
|
-
<ImageView
|
227
|
-
android:layout_width="match_parent"
|
228
|
-
android:layout_height="53dp"
|
229
|
-
android:scaleType="fitCenter"
|
230
|
-
android:id="@+id/icon"
|
231
|
-
android:layout_gravity="center"
|
232
|
-
android:gravity="center"/>
|
233
|
-
</LinearLayout>
|
234
217
|
```
|
3
最小のプログラムに記載を修正しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,50 +1,221 @@
|
|
1
1
|
Android Studio3.3.2を利用しています。
|
2
2
|
以下のようにViewPagerを利用していますが、setCurrentItemが正常に動作してくれません。
|
3
|
-
|
3
|
+
以下のプロジェクトでbutton1を押して1−0を選択した後にbutton2を押すとなぜか一つずれて表示されてしまいます。
|
4
|
-
|
4
|
+
(tabFragmentは作成し変更情報のみ記載してます。)
|
5
5
|
|
6
|
+
何か分かればアドバイスをお願いします。
|
7
|
+
|
6
8
|
```Java
|
9
|
+
public class MainActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener, tabFragment.OnFragmentInteractionListener {
|
10
|
+
|
11
|
+
private SectionsPagerAdapter mSectionsPagerAdapter;
|
12
|
+
private ViewPager mViewPager1;
|
13
|
+
private ViewPager mViewPager2;
|
14
|
+
private TabLayout tabLayout;
|
15
|
+
private tabFragment mTabFragment;
|
7
|
-
|
16
|
+
int viewFlag = 1;
|
17
|
+
@Override
|
18
|
+
protected void onCreate(Bundle savedInstanceState) {
|
19
|
+
super.onCreate(savedInstanceState);
|
20
|
+
setContentView(R.layout.activity_main);
|
21
|
+
setTabicons(0);
|
22
|
+
}
|
23
|
+
|
24
|
+
private void setTabicons(int setTub) {
|
8
|
-
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
25
|
+
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
|
9
|
-
mViewPager1 = (ViewPager) findViewById(R.id.container1);
|
26
|
+
mViewPager1 = (ViewPager) findViewById(R.id.container1);
|
27
|
+
mViewPager2 = (ViewPager) findViewById(R.id.container2);
|
10
|
-
tabLayout = (TabLayout) findViewById(R.id.tabs);
|
28
|
+
tabLayout = (TabLayout) findViewById(R.id.tabs);
|
29
|
+
if (viewFlag == 1) {
|
30
|
+
mViewPager1.setVisibility(View.VISIBLE);
|
31
|
+
mViewPager2.setVisibility(View.GONE);
|
11
|
-
mViewPager1.setAdapter(mSectionsPagerAdapter);
|
32
|
+
mViewPager1.setAdapter(mSectionsPagerAdapter);
|
12
|
-
tabLayout.setupWithViewPager(mViewPager1);
|
33
|
+
tabLayout.setupWithViewPager(mViewPager1);
|
13
|
-
|
34
|
+
} else {
|
35
|
+
mViewPager1.setVisibility(View.GONE);
|
36
|
+
mViewPager2.setVisibility(View.VISIBLE);
|
14
|
-
|
37
|
+
mViewPager2.setAdapter(mSectionsPagerAdapter);
|
38
|
+
tabLayout.setupWithViewPager(mViewPager2);
|
39
|
+
}
|
15
|
-
tabLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
|
40
|
+
tabLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
|
16
|
-
|
41
|
+
if (viewFlag == 1) {
|
42
|
+
for (int i = 0; i < 16; i++) {
|
17
|
-
|
43
|
+
tabLayout.getTabAt(i).setText("1-" + String.valueOf(i));
|
44
|
+
}
|
18
|
-
|
45
|
+
} else {
|
19
|
-
for (int i = 0; i <
|
46
|
+
for (int i = 0; i < 6; i++) {
|
20
|
-
int resID = getResources().getIdentifier(defTabIcon[i], "drawable", getPackageName());
|
21
|
-
View view1 = getLayoutInflater().inflate(R.layout.customtab, null);
|
22
|
-
view1.findViewById(R.id.icon).setBackgroundResource(resID);
|
23
|
-
|
47
|
+
tabLayout.getTabAt(i).setText("2-" + String.valueOf(i));
|
48
|
+
}
|
49
|
+
}
|
50
|
+
if (viewFlag == 1) {
|
51
|
+
mViewPager1.setCurrentItem(setTub);
|
52
|
+
|
53
|
+
mSectionsPagerAdapter.finishUpdate(mViewPager1);
|
54
|
+
mSectionsPagerAdapter.finishUpdate(mViewPager2);
|
55
|
+
} else {
|
56
|
+
mViewPager2.setCurrentItem(setTub);
|
57
|
+
mSectionsPagerAdapter.finishUpdate(mViewPager2);
|
58
|
+
mSectionsPagerAdapter.finishUpdate(mViewPager1);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
@Override
|
62
|
+
public void onFragmentInteraction(Uri uri) {
|
63
|
+
|
64
|
+
}
|
65
|
+
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
|
66
|
+
public SectionsPagerAdapter(FragmentManager fm) {
|
67
|
+
super(fm);
|
68
|
+
}
|
69
|
+
@Override
|
70
|
+
public Fragment getItem(int position) {
|
71
|
+
tabFragment npanel1 = tabFragment.newInstance(position);
|
72
|
+
return npanel1;
|
73
|
+
}
|
74
|
+
@Override
|
75
|
+
public int getCount() {
|
76
|
+
if (viewFlag == 1) {
|
77
|
+
return 16;
|
78
|
+
} else {
|
79
|
+
return 6;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
@Override
|
83
|
+
public CharSequence getPageTitle(int position) {
|
84
|
+
return null;
|
85
|
+
}
|
86
|
+
@Override
|
87
|
+
public Object instantiateItem(ViewGroup container, int position) {
|
88
|
+
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
|
89
|
+
return createdFragment;
|
90
|
+
}
|
91
|
+
@Override
|
92
|
+
public int getItemPosition(Object object) {
|
93
|
+
return POSITION_NONE;
|
94
|
+
}
|
95
|
+
public void destroyAllItem(ViewPager pager) {
|
96
|
+
for (int i = 0; i < getCount() - 1; i++) {
|
97
|
+
try {
|
98
|
+
Object obj = this.instantiateItem(pager, i);
|
99
|
+
if (obj != null)
|
100
|
+
destroyItem(pager, i, obj);
|
101
|
+
} catch (Exception e) {
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
@Override
|
106
|
+
public void destroyItem(ViewGroup container, int position, Object object) {
|
107
|
+
super.destroyItem(container, position, object);
|
108
|
+
|
109
|
+
if (position <= getCount()) {
|
110
|
+
FragmentManager manager = ((Fragment) object).getFragmentManager();
|
111
|
+
FragmentTransaction trans = manager.beginTransaction();
|
112
|
+
trans.remove((Fragment) object);
|
113
|
+
trans.commit();
|
114
|
+
}
|
115
|
+
}
|
116
|
+
}
|
117
|
+
public void button1(View v) {
|
118
|
+
viewFlag = 1;
|
119
|
+
setTabicons(mViewPager2.getCurrentItem());
|
120
|
+
}
|
121
|
+
public void button2(View v) {
|
122
|
+
viewFlag = 2;
|
123
|
+
setTabicons(mViewPager1.getCurrentItem());
|
124
|
+
}
|
125
|
+
@Override
|
126
|
+
public void onGlobalLayout() {
|
127
|
+
}
|
24
128
|
}
|
25
|
-
mViewPager1.setCurrentItem(0);
|
26
129
|
```
|
27
|
-
|
28
|
-
以下の
|
130
|
+
tabFragmentは以下のところだけ修正しました。XMLは変更していません。
|
29
|
-
```
|
131
|
+
```java
|
132
|
+
public class tabFragment extends Fragment {
|
133
|
+
(省略)
|
134
|
+
public static tabFragment newInstance(int tubpos) {
|
135
|
+
tabFragment frag = new tabFragment();
|
30
|
-
|
136
|
+
Bundle args = new Bundle();
|
31
|
-
@Override
|
32
|
-
public void run() {
|
33
|
-
|
137
|
+
args.putInt("tabPosition", tubpos);
|
138
|
+
frag.setArguments(args);
|
139
|
+
return frag;
|
34
|
-
|
140
|
+
}
|
35
|
-
}, 100);
|
36
141
|
```
|
37
142
|
|
143
|
+
mainのLayoutを追記します
|
38
|
-
|
144
|
+
```xml
|
145
|
+
<?xml version="1.0" encoding="utf-8"?>
|
146
|
+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
147
|
+
android:id="@+id/relativeLayout"
|
148
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
149
|
+
xmlns:tools="http://schemas.android.com/tools"
|
39
|
-
|
150
|
+
android:layout_width="match_parent"
|
151
|
+
android:layout_height="match_parent"
|
40
|
-
|
152
|
+
android:fitsSystemWindows="true"
|
41
|
-
そのため、for文の前に実行しています。
|
42
|
-
|
153
|
+
tools:context=".MainActivity">
|
43
154
|
|
155
|
+
<TextView
|
156
|
+
android:layout_width="wrap_content"
|
157
|
+
android:layout_height="wrap_content"
|
158
|
+
android:text="Hello World!"
|
159
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
160
|
+
app:layout_constraintLeft_toLeftOf="parent"
|
161
|
+
app:layout_constraintRight_toRightOf="parent"
|
44
|
-
|
162
|
+
app:layout_constraintTop_toTopOf="parent" />
|
45
163
|
|
46
|
-
|
164
|
+
<android.support.v4.view.ViewPager
|
165
|
+
android:id="@+id/container2"
|
166
|
+
android:layout_width="match_parent"
|
167
|
+
android:layout_height="match_parent"
|
168
|
+
android:layout_marginTop="100dp"
|
169
|
+
android:layout_marginBottom="55dp"
|
170
|
+
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
|
47
171
|
|
172
|
+
</android.support.v4.view.ViewPager>
|
173
|
+
|
174
|
+
<android.support.v4.view.ViewPager
|
175
|
+
android:id="@+id/container1"
|
176
|
+
android:layout_width="match_parent"
|
177
|
+
android:layout_height="match_parent"
|
178
|
+
android:layout_marginTop="100dp"
|
179
|
+
android:layout_marginBottom="55dp"
|
180
|
+
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
|
181
|
+
|
182
|
+
</android.support.v4.view.ViewPager>
|
183
|
+
|
184
|
+
<android.support.design.widget.AppBarLayout
|
185
|
+
android:id="@+id/appbar"
|
186
|
+
android:layout_width="match_parent"
|
187
|
+
android:layout_height="50dp"
|
188
|
+
android:background="@color/colorPrimary">
|
189
|
+
|
190
|
+
<android.support.design.widget.TabLayout
|
191
|
+
android:id="@+id/tabs"
|
192
|
+
android:layout_width="match_parent"
|
193
|
+
android:layout_height="match_parent"
|
194
|
+
app:tabMode="scrollable" />
|
195
|
+
|
196
|
+
</android.support.design.widget.AppBarLayout>
|
197
|
+
|
198
|
+
<Button
|
199
|
+
android:id="@+id/button"
|
200
|
+
android:layout_width="wrap_content"
|
201
|
+
android:layout_height="wrap_content"
|
202
|
+
android:layout_marginTop="50dp"
|
203
|
+
android:layout_marginLeft="10dp"
|
204
|
+
android:text="Button1"
|
205
|
+
android:onClick="button1"/>
|
206
|
+
|
207
|
+
<Button
|
208
|
+
android:id="@+id/button2"
|
209
|
+
android:layout_width="wrap_content"
|
210
|
+
android:layout_height="wrap_content"
|
211
|
+
android:layout_marginTop="50dp"
|
212
|
+
android:layout_marginLeft="150dp"
|
213
|
+
android:text="Button2"
|
214
|
+
android:onClick="button2"/>
|
215
|
+
|
216
|
+
</RelativeLayout>
|
217
|
+
```
|
218
|
+
|
48
219
|
customtabを追記します
|
49
220
|
```xml
|
50
221
|
<?xml version="1.0" encoding="utf-8"?>
|
@@ -60,55 +231,4 @@
|
|
60
231
|
android:layout_gravity="center"
|
61
232
|
android:gravity="center"/>
|
62
233
|
</LinearLayout>
|
63
|
-
```
|
64
|
-
|
65
|
-
SectionsPagerAdapterは以下のような感じです。
|
66
|
-
```java
|
67
|
-
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
|
68
|
-
public SectionsPagerAdapter(FragmentManager fm) {
|
69
|
-
super(fm);
|
70
|
-
}
|
71
|
-
@Override
|
72
|
-
public Fragment getItem(int position) {
|
73
|
-
tabFragment npanel1 = tabFragment.newInstance(position);
|
74
|
-
return npanel1;
|
75
|
-
}
|
76
|
-
@Override
|
77
|
-
public int getCount() {
|
78
|
-
return 7;
|
79
|
-
}
|
80
|
-
@Override
|
81
|
-
public CharSequence getPageTitle(int position) {
|
82
|
-
return null;
|
83
|
-
}
|
84
|
-
@Override
|
85
|
-
public Object instantiateItem(ViewGroup container, int position) {
|
86
|
-
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
|
87
|
-
mTabFragment = (tabFragment) createdFragment;
|
88
|
-
return createdFragment;
|
89
|
-
}
|
90
|
-
@Override
|
91
|
-
public int getItemPosition(Object object) {
|
92
|
-
return POSITION_NONE;
|
93
|
-
}
|
94
|
-
public void destroyAllItem(ViewPager pager) {
|
95
|
-
for (int i = 0; i < getCount() - 1; i++) {
|
96
|
-
try {
|
97
|
-
Object obj = this.instantiateItem(pager, i);
|
98
|
-
if (obj != null) destroyItem(pager, i, obj);
|
99
|
-
} catch (Exception e) {
|
100
|
-
}
|
101
|
-
}
|
102
|
-
}
|
103
|
-
@Override
|
104
|
-
public void destroyItem(ViewGroup container, int position, Object object) {
|
105
|
-
super.destroyItem(container, position, object);
|
106
|
-
if (position <= getCount()) {
|
107
|
-
FragmentManager manager = ((Fragment) object).getFragmentManager();
|
108
|
-
FragmentTransaction trans = manager.beginTransaction();
|
109
|
-
trans.remove((Fragment) object);
|
110
|
-
trans.commit();
|
111
|
-
}
|
112
|
-
}
|
113
|
-
}
|
114
234
|
```
|
2
SectionsPagerAdapter追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -60,4 +60,55 @@
|
|
60
60
|
android:layout_gravity="center"
|
61
61
|
android:gravity="center"/>
|
62
62
|
</LinearLayout>
|
63
|
+
```
|
64
|
+
|
65
|
+
SectionsPagerAdapterは以下のような感じです。
|
66
|
+
```java
|
67
|
+
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
|
68
|
+
public SectionsPagerAdapter(FragmentManager fm) {
|
69
|
+
super(fm);
|
70
|
+
}
|
71
|
+
@Override
|
72
|
+
public Fragment getItem(int position) {
|
73
|
+
tabFragment npanel1 = tabFragment.newInstance(position);
|
74
|
+
return npanel1;
|
75
|
+
}
|
76
|
+
@Override
|
77
|
+
public int getCount() {
|
78
|
+
return 7;
|
79
|
+
}
|
80
|
+
@Override
|
81
|
+
public CharSequence getPageTitle(int position) {
|
82
|
+
return null;
|
83
|
+
}
|
84
|
+
@Override
|
85
|
+
public Object instantiateItem(ViewGroup container, int position) {
|
86
|
+
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
|
87
|
+
mTabFragment = (tabFragment) createdFragment;
|
88
|
+
return createdFragment;
|
89
|
+
}
|
90
|
+
@Override
|
91
|
+
public int getItemPosition(Object object) {
|
92
|
+
return POSITION_NONE;
|
93
|
+
}
|
94
|
+
public void destroyAllItem(ViewPager pager) {
|
95
|
+
for (int i = 0; i < getCount() - 1; i++) {
|
96
|
+
try {
|
97
|
+
Object obj = this.instantiateItem(pager, i);
|
98
|
+
if (obj != null) destroyItem(pager, i, obj);
|
99
|
+
} catch (Exception e) {
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}
|
103
|
+
@Override
|
104
|
+
public void destroyItem(ViewGroup container, int position, Object object) {
|
105
|
+
super.destroyItem(container, position, object);
|
106
|
+
if (position <= getCount()) {
|
107
|
+
FragmentManager manager = ((Fragment) object).getFragmentManager();
|
108
|
+
FragmentTransaction trans = manager.beginTransaction();
|
109
|
+
trans.remove((Fragment) object);
|
110
|
+
trans.commit();
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
63
114
|
```
|
1
customtabを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -43,4 +43,21 @@
|
|
43
43
|
|
44
44
|
notifyDataSetChangedを最後に実行すれば問題が解決するのかもしれませんがメニュー内のIcon自体が表示されなくなってしまうためにfor文の前で実行しています。
|
45
45
|
|
46
|
-
何か試した方が良い方法などについてご連絡お願いします。
|
46
|
+
何か試した方が良い方法などについてご連絡お願いします。
|
47
|
+
|
48
|
+
customtabを追記します
|
49
|
+
```xml
|
50
|
+
<?xml version="1.0" encoding="utf-8"?>
|
51
|
+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
52
|
+
android:layout_width="75dp"
|
53
|
+
android:layout_height="match_parent">
|
54
|
+
|
55
|
+
<ImageView
|
56
|
+
android:layout_width="match_parent"
|
57
|
+
android:layout_height="53dp"
|
58
|
+
android:scaleType="fitCenter"
|
59
|
+
android:id="@+id/icon"
|
60
|
+
android:layout_gravity="center"
|
61
|
+
android:gravity="center"/>
|
62
|
+
</LinearLayout>
|
63
|
+
```
|