Android (8) ViewPager page slide Switching

Source: Internet
Author: User

Android (8) ViewPager page slide Switching

1. Implement sliding switching of ViewPager pages:

It is easier to use the source code of others, and this effect is often used. We hereby record it as follows:

:

Main Interface:

 

Public class MainActivity extends Activity {private ViewPager mPager; // page card content private List
 
  
ListViews; // Tab page list private ImageView cursor; // Animated Image private TextView t1, t2, t3; // the header of the page card is private int offset = 0; // animation image offset private int currIndex = 0; // current page card number private int bmp w; // animation Image Width @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_tab_main); InitImageView (); InitTextView (); InitViewPager ();} // initial option private void InitTextView () {t1 = (TextView) findViewById (R. id. text1); t2 = (TextView) findViewById (R. id. text2); t3 = (TextView) findViewById (R. id. text3); listener (new MyOnClickListener (0); t2.setOnClickListener (new MyOnClickListener (1); listener (new MyOnClickListener (2);} // initialize ViewPagerprivate void InitViewPager () {mPager = (ViewPager) findViewById (R. id. vPager); listViews = new ArrayList
  
   
(); 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 is selected by default. setCurrentItem (0); mPager. setOnPageChangeListener (new MyOnPageChangeListener ();} // initialize the animation private void InitImageView () {cursor = (ImageView) findViewById (R. id. cursor); bmp w = BitmapFactory. decodeResource (getResources (), R. drawable. icon_tab_page_bg ). getWidth (); // obtain the image width DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); int screenW = dm. widthPixels; // get the resolution width offset = (screenW/3-bmp w)/2; // calculate the offset Matrix matrix = new Matrix (); matrix. postTranslate (offset, 0); cursor. setImageMatrix (matrix); // sets the initial animation position} // The page card listens to the public class MyOnClickListener implements View. onClickListener {private int index = 0; public MyOnClickListener (int I) {index = I;} public void onClick (View v) {mPager. setCurrentItem (index) ;}}; // sliding listener public class MyOnPageChangeListener implements OnPageChangeListener {int one = offset * 2 + bmp w; // page Card 1-> page Card 2 offset int two = one * 2; // page Card 1-> page card 3 offset // arg0 indicates the number of pages public void onPageSelected (int arg0) {// TranslateAnimation displacement Animation animation Animation = null; // switch (arg0) {case 0: if (currIndex = 1) {animation = new TranslateAnimation (one, 0, 0, 0, 0);} else if (currIndex = 2) {animation = new TranslateAnimation (two, 0, 0, 0);} break; case 1: if (currIndex = 0) {animation = new TranslateAnimation (offset, one, 0, 0);} else if (currIndex = 2) {animation = new TranslateAnimation (two, one, 0, 0 );} break; case 2: if (currIndex = 0) {animation = new TranslateAnimation (offset, two, 0, 0);} else if (currIndex = 1) {animation = new TranslateAnimation (one, two, 0, 0);} break;} currIndex = arg0; animation. setFillAfter (true); // true: indicates that the image is parked at the animation end position. setDuration (1, 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
 
  
MListViews; private Context context; public MyViewPagerAdapter (List
  
   
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);} // click the 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 + 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:

 

 
  
 

Three pages:

 

 

 
 
  
 

OK.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.