質問編集履歴
1
MainActivityのコードを全文載せました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,17 +4,143 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
+
訂正し、MainActivityのコードをすべて載せました。Fragmentを3つ用意しスワイプさせるためAdapterも使用しています。
|
8
|
+
|
9
|
+
|
10
|
+
|
7
11
|
```java
|
12
|
+
|
13
|
+
package com.example.viewpagertest;
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
import android.net.Uri;
|
18
|
+
|
19
|
+
import android.support.v4.app.Fragment;
|
20
|
+
|
21
|
+
import android.support.v4.app.FragmentActivity;
|
22
|
+
|
23
|
+
import android.support.v4.app.FragmentManager;
|
24
|
+
|
25
|
+
import android.support.v4.app.FragmentStatePagerAdapter;
|
26
|
+
|
27
|
+
import android.support.v4.view.ViewPager;
|
28
|
+
|
29
|
+
import android.support.v7.app.AppCompatActivity;
|
30
|
+
|
31
|
+
import android.os.Bundle;
|
32
|
+
|
33
|
+
import android.util.Log;
|
34
|
+
|
35
|
+
import android.widget.TextView;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
import java.util.Random;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
8
44
|
|
9
45
|
public class MainActivity extends FragmentActivity {
|
10
46
|
|
47
|
+
private InfinateSwipeViewPagerAdapter mPagerAdapter;
|
11
48
|
|
12
|
-
|
13
|
-
|
49
|
+
private ViewPager mViewPager;
|
14
50
|
|
15
51
|
|
16
52
|
|
53
|
+
@Override
|
54
|
+
|
55
|
+
protected void onCreate(Bundle savedInstanceState) {
|
56
|
+
|
57
|
+
super.onCreate(savedInstanceState);
|
58
|
+
|
59
|
+
setContentView(R.layout.activity_main);
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
mPagerAdapter = new InfinateSwipeViewPagerAdapter(getSupportFragmentManager());
|
64
|
+
|
65
|
+
mViewPager = (ViewPager) findViewById(R.id.pager);
|
66
|
+
|
67
|
+
mViewPager.setAdapter(mPagerAdapter);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
@Override
|
74
|
+
|
75
|
+
protected void onResume() {
|
76
|
+
|
77
|
+
super.onResume();
|
78
|
+
|
79
|
+
mViewPager.setCurrentItem(1);
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
class InfinateSwipeViewPagerAdapter extends FragmentStatePagerAdapter {
|
86
|
+
|
87
|
+
public static final int MAX_PAGE_NUM = 10;
|
88
|
+
|
89
|
+
private static final int OBJECT_NUM = 3;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
public InfinateSwipeViewPagerAdapter(FragmentManager fm) {
|
94
|
+
|
95
|
+
super(fm);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
@Override
|
102
|
+
|
103
|
+
public Fragment getItem(int position) {
|
104
|
+
|
105
|
+
Fragment fragment = null;
|
106
|
+
|
107
|
+
Log.d("position", "positionは「" + position + "」");
|
108
|
+
|
109
|
+
int diff = (position - (MAX_PAGE_NUM / 2)) % OBJECT_NUM;
|
110
|
+
|
111
|
+
int index = (0 > diff) ? (OBJECT_NUM + diff) : diff;
|
112
|
+
|
113
|
+
Log.d("index", "indexは「" + index + "」");
|
114
|
+
|
115
|
+
switch (index) {
|
116
|
+
|
117
|
+
case 0:
|
118
|
+
|
119
|
+
fragment = new TestFragment1();
|
120
|
+
|
121
|
+
break;
|
122
|
+
|
123
|
+
case 1:
|
124
|
+
|
125
|
+
fragment = new TestFragment2();
|
126
|
+
|
127
|
+
break;
|
128
|
+
|
129
|
+
case 2:
|
130
|
+
|
131
|
+
fragment = new TestFragment3();
|
132
|
+
|
133
|
+
break;
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
return fragment;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
//以下がRandom関数を使っているところです。
|
142
|
+
|
17
|
-
public class Ran {
|
143
|
+
public class Ran {
|
18
144
|
|
19
145
|
public void main(String[] args) {
|
20
146
|
|
@@ -22,15 +148,9 @@
|
|
22
148
|
|
23
149
|
int ran = r.nextInt(MAX_PAGE_NUM);
|
24
150
|
|
25
|
-
//MAX_PAGE_NUMは作っているアプリの中で別のクラス内で指定してる変数、
|
26
|
-
|
27
|
-
//ここではMAX_PAGE_NUM=10としている
|
28
|
-
|
29
151
|
TextView text;
|
30
152
|
|
31
153
|
text = (TextView) findViewById(R.id.text1);
|
32
|
-
|
33
|
-
//text1はFragment上に用意しているTextViewのID
|
34
154
|
|
35
155
|
text.setText(Integer.toString(ran));
|
36
156
|
|
@@ -40,4 +160,22 @@
|
|
40
160
|
|
41
161
|
|
42
162
|
|
163
|
+
@Override
|
164
|
+
|
165
|
+
public int getCount() {
|
166
|
+
|
167
|
+
return MAX_PAGE_NUM;
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
43
181
|
```
|