One. Viewpager page slide Switch implementation:
Reference to other people's source code, or relatively easy to achieve, and this effect is often used, it is hereby recorded:
:
Main interface:
public class Mainactivity extends Activity {private Viewpager mpager;//page card content private list<view> listviews;//tab page List Private ImageView cursor;//animated picture private TextView T1, T2, t3;//tab header private int offset = 0;//animated picture offset private int Currindex = 0;//Current page card number private int bmpw;//animated picture width @overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_tab_main); Initimageview (); Inittextview (); InitViewPager ();} Initial options private void Inittextview () {t1 = (TextView) Findviewbyid (r.id.text1); t2 = (TextView) Findviewbyid (r.id.text2); T3 = (TextView) Findviewbyid (R.ID.TEXT3); T1.setonclicklistener (new Myonclicklistener (0)); T2.setonclicklistener (new Myonclicklistener (1)); T3.setonclicklistener (New Myonclicklistener (2));} Initialize viewpagerprivate void Initviewpager () {Mpager = (Viewpager) Findviewbyid (r.id.vpager); listviews = new ArrayList <View> (); Layoutinflater minflater = Getlayoutinflater (); Listviews.add (Minflater.inflate (r.layout.activity_Tab_one_pager, null)); Listviews.add (minflater.inflate (R.layout.activity_tab_two_pager, null)); Listviews.add ( Minflater.inflate (R.layout.activity_tab_three_pager, null)); Mpager.setadapter (New Myviewpageradapter (ListViews, this);//The first page mpager.setcurrentitem (0) is selected by default; Mpager.setonpagechangelistener (new Myonpagechangelistener ());} Initialize animation private void Initimageview () {cursor = (ImageView) Findviewbyid (r.id.cursor); BMPW = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.ICON_TAB_PAGE_BG). GetWidth ();//Get picture width displaymetrics dm = New Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); int screenw = dm.widthpixels;// Get resolution width offset = (SCREENW/3-BMPW)/2;//compute offset Matrix matrix = new Matrix () matrix.posttranslate (offset, 0); Cursor.setim Agematrix (matrix);//Set Animation initial position}//page Card listener public class Myonclicklistener implements View.onclicklistener {private int index = 0;public myonclicklistener (int i) {index = i;} public void OnClick (View v) {mpager.setcurrentitem (index);}};/ /Swipe to listen public Class Myonpagechangelistener implements Onpagechangelistener {int one = offset * 2 + bmpw;//page Card 1-page Card 2 offset int one = O NE * 2;//page Card 1-page Card 3 offset//arg0 indicates the number of pages public void onpageselected (int arg0) {//Translateanimation displacement animation animation Anim ation = null;//represents a jump from the first page to the current page card switch (arg0) {case 0:if (currindex = = 1) {animation = new Translateanimation (one, 0, 0, 0); } else if (Currindex = = 2) {animation = new Translateanimation (0, 0, 0);} Break;case 1:if (Currindex = = 0) {animation = new Translateanimation (offset, one, 0, 0);} else if (Currindex = = 2) {Animat Ion = new Translateanimation (both, one, 0, 0);} Break;case 2:if (Currindex = = 0) {animation = new Translateanimation (offset, 0, 0);} else if (Currindex = = 1) {Animat Ion = new Translateanimation (one, 0, 0);} break;} Currindex = Arg0;animation.setfillafter (TRUE);//true: Indicates that the picture stops at the end of the animation animation.setduration (300); Cursor.startanimation (animation);} public void onpagescrolled (int arg0, float arg1, int arg2) {}public void onpagescrollstatechanged (int arg0) {}}}
Viewpager Adapter:
public class Myviewpageradapter extends Pageradapter {Private list<view> mlistviews;private context context; Public Myviewpageradapter (list<view> mlistviews, Context context) {this.mlistviews = Mlistviews;this.context = Context;} @Overridepublic void Destroyitem (View arg0, int arg1, Object arg2) {(Viewpager) arg0). Removeview (Mlistviews.get (arg1)) ;} @Overridepublic void Finishupdate (View arg0) {} @Overridepublic int getcount () {return mlistviews.size ();} @Overridepublic Object Instantiateitem (View arg0, int arg1) {if (Arg1 < 3) {(Viewpager) arg0). AddView (Mlistviews.get ( ARG1), 0);} Page Card Click event if (arg1 = = 0) {button btn = (button) Arg0.findviewbyid (R.ID.BTN); Btn.setonclicklistener (new View.onclicklistener () {public void OnClick (View v) {showDialog ("1");}}); if (arg1 = = 1) {button btn = (button) Arg0.findviewbyid (R.ID.BTN); Btn.setonclicklistener (new View.onclicklistener () { public void OnClick (View v) {showDialog ("2");}}); if (arg1 = = 2) {button btn = (button) Arg0.findviewbyid (R. id.btn); Btn.setonclicklistener (new View.onclicklistener () {public void OnClick (View v) {showDialog ("3");}}); Return Mlistviews.get (ARG1);} public void ShowDialog (String arg) {new Alertdialog.builder (context). Settitle ("description"). Setmessage ("This is the first" +arg+ "page"). Setnegativebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog, int which) { }}). Show ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = (arg1);} @Overridepublic void Restorestate (parcelable arg0, ClassLoader arg1) {} @Overridepublic parcelable saveState () {return null;} @Overridepublic void Startupdate (View arg0) {}}
Xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background=" #FF969696 " android:orientation= "vertical" > <linearlayout android:id= "@+id/linearlayout1" Android:layout_widt H= "Fill_parent" android:layout_height= "45DP" android:background= "#FFDFD7D7" > <textview Android:id= "@+id/text1" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "\ n Page card 1" Android:textco Lor= "#000000"/> <textview android:id= "@+id/text2" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "\ n Page Card 2" anDroid:textcolor= "#000000"/> <textview android:id= "@+id/text3" android:layout_width= "0d P "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:gravity=" center "android:text=" \ n Page Card 3 "android:textcolor=" #000000 "/> </LinearLayout> <imageview Android:id= "@+id/cursor" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:scaletype= "Matrix" android:src= "@drawable/icon_tab_page_bg"/> <android.support.v4.view.viewpage R android:id= "@+id/vpager" android:layout_width= "wrap_content" android:layout_height= "Fill_parent" android:layout_gravity= "Center" android:layout_weight= "1" android:background= "#FFDFD7D7" Android : flipinterval= "android:persistentdrawingcache=" animation "/></linearlayout>
Three pages:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:background = "#FFDFD7D7" android:orientation= "vertical" > <button android:id= "@+id/btn" android: Layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_gravity= "center" android:gravity= "center" android:text= "click Me"/></linearlayout>
OK, it's done.
Android (8) viewpager page swipe Toggle