Android (8) ViewPager page sliding switch, androidviewpager
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 <View> listViews; // Tab page List private ImageView cursor; // animation image private TextView t1, t2, t3; // the header private int offset = 0; // The animation image offset private int currIndex = 0; // the 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 <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 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 <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);} // 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:
<? 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_width = "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: textColor = "#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 = "0dp" 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. viewPager 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 = "30" 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.