ViewPager, PagerAdapter, and viewpager

Source: Internet
Author: User

ViewPager, PagerAdapter, and viewpager

ViewPager is a component that can be used to slide the internal View. He has an old partner PagerAdapter. This time, let's take a look at the capabilities of the two of them.

We need to use ViewPager and PagerAdapter

 

 

First layout the file

<Android. support. v4.view. ViewPager
Android: id = "@ + id/viewpager"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">

Then implement your own adapter to inherit PagerAdapter. Implementation of PagerAdapter

1. public int getCount (); // obtain the total number of views

2. public void startUpdate (ViewGroup container );
3. public Object instantiateItem (ViewGroup container, int position); // instantiate the current View and add it to contrainer, then return the generated View or associated Object (used for checking in isViewFromObject)

4. public void destroyItem (ViewGroup container,Int position, Object object); // remove the specified View from the container (release memory)
5. public void finishUpdate (ViewGroup container );

6. public boolean isViewFromObject (View view, Object object); // check whether the View is associated with an Object,

The demo is as follows:

 

 

Package com. example. communityfunction. myView; import java. util. list; import android. animation. animator; import android. animation. animator. animatorListener; import android. animation. animatorSet; import android. animation. objectAnimator; import android. app. actionBar. layoutParams; import android. content. context; import android. graphics. bitmap; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. imageView; public class ImgViewPagerAdapter extends PagerAdapter {private List <Bitmap> imgBm; private Context mContext; private ViewPager pager; public ImgViewPagerAdapter (Context context, List <Bitmap> imgBm, ViewPager pager) {this. imgBm = imgBm; this. mContext = context; this. pager = pager;} @ Override public int getCount () {return imgBm. size () ;}@ Override public Object instantiateItem (final ViewGroup container, int position) {ImageView imageView = new ImageView (mContext); imageView. setImageBitmap (imgBm. get (position); container. addView (imageView, LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); imageView. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {startAnim () ;}}); return imageView ;}@ Override public void destroyItem (ViewGroup container, int position, Object object) {container. removeView (View) object) ;}; @ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1; // official prompt WRITING}
/*
* Animation effect
*/Public void startAnim () {AnimatorSet animSet = new AnimatorSet (); animSet. play (ObjectAnimator. ofFloat (pager, "alpha", 1.0f, 0f )). with (ObjectAnimator. ofFloat (pager, "scaleX", 1.0f, 0f )). with (ObjectAnimator. ofFloat (pager, "scaleY", 1.0f, 0f); animSet. setDuration (400); animSet. start (); animSet. addListener (new AnimatorListener () {@ Override public void onAnimationStart (Animator animation) {}@ Override public void Merge (Animator animation) {pager. setVisibility (View. GONE) ;}@ Override public void onAnimationCancel (Animator animation ){}});}}

 

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.