Android ViewPager Lesson 2: androidviewpager
Paste only part of the code here
In the first lesson, the animation effect is triggered only after the View slides. In the second lesson, the slider and View are synchronized.
SecondActivity. java
Package com. android3; import android. annotation. suppressLint; import android. content. intent; import android. graphics. bitmapFactory; import android. graphics. matrix; import android. OS. bundle; import android. support. v4.view. viewPager; import android. support. v7.app. appCompatActivity; import android. support. v7.widget. toolbar; import android. util. displayMetrics; import android. view. gravity; import android. view. layo UtInflater; import android. view. view; import android. widget. button; import android. widget. imageView; import android. widget. linearLayout; import android. widget. textView; import java. util. arrayList; import java. util. list; public class SecondActivity extends AppCompatActivity implements View. onClickListener, ViewPager. onPageChangeListener {private ArrayList <View> viewList; private ImageView cursor; private Float offset = 0; private float screenW = 0; private float eCurrentX = 0; private Matrix matrix; private float fScreenW = 0; private int currentIndex = 0; private int temp = 1; private float sCurrentX; private int len; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_second); initToolbar (); initViewPager ();}/*** ViewPager ensures that there are three views in the cache, namely, destroy, */@ SuppressLint ("InflateParams") private void initViewPager () {ViewPager viewPager = (ViewPager) findViewById (R. id. viewpager); LinearLayout titleBar = (LinearLayout) findViewById (R. id. titleBar); LayoutInflater inflater = getLayoutInflater (); // create four View view1 = inflater. inflate (R. layout. viewpage_01, null); View view2 = inflater. inflate (R. layo Ut. viewpage_02, null); View view3 = inflater. inflate (R. layout. viewpage_03, null); View view4 = inflater. inflate (R. layout. viewpage_04, null); viewList = new ArrayList <> (); // load the View to be displayed by page into the viewList array. add (view1); viewList. add (view2); viewList. add (view3); viewList. add (view4); len = viewList. size (); MyPagerAdapter adapter = new MyPagerAdapter (viewList); List <String> titleList = new ArrayList <> (); titl EList. add ("first page"); titleList. add ("second page"); titleList. add ("Third page"); titleList. add ("fourth page"); for (int I = 0; I <titleList. size (); I ++) {TextView textView = new TextView (this); LinearLayout. layoutParams params = new LinearLayout. 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 the position of the indicator viewPager. setAdapter (adapter); // bind the adapter viewPager. addOnPageChangeListener (this); // Note: setOnPageChangeListener obsolete}/*** unit px */public void initCursorPos () {// initialize the animation cursor = (ImageView) findViewById (R. id. cursor); float cursorW = BitmapFac Invalid. decodeResource (getResources (), R. mipmap. cursor ). getWidth (); DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); screenW = dm. widthPixels; // get the resolution width fScreenW = screenW/viewList. size (); offset = (fScreenW-cursorW)/2; // calculate the offset matrix = new Matrix (); matrix. postTranslate (offset, 0); cursor. setImageMatrix (matrix); // sets the initial animation position ### original position} private void InitToolbar () {Toolbar mToolbar = (Toolbar) findViewById (R. id. toolbar); mToolbar. setTitle (""); mToolbar. setNavigationIcon (R. mipmap. back); setsuppactionactionbar (mToolbar); mToolbar. setNavigationOnClickListener (this);} @ Override public void onClick (View view) {switch (view. getId () {case-1: finish (); break ;}@override public void onPageScrolled (int position, float positionOffset, int positionO FfsetPixels) {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 void onPageSelected (int position) {}@ Override public void onPageScrollStateChanged (int state ){}}
The following describes three rewrite methods for the ViewPager. OnPageChangeListener listener:
When you press your finger to stop the page:
First, the system calls the onPageScrollStateChanged (int state) method. state = 1 indicates that the slide starts;
Then the system calls the onPageScrolled (int position, float positionOffset, int positionOffsetPixels) method,
Position is the number of the current page, positionOffset sliding percentage, value range: [0, 1], positionOffsetPixels sliding length, value: [0, mobile phone width (px)]
/*************************************** **************************************** **/
The following factors need to be emphasized in the Sliding Process:
1. Sliding positionOffset and positionOffsetPixels from left to right is set to 0, and increases according to the sliding distance;
2. Slide positionOffset from the right to the left with an initial value of 1 and decrease according to the sliding distance. The initial value of positionOffsetPixels is the mobile phone width, and the sliding distance is reduced;
3. When position = 0, slide from right to left. positionOffset and positionOffsetPixels are always 0;
4. When (position = total number of pages-1), slide from left to right. positionOffset and positionOffsetPixels are always 0;
/*************************************** **************************************** **/
The system may call the onPageScrollStateChanged (int state) method state = 2 again when the finger leaves the screen during the slide process. When the slide is bound to the next page,
PositionOffset and positionOffsetPixels both reach the maximum value, and then call the onPageScrolled (int position, float positionOffset, int positionOffsetPixels) method again,
Position indicates the number of the new page, and positionOffset and positionOffsetPixels are set to zero;
Finally, call the onPageScrollStateChanged (int state) method state = 0, indicating that the sliding stops;
So the code in the onPageScrolled () method is explained,
sCurrentX = positionOffset * fScreenW;
FScreenW is the distance from the slider to the other title. sCurrentX is the relative distance of the slider when sliding the page,
The member variable temp is used to identify whether the onPageScrolled () is called for the first time when a page is swiped. When the method is called for the first time, the member variable temp is used to determine whether to slide left or right:
if (positionOffset > 0.5) { eCurrentX = fScreenW; } else { eCurrentX = 0; }
When the slide stops, reset temp to 1;
The cursor slide uses the matrix. postTranslate (sCurrentX-eCurrentX), 0); cursor. setImageMatrix (matrix); Code, relative displacement.
Thank you.
The system may call the onPageScrollStateChanged (int state) method again to state = 2,
When you slide to the next page, both positionOffset and positionOffsetPixels reach the maximum value,
Call the onPageScrolled (int position, float positionOffset, int positionOffsetPixels) method again,
Position indicates the number of the new page, and positionOffset and positionOffsetPixels are set to zero;
By: naughty child
Source: http://www.cnblogs.com/xiaotaoqi/p/5998845.html/>
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and give it clearly on the article page.