Growth notes of Android cainiao (27) -- Use of ViewPager

Source: Internet
Author: User

ViewPager is an API available for Android 3.0 and later versions.

1. What can ViewPager do?

1. ViewPager can be easily used to slide the section in Section 5.0.

2. Implement a navigation guide interface similar to Sina Weibo.

3. Implement other navigation or menus.

Ii. Use of ViewPager

1. Add Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><android.support.v4.view.ViewPager    android:id="@+id/viewpager"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="center"/>  </LinearLayout>
2. Load the layout file to be displayed (View)

        LayoutInflater lf = getLayoutInflater().from(this);        view1 = lf.inflate(R.layout.layout_0, null);        view2 = lf.inflate(R.layout.layout_1, null);        view3 = lf.inflate(R.layout.layout_2, null);
3. Create a View set

        mListViews = new ArrayList<View>();        mListViews.add(view1);        mListViews.add(view2);        mListViews.add(view3);
4. Create a PagerAdapter

The adapter of ViewPager is PagerAdapter. To implement PagerAdapter, you must at least implement the following methods:

(1) instantiateItem (ViewGroup, int) // Add

(2) destroyItem (ViewGroup, int, Object) // Delete

(3) getCount ()

(4) isViewFromObject (View, Object)

Private class MyPageAdapter extends PagerAdapter {private List <View> mListViews; public MyPageAdapter (List <View> mListViews) {this. mListViews = mListViews;}/*** delete a page card */@ Override public void destroyItem (View container, int position, Object object) {(ViewPager) container ). removeView (mListViews. get (position);}/*** instantiate the page card */@ Override public Object instantiateItem (View container, int position) {(ViewPager) container ). addView (mListViews. get (position); return mListViews. get (position);}/*** number of returned page cards */@ Overridepublic int getCount () {return mListViews. size () ;}@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1; // This is the official prompt }}
5. Configure the adapter

mViewPager.setAdapter(new MyPageAdapter(mListViews));

The Code is as follows:

Package com. example. viewpagetest; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. layoutInflater; import android. view. view; public class MainActivity extends Activity {private List <View> mListViews; private View view1; private View view2; private View view3; private ViewPager mViewPager; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mViewPager = (ViewPager) findViewById (R. id. viewpager); // load the tab LayoutInflater lf = getLayoutInflater () to be displayed (). from (this); view1 = lf. inflate (R. layout. layout_0, null); view2 = lf. inflate (R. layout. layout_1, null); view3 = lf. inflate (R. layout. layout_2, null); mListViews = new ArrayList <View> (); mListViews. add (view1); mListViews. add (view2); mListViews. add (view3); mViewPager. setAdapter (new MyPageAdapter (mListViews);} private class MyPageAdapter extends PagerAdapter {private List <View> mListViews; public MyPageAdapter (List <View> mListViews) {this. mListViews = mListViews;}/*** delete a page card */@ Override public void destroyItem (View container, int position, Object object) {(ViewPager) container ). removeView (mListViews. get (position);}/*** instantiate the page card */@ Override public Object instantiateItem (View container, int position) {(ViewPager) container ). addView (mListViews. get (position); return mListViews. get (position);}/*** number of returned page cards */@ Overridepublic int getCount () {return mListViews. size () ;}@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1; // The official prompt is as follows }}}
Page code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent">  <TextView     android:layout_width="match_parent"    android:layout_height="match_parent"    android:text="1"    android:gravity="center"    android:textSize="120sp"    android:textColor="#33b5e5"    /></LinearLayout>
Running effect:

Iii. Menu Linkage

The interfaces and classes in the android. support. v4.view package are as follows:


From the figure above, we can see PagerTabStrip and PagerTitleStrip

Literally, PagerTabStrip is a navigation tab that can be clicked, while PagerTitleStrip is a navigation tab that cannot be interacted (clicked.

Below we modify the layout file as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><android.support.v4.view.ViewPager    android:id="@+id/viewpager"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="center">    <android.support.v4.view.PagerTabStrip        android:id="@+id/pagertab"        android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="top"/>    </android.support.v4.view.ViewPager></LinearLayout>

Layout_gravity can be used to set the position of a Tab.

Modify MainActvity

MTitleList = new ArrayList <String> (); mTitleList. add ("chat"); mTitleList. add ("Discover"); mTitleList. add ("Address Book ");
Add the following code to PagerAdapter:
@Overridepublic CharSequence getPageTitle(int position) {return mTitleList.get(position);}

Paste all the Code as follows:

Package com. example. viewpagetest; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. layoutInflater; import android. view. view; public class MainActivity extends Activity {private List <String> mTitleList; private List <View> mListViews; private ViewPager mViewPager; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mViewPager = (ViewPager) findViewById (R. id. viewpager); // load the tab LayoutInflater lf = getLayoutInflater () to be displayed (). from (this); View view1 = lf. inflate (R. layout. layout_0, null); View view2 = lf. inflate (R. layout. layout_1, null); View view3 = lf. inflate (R. layout. layout_2, null); mListViews = new ArrayList <View> (); mListViews. add (view1); mListViews. add (view2); mListViews. add (view3); mTitleList = new ArrayList <String> (); mTitleList. add ("chat"); mTitleList. add ("found"); mTitleList. add ("Address Book"); mViewPager. setAdapter (new MyPageAdapter (mListViews);} private class MyPageAdapter extends PagerAdapter {private List <View> mListViews; public MyPageAdapter (List <View> mListViews) {this. mListViews = mListViews;}/*** delete a page card */@ Override public void destroyItem (View container, int position, Object object) {(ViewPager) container ). removeView (mListViews. get (position);}/*** instantiate the page card */@ Override public Object instantiateItem (View container, int position) {(ViewPager) container ). addView (mListViews. get (position); return mListViews. get (position);}/*** number of returned page cards */@ Overridepublic int getCount () {return mListViews. size () ;}@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1; // The official prompt is as follows.}/*** Set Tab */@ Overridepublic CharSequence getPageTitle (int position) {return mTitleList. get (position );}}}
Running effect:

Although we have a Tab above and can change with the page switch, this is not what we want to achieve. Next we will create a static Tab bottom and an animation.

<? 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 = "# eeeeee" android: orientation = "vertical"> <LinearLayout android: id = "@ + id/linearLayout1" android: layout_width = "fill_parent" android: layout_height = "32.0dip" android: background = "# eeeeee"> <TextView android: id = "@ + id/text1" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1.0" android: gravity = "center" android: text = "chat" android: textColor = "#000000" android: textSize = "16347dip"/> <TextView android: id = "@ + id/text2" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1.0" android: gravity = "center" android: text = "detected" android: textColor = "#000000" android: textSize = "16347dip"/> <TextView android: id = "@ + id/text3" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1.0" android: gravity = "center" android: text = "Address Book" android: textColor = "#000000" android: textSize = "16.0dip"/> </LinearLayout> <ImageView android: id = "@ + id/cursor" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: scaleType = "matrix" android: src = "@ drawable/a"/> <ImageView android: layout_width = "match_parent" android: layout_height = "1px" android: background = "# cccccc"/> <android. support. v4.view. viewPager android: id = "@ + id/vPager" android: layout_width = "wrap_content" android: layout_height = "0dp" android: layout_gravity = "center" android: layout_weight = "1.0" android: flipInterval = "30" android: background = "# ffffff" android: persistentDrawingCache = "animation"/> </LinearLayout>

Package com. example. wuye001; import java. util. arrayList; import java. util. list; import android. app. activity; import android. graphics. bitmapFactory; import android. graphics. matrix; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. util. displayMetrics; import android. view. LayoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. view. animation. animation; import android. view. animation. translateAnimation; import android. widget. imageView; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private ViewPager viewPager; // page card content private ImageView imageView ;// Animation image private TextView textView1, textView2, textView3; private List <View> views; // Tab page List private int offset = 0; // animation image offset private int currIndex = 0; // current page card number private int bmp w; // animation picture width private View view1, view2, view3; // each page card @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); InitImageView (); InitTextView (); InitViewPager ();} Private void InitViewPager () {viewPager = (ViewPager) findViewById (R. id. vPager); views = new ArrayList <View> (); LayoutInflater inflater = getLayoutInflater (); view1 = inflater. inflate (R. layout. layout_0, null); view2 = inflater. inflate (R. layout. layout_1, null); view3 = inflater. inflate (R. layout. layout_2, null); views. add (view1); views. add (view2); views. add (view3); viewPager. setAdapter (new MyViewPagerAdapter (views); vie WPager. setCurrentItem (0); viewPager. setOnPageChangeListener (new MyOnPageChangeListener ();}/*** initialize header mark */private void InitTextView () {textView1 = (TextView) findViewById (R. id. text1); textView2 = (TextView) findViewById (R. id. text2); textView3 = (TextView) findViewById (R. id. text3); textView1.setOnClickListener (new MyOnClickListener (0); textView2.setOnClickListener (new MyOnClickListener (1); textView3.se TOnClickListener (new MyOnClickListener (2);}/*** initialize the animation. This is the effect of sliding the horizontal line below when the page card slides, here you need to calculate some data */private void InitImageView () {imageView = (ImageView) findViewById (R. id. cursor); bmp w = BitmapFactory. decodeResource (getResources (), R. drawable. a ). 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); imageView. setImageMatrix (matrix); // set the initial animation position}/***** click the header to listen to 3 */private class MyOnClickListener implements OnClickListener {private int index = 0; public MyOnClickListener (int I) {index = I;} public void onClick (View v) {viewPager. setCurrentItem (index) ;}} public class MyViewPagerAdapter extends PagerAdapter {pr Ivate List <View> mListViews; public MyViewPagerAdapter (List <View> mListViews) {this. mListViews = mListViews;} @ Overridepublic void destroyItem (ViewGroup container, int position, Object object Object) {container. removeView (mListViews. get (position) ;}@ Overridepublic Object instantiateItem (ViewGroup container, int position) {container. addView (mListViews. get (position), 0); return mListViews. get (position) ;}@ Ov Erridepublic int getCount () {return mListViews. size () ;}@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}} public class MyOnPageChangeListener implements OnPageChangeListener {int one = offset * 2 + BMP; // page Card 1-> page Card 2 offset int two = one * 2; // page Card 1-> page card 3 offset public void onPageScrollStateChanged (int arg0) {} public void onPageScrolled (int arg0, float arg1, int arg2 ){} Public void onPageSelected (int arg0) {/*. This is one method. The following is another method. Obviously, this method is troublesome: Animation animation = null; switch (arg0) {case 0: if (currIndex = 1) {animation = new TranslateAnimation (one, 0, 0, 0);} else if (currIndex = 2) {animation = new TranslateAnimation (two, 0, 0);} break; case 1: if (currIndex = 0) {animation = new TranslateAnimation (offset, one, 0, 0);} else if (currIndex = 2) {animation = new Tran SlateAnimation (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;} */Animation animation = new TranslateAnimation (one * currIndex, one * arg0, 0, 0); // obviously, this is concise, with only one line of code. CurrIndex = arg0; animation. setFillAfter (true); // True: the image stops at the animation end position animation. setDuration (300); imageView. startAnimation (animation );}}}
Running effect:








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.