ViewPager loads view in basic mode, and viewpager loads view
1. Create view_pager.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /></RelativeLayout>
2. Create a ViewPagerAdapter class to load the adapter for the ViewPager layout instance;
public class ViewPagerAdapter extends PagerAdapter{ private Context context; private List<View> viewlist; public ViewPagerAdapter( List<View> views,Context context) { this.context = context; this.views = views; }
// Repeat the four methods
// GetCount () returns the size of List <View>: @ Override public int getCount () {// TODO Auto-generated method stub return views. size ();}
// InstantiateItem (): adds the current view to the ViewGroup and returns the current View @ Override public Object instantiateItem (ViewGroup container, int position) {container. addView (views. get (position); return views. get (position) ;}// destroyItem (): deletes the current View;
@ Override public void destroyItem (ViewGroup container, int position, Object object) {container. removeView (views. get (position);} // isViewFromObject checks whether the current View is the same as the expected Object (value: View); returns trun/false;
@Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return (arg0 == arg1); }}
3. Create a Guide class, get the View object, put the view object in List <view>, and then use List <view> to put it in the adapter ViewPagerAdapter. Finally, get the ViewPager and
Public class Guide extends Activity {private List <View> listView; private ViewPager viewPager; private ViewPagerAdapter vpAdapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. view_pager); init ();} private void init () {LayoutInflater inflater = LayoutInflater. from (this); listView = new ArrayList <View> (); // obtain the View object View view1 = inflater. inflate (R. layout. view_pager_img1, null); View view2 = inflater. inflate (R. layout. view_pager_img2, null); View view3 = inflater. inflate (R. layout. view_pager_img3, null); View view4 = inflater. inflate (R. layout. view_pager_img4, null); // place the view object in List <view> listView. add (view1); listView. add (view2); listView. add (view3); listView. add (view4); // List <view> place vpAdapter = new ViewPagerAdapter (listView, this) in the adapter ViewPagerAdapter; // obtain ViewPage, set adapter; viewPager = (ViewPager) findViewById (R. id. viewpager); viewPager. setAdapter (vpAdapter );}}
4. Create view_pager_img.xml;
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" ><ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background2"/></LinearLayout>