Android ViewPager example

Source: Internet
Author: User

Android ViewPager example

Viewpager is used today to implement multi-view animation switching. I made one by myself.

First, it is just a simple example.


Step: 1. Add the viewPager layout in the main layout file.

     
          
           
           
       
          
              
 

(You may need to import the jar package. : Android-support-v4.jar)

Create 3 more layout for filling in ViewPager. Here is a textview.

2. viewPager requires a subclass of pagerAdapter.

Package com. away. viewpager; import java. util. list; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. view; public class ViewPagerAdapter extends PagerAdapter {List
 
  
ViewLists; public ViewPagerAdapter (List
  
   
Lists) {viewLists = lists;} // obtain the size @ Override public int getCount () {return viewLists. size () ;}@ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}// destroy Item @ Override public void destroyItem (View view View, int position, Object object) {(ViewPager) view ). removeView (viewLists. get (position);} // instantiate Item @ Override public Object instantiateItem (View view, int position) {(ViewPager) view ). addView (viewLists. get (position), 0); return viewLists. get (position );}}
  
 
3. At last, mainActivity mainly writes the left-right sliding switching page, and an animated effect of small images switching displacement with the page.
Package com. away. viewpager; import java. util. arrayList; import java. util. list; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. matrix; import android. OS. bundle; import android. support. v4.view. viewPager; import android. util. displayMetrics; import android. view. menu; import android. view. view; import android. view. animation. animation; import android. view. animation. translateAnimation; import android. widget. imageView; import android. widget. textView; public class MainActivity extends Activity {private ViewPager viewPager; private ImageView imageView; private List
 
  
Lists = new ArrayList
  
   
(); Private ViewPagerAdapter adapter; private Bitmap cursor; private int offSet; private int currentItem; private Matrix matrix = new Matrix (); private int bmWidth; private Animation animation; private TextView textView1; private TextView textView2; private TextView textView3; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageView = (ImageView) findViewById (R. id. cursor); textView1 = (TextView) findViewById (R. id. textView1); textView2 = (TextView) findViewById (R. id. textView2); textView3 = (TextView) findViewById (R. id. textView3); lists. add (getLayoutInflater (). inflate (R. layout. layout1, null); lists. add (getLayoutInflater (). inflate (R. layout. layout2, null); lists. add (getLayoutInflater (). inflate (R. layout. layout3, null); initeCursor (); adapter = new ViewPagerAdapter (lists); viewPager = (ViewPager) findViewById (R. id. viewPager); viewPager. setAdapter (adapter); viewPager. setOnPageChangeListener (new ViewPager. onPageChangeListener () {// when sliding, The imageView on the top slides slowly through animation @ Overridepublic void onPageSelected (int arg0) {switch (arg0) {case 0: if (currentItem = 1) {animation = new TranslateAnimation (offSet * 2 + bmWidth, 0, 0);} else if (currentItem = 2) {animation = new TranslateAnimation (offSet * 4 + 2 * bmWidth, 0, 0);} break; case 1: if (currentItem = 0) {animation = new TranslateAnimation (0, offSet * 2 + bmWidth, 0, 0);} else if (currentItem = 2) {animation = new TranslateAnimation (4 * offSet + 2 * bmWidth, offSet * 2 + bmWidth, 0, 0);} break; case 2: if (currentItem = 0) {animation = new TranslateAnimation (0, 4 * offSet + 2 * bmWidth, 0, 0);} else if (currentItem = 1) {animation = new TranslateAnimation (offSet * 2 + bmWidth, 4 * offSet + 2 * bmWidth, 0, 0) ;}} currentItem = arg0; animation. setDuration (150); // The animation speed at which the cursor slides. setFillAfter (true); imageView. startAnimation (animation) ;}@ Overridepublic void onPageScrolled (int arg0, float arg1, int arg2) {}@ Overridepublic void merge (int arg0) {}}); merge (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {viewPager. setCurrentItem (0) ;}}); textView2.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {viewPager. setCurrentItem (1) ;}}); textView3.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {viewPager. setCurrentItem (2) ;}});} private void initeCursor () {cursor = BitmapFactory. decodeResource (getResources (), R. drawable. cursor); bmWidth = cursor. getWidth (); DisplayMetrics dm; dm = getResources (). getDisplayMetrics (); offSet = (dm. widthPixels-3 * bmWidth)/6; matrix. setTranslate (offSet, 0); imageView. setImageMatrix (matrix); // the scaleType required for iamgeView is matrixcurrentItem = 0 ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
  
 
The optical map is attached.

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.