Adding a ViewPager
suggest changeMake sure the following dependency is added to your app’s build.gradle
file under dependencies:
compile 'com.android.support:support-core-ui:25.3.0'
Then add the ViewPager
to your activity layout:
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Then define your PagerAdapter
:
public class MyPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
// Create the page for the given position. For example:
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.xxxx, collection, false);
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
// Remove a page for the given position. For example:
collection.removeView((View) view);
}
@Override
public int getCount() {
//Return the number of views available.
return numberOfPages;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// Determines whether a page View is associated with a specific key object
// as returned by instantiateItem(ViewGroup, int). For example:
return view == object;
}
}
Finally setup the ViewPager
in your Activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyPagerAdapter(this));
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
2 Layouts
3 Gradle
6 Intent
11 Exceptions
13 AsyncTask
15 Emulator
18 Service
20 WebView
22 RecyclerView
23 Google Maps
26 Android NDK
28 Camera 2 API
29 ViewPager
30 CardView
32 SQLite
34 ButterKnife
36 Glide
37 Retrofit2
38 Dialog
39 ACRA
40 GreenDAO
43 AlarmManager
44 Fragments
45 Handler
48 Activity
49 Snackbar
54 Toast
55 Interfaces
56 Animators
57 Location
60 Parcelable
61 MediaPlayer
64 Menu
66 Picasso
68 RoboGuice
69 Memory Leaks
71 Volley
72 Widgets
78 Dagger 2
79 Realm
85 ProgressBar
86 Custom Fonts
87 Vibration
90 UI Lifecycle
91 Spinner
96 OkHttp
100 Firebase
103 Facebook SDK
104 Unzip File
109 TextView
110 ListView
112 Loader
117 MVP Architecture
119 Xposed
120 Security
121 PackageManager
122 ImageView
124 Doze Mode
126 SearchView
128 Callback URL
129 Twitter APIs
131 Drawables
132 Colors
133 ConstraintLayout
134 RenderScript
135 Fresco
136 Swipe to Refresh
139 IntentService
140 AdMob
141 Implicit Intents
145 Email Validation
146 Keyboard
147 Button
148 TextInputLayout
149 Bottom Sheets
151 EditText
156 Vk SDK
158 Count Down Timer
160 Otto Event Bus
164 ExoPlayer
166 MediaSession
168 FileProvider
170 XMPP
171 Authenticator
173 AudioManager
174 Job Scheduling
176 OpenCV
178 Threads
179 MediaStore
180 Time Utils
181 Touch Events
182 Fingerprint API
185 ORMLite
186 YouTube API
187 TabLayout
190 ShortcutManager
191 LruCache
192 Using Jenkins CI
193 Zip files
194 Vector Drawables
195 Fastlane
200 FileIO
202 Robolectric
203 Moshi
206 Retrolambda
207 SparseArray
210 Android Things
211 VideoView
212 ViewFlipper
217 Paint
218 AudioTrack
219 ProGuard
221 Java on Android
226 ConstraintSet
227 CleverTap
229 ADB shell
230 Ping ICMP
231 AIDL
232 Using Kotlin
235 Context
239 Bitmap Cache
241 JCodec
242 Design Patterns
243 Okio
245 TensorFlow
246 Game developmen
249 Leak Canary
250 FuseView
254 SpannableString
255 Looper
257 Google Drive API
262 Fastjson
264 Jackson
265 Play Store
268 Smartcard
270 Contributors