Viewpager Implementing an infinite loop scrolling slide

Source: Internet
Author: User

Always want to do the effect of circular slide slide, similar to PPTV, such as the home page effect is very sorry, not the whole GIF motion diagram ~ ~ ~

Less nonsense, first look at the effect:



The idea is: Set Pageradapter count as Integer.max_value, but the actual item only a few, with the way to take the remainder of the item, set the adapter at the same time set the CurrentItem to the actual item number n times ( Be big enough). This allows you to slide seamlessly around the loop! (After all, you can't skate on billions of times), and don't worry about memory problems, just the stuff.


There is no use of fragmentpageradapter or fragmentstatepageradapter, because these two item is fragment, they have their own fragment management mechanism, if used, getfragment () Return the same fragment (the pointer is the same) will be an error, do not believe it try ~ ~ ~

To inherit Pageradapter, use view as item, overriding the necessary methods ...

The indicator in the lower right-hand corner of the figure is not what I did, but I modified it a little to make him use my "infinite loop" method.

Too much nonsense, on the part of the source code:

This is adapter:

/** * Custom View Slide Adapter * Infinite loop * @author Hezb */public class Customviewpageradapter extends Pageradapter{private final String T AG = "Hezb";p rivate int realcount = 0;private viewgroup.layoutparams LP = new Viewgroup.layoutparams (Viewgroup.layoutpara Ms. Match_parent, ViewGroup.LayoutParams.MATCH_PARENT);p rivate list<view> viewlist;/** * Incoming list of custom View */public Customviewpageradapter (list<view> viewlist) {if (viewlist = = null) {LOG.E (TAG, "Customviewpageradapter viewList I S null! "); return;} Realcount = Viewlist.size (); this.viewlist = viewlist;for (int i = 0; i < viewlist.size (); i++) {Viewlist.get (i). SetLayo UTPARAMS (LP);}} @Overridepublic int GetCount () {return realcount = = 0? 0:integer.max_value;} /** * @return The actual item number */public int Getrealcount () {return realcount;} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic Object Instantiateitem (View container, int position) {position = position% Realcount;try {((Viewpager) CO NtaIner). AddView (Viewlist.get (position));} catch (Exception e) {log.e (TAG, E.getmessage ());} return Viewlist.get (position);} @Overridepublic Object Instantiateitem (viewgroup container, int position) {position = position% Realcount;try {(viewpag ER) container). AddView (Viewlist.get (position));} catch (Exception e) {log.e (TAG, E.getmessage ());} return Viewlist.get (position);} @Overridepublic void Destroyitem (view container, int position, object object) {(ViewGroup) container). Removeview ((view ) object);} @Overridepublic void Destroyitem (ViewGroup container, int position, object object) {(ViewGroup) container). Removeview ( (View) object);}}

Alas, the ability to express needs to be improved, directly under the engineering documents to see it!

Click to download!!!




Viewpager Implementing an infinite loop scrolling slide

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.