Let's take a look:
Main Code:
Package COM. example. introducedemo; import Java. util. arraylist; import android. app. activity; import android. OS. bundle; import android. OS. parcelable; import android. support. v4.view. pageradapter; import android. support. v4.view. viewpager; import android. support. v4.view. viewpager. onpagechangelistener; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup. layoutparams; import android. view. window; import android. widget. imageview; import android. widget. linearlayout; public class mainactivity extends activity {private imageview mimageview; view mview; private arraylist <View> mpageviews; // defines layoutinflater minflater; private viewpager mviewpager; linearlayout extends; private imageview [] mimageviews; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // set the window without the title requestwindowfeature (window. feature_no_title); minflater = getlayoutinflater (); mpageviews = new arraylist <View> (); mpageviews. add (minflater. inflate (R. layout. view_01, null); mpageviews. add (minflater. inflate (R. layout. view_02, null); mpageviews. add (minflater. inflate (R. layout. view_03, null); mimageviews = new imageview [mpageviews. size ()]; mview = minflater. inflate (R. layout. activity_main, null); mviewpager = (viewpager) mview. findviewbyid (R. id. myviewpager); mlinearlayout = (linearlayout) mview. findviewbyid (R. id. mybottomviewgroup); For (INT I = 0; I <mimageviews. length; I ++) {mimageview = new imageview (mainactivity. this); mimageview. setlayoutparams (New layoutparams (10, 10); linearlayout. layoutparams Lp = new linearlayout. layoutparams (linearlayout. layoutparams. wrap_content, linearlayout. layoutparams. wrap_content); LP. setmargins (10, 0, 10, 0); mimageview. setlayoutparams (LP); if (I = 0) {mimageview. setbackgroundresource (R. drawable. page_indicator_focused);} else {mimageview. setbackgroundresource (R. drawable. page_indicator_unfocused);} mimageviews [I] = mimageview; // Add the image with a long distance to the bottom view of mlinearlayout. addview (mimageviews [I]);} setcontentview (mview); mviewpager. setadapter (New mypageradapter (); mviewpager. setonpagechangelistener (New onpagechangelistener () {public void onpageselected (INT arg0) {for (INT I = 0; I <mimageviews. length; I ++) {if (I = arg0) {mimageviews [I]. setbackgroundresource (R. drawable. page_indicator_focused);} else {mimageviews [I]. setbackgroundresource (R. drawable. response) ;}}@ override public void onpagescrolled (INT arg0, float arg1, int arg2) {// todo auto-generated method stub} @ override public void onpagescrollstatechanged (INT arg0) {// todo auto-generated method stub});} class mypageradapter extends pageradapter {@ override public int getcount () {return mpageviews. size () ;}@ override public Boolean isviewfromobject (view arg0, object arg1) {return arg0 = arg1 ;}@ override public int getitemposition (Object object) {// todo auto-generated method stub return Super. getitemposition (object) ;}@ override public void destroyitem (view arg0, int arg1, object arg2) {// todo auto-generated method stub (viewpager) arg0 ). removeview (mpageviews. get (arg1) ;}@ override public object instantiateitem (view arg0, int arg1) {// todo auto-generated method stub (viewpager) arg0 ). addview (mpageviews. get (arg1); Return mpageviews. get (arg1) ;}@ override public void restorestate (parcelable arg0, classloader arg1) {// todo auto-generated method stub} @ override public parcelable savestate () {// todo auto-generated method stub return NULL;} @ override public void startupdate (view arg0) {// todo auto-generated method stub} @ override public void finishupdate (view arg0) {// todo auto-generated method stub }}}
Http://download.csdn.net/detail/wenwei19861106/4860072 (csdn)