Just paste part of the code here
In the first lesson, only the view slide is complete, the animation effect is triggered, the slider moves, and in the second lesson, the implementation slider runs in sync with view.
Secondactivity.java
Packagecom.android3;ImportAndroid.annotation.SuppressLint;Importandroid.content.Intent;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Matrix;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.ViewPager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.Toolbar;ImportAndroid.util.DisplayMetrics;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView;Importjava.util.ArrayList;Importjava.util.List; Public classSecondactivityextendsAppcompatactivityImplementsView.onclicklistener, Viewpager.onpagechangelistener {PrivateArraylist<view>viewlist; PrivateImageView cursor; Private floatOffset = 0; Private floatSCREENW = 0; Private floatEcurrentx = 0; Privatematrix Matrix; Private floatFSCREENW = 0; Private intCurrentindex = 0; Private inttemp = 1; Private floatScurrentx; Private intLen; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_second); Inittoolbar (); Initviewpager (); } /*** Viewpager guarantee that there are three views in the cache, that is, the left-right and middle-separated gray is destroy,*/@SuppressLint ("Inflateparams") Private voidInitviewpager () {Viewpager Viewpager=(Viewpager) Findviewbyid (R.id.viewpager); LinearLayout titlebar=(LinearLayout) Findviewbyid (R.id.titlebar); Layoutinflater Inflater=Getlayoutinflater (); //Create a four viewView view1 = inflater.inflate (r.layout.viewpage_01,NULL); View View2= Inflater.inflate (r.layout.viewpage_02,NULL); View VIEW3= Inflater.inflate (r.layout.viewpage_03,NULL); View View4= Inflater.inflate (R.layout.viewpage_04,NULL); Viewlist=NewArraylist<> ();//The view that will be paginated is loaded into the arrayViewlist.add (View1); Viewlist.add (VIEW2); Viewlist.add (VIEW3); Viewlist.add (VIEW4); Len=viewlist.size (); Mypageradapter Adapter=NewMypageradapter (viewlist); List<String> titlelist =NewArraylist<>(); Titlelist.add ("First Page"); Titlelist.add ("Second Page"); Titlelist.add ("Third page"); Titlelist.add ("Page Four"); for(inti = 0; I < titlelist.size (); i++) {TextView TextView=NewTextView ( This); Linearlayout.layoutparams params=Newlinearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); Params.weight= 1; Params.setmargins (5, 3, 5, 3); Textview.setlayoutparams (params); Textview.settext (Titlelist.get (i)); Textview.settextsize (15); Textview.setgravity (Gravity.center); Titlebar.addview (TextView); } initcursorpos (); //Initialize indicator positionViewpager.setadapter (adapter);//Binding AdapterViewpager.addonpagechangelistener ( This);//Note: Setonpagechangelistener obsolete } /*** unit px*/ Public voidInitcursorpos () {//Initializing animationscursor =(ImageView) Findviewbyid (r.id.cursor); floatCURSORW =Bitmapfactory.decoderesource (Getresources (), r.mipmap.cursor). GetWidth (); Displaymetrics DM=NewDisplaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); Screenw= Dm.widthpixels;//Get resolution widthFscreenw = SCREENW/viewlist.size (); Offset= (FSCREENW-CURSORW)/2;//Calculate offsetMatrix =NewMatrix (); Matrix.posttranslate (offset,0); Cursor.setimagematrix (matrix);//set the initial position of the animation # # #原始位置 } Private voidInittoolbar () {Toolbar Mtoolbar=(Toolbar) Findviewbyid (R.id.toolbar); Mtoolbar.settitle (""); Mtoolbar.setnavigationicon (R.mipmap.back); Setsupportactionbar (Mtoolbar); Mtoolbar.setnavigationonclicklistener ( This); } @Override Public voidOnClick (view view) {Switch(View.getid ()) { Case-1: Finish (); Break; }} @Override Public voidOnpagescrolled (intPositionfloatPositionoffset,intpositionoffsetpixels) {Scurrentx= Positionoffset *Fscreenw; if(position!=currentindex) {Temp=1; Currentindex=position; return; } if(temp = = 0) {matrix.posttranslate (Scurrentx-Ecurrentx), 0); Cursor.setimagematrix (matrix); Ecurrentx=Scurrentx; } Else { if(Positionoffset > 0.5) {Ecurrentx=Fscreenw; } Else{Ecurrentx= 0; } temp--; } currentindex=position; } @Override Public voidOnpageselected (intposition) {} @Override Public voidOnpagescrollstatechanged (intState ) { }}
The following is a detailed description of the three overriding methods of the Viewpager.onpagechangelistener listener:
When you press from your finger, the process of sliding to the page stops:
First the system calls the onpagescrollstatechanged (int state) method, where state=1, which indicates the start of sliding;
The system then calls onpagescrolled (int position, float positionoffset, int positionoffsetpixels) methods,
Position is the current page number, Positionoffset slide percentage, value range [0,1],positionoffsetpixels sliding length, value [0, Cell phone width (px)]
/*********************************************************************************/
There are several factors that need to be emphasized during the sliding process:
1. Swipe from left to right Positionoffset and positionoffsetpixels the initial value is 0, and increases according to the sliding distance;
2, from right to left slide Positionoffset initial value is 1, and according to the sliding distance decreases, the Positionoffsetpixels initial value is the handset width, and decreases according to the sliding distance;
3. When position=0, swipe from right to left, Positionoffset and positionoffsetpixels are always 0;
4, when (position= page total-1), swipe from left to right, Positionoffset and positionoffsetpixels are always 0;
/*********************************************************************************/
It is possible for the finger to leave the screen during the swipe, when the system calls the onpagescrollstatechanged (int state) method again state=2, when the swipe is sure to go to the next page,
Positionoffset and Positionoffsetpixels both reach the maximum value, and then call onpagescrolled again (int position, float positionoffset, int Positionoffsetpixels) method,
At this time position for the new page number, Positionoffset and positionoffsetpixels 0;
Finally, the onpagescrollstatechanged (int state) method state=0 is called and the sliding stop is indicated;
So the code in the Onpagescrolled () method is explained,
Scurrentx = Positionoffset * FSCREENW;
Fscreenw is the distance that the slider slides from one heading to the other, and Scurrentx is the distance that the slider is relative to when the page is sliding,
The member variable, temp, is the first call to Onpagescrolled () when a sliding page is distinguished, and, when the method is first called, whether to swipe left or right:
if (Positionoffset > 0.5) { = fscreenw; Else { = 0; }
When the slide stops, reset temp to 1;
The cursor is sliding using matrix.posttranslate ((Scurrentx-ecurrentx), 0); Cursor.setimagematrix (matrix); These two lines of code, relative displacements.
Thank you
The onpagescrollstatechanged (int state) method state=2 can be called again when the finger leaves the screen during the swipe process.
When the swipe is sure to go to the next page, both Positionoffset and positionoffsetpixels reach the maximum value,
Then call the onpagescrolled (int position, float positionoffset, int positionoffsetpixels) method again,
At this time position for the new page number, Positionoffset and positionoffsetpixels 0;
Little Rascal.
Source: http://www.cnblogs.com/xiaotaoqi/p/5998845.html/>
This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this statement, and in the article page obvious location given.
Android Viewpager Lesson Two