I. First add the Viewpager control to XML
We want each Viewpager to display a picture
Create a new Pager_item.xml file
The code is as follows
Two. Analysis
A viewpager needs to set up an adapter that can inherit Fragmentstatepageradapter,
The adapter also needs to set the data, which adds fragmentation to the adapter. Fragments can inherit fragment, set views, listen, and so on, as shown below:
Three. We need to create a new fragment first.
Note that this inherited Fragment need to be under package android.support.v4.app.Fragment
Fragments and activity are similar, have their own life cycle, needless to say, the knowledge of debris can find information on their own.
The code is as follows: Key methods three
1: This is the setting of the view
2.: This is the setting of the picture, according to the number of the different settings of different pictures (number by mainactivity Pass in)
3 This is to accept the number in the outside, and set the number in the bundle
Four. Write an adapter
The fragment is written, now you need to write an adapter
There are two main ways to write an adapter:
Five. Set the shards in Mainactivity
So it's done.
SOURCE Download:
http://pan.baidu.com/share/link?shareid=3331882871&uk=705537436