Current situation
Without modifying the source code, when viewpager slides to the last item, it cannot slide to the right. When viewpager slides to the first item, he cannot move forward. (All the above are nonsense)
Ideas
We can think like this, when sliding the last one, we let him jump to the first one, so that he can continue to slide back, so that we want the cycle sliding.
In this case, although the function is a loop, it will automatically jump between the last one and the first one in actual display.
Optimization
We can add a fake item at the beginning and end of the original linked list, and use two additional items for the jump action, so as to avoid the error picture of automatic jump. See the following demo.
The following figure shows A, B, and C at 0, 1, and 2.
In fact, when we add data, we add two more. The last Interface C is added at location 0, and the first interface A is added at location 4.
When the interface slides to position 3, it can also slide to the right, which gives people the feeling of loop. However, when sliding to position 4, there is no more on the right side of him. Isn't that just a cover? Therefore, when sliding to position 4, jump to position 1 immediately. Because they are the same data, we can't see the jump from the display effect, so we actually changed to position 1, so we can continue to slide to the right.
Repeat the judgment of the above conditions to realize the cycle to the right.
Code Analysis
Perform condition judgment in onpageselected and jump in onpagescrollstatechanged. The key code is as follows:
Initialize and add an item at the beginning and end.
// Add 1st interfaces. In fact, the last interface is addtextview (point_length-1 ); // Add the displayed 2, 3, and 4 interfaces for (INT I = 0; I <3; I ++) {addtextview (I); addpoint (I );} // Add the last 5th interfaces. In fact, the first interface is addtextview (0 );
Condition determination:
@ Overridepublic void onpageselected (INT pposition) {mischanged = true; If (pposition> point_length) {mcurrentpageposition = cursor;} else if (pposition <first_item_index) {mcurrentpageposition = point_length ;} else {mcurrentpageposition = pposition;} log. I (TAG, "the current position is" + mcurrentpageposition); setcurrentdot (mcurrentpageposition );}
Jump:
@Overridepublic void onPageScrollStateChanged(int pState) {if (ViewPager.SCROLL_STATE_IDLE == pState) {if (mIsChanged) {mIsChanged = false;mViewPager.setCurrentItem(mCurrentPagePosition, false);}}}
The complete logic is as follows:
Package COM. ahacool. circleviewpager; import Java. util. arraylist; import android. app. activity; 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. log; import android. view. gravity; import android. view. view; import android. view. viewgroup; import android. widget. imageview; Import Android. widget. linearlayout. layoutparams; import Android. widget. textview;/*** @ classname mainactivity * @ description one way to move viewpager cyclically and smoothly. Implementation Method: add an interface at the beginning and end of the displayed interface. * @ Author Moto * @ date 2014 2014-7-18 **/public class mainactivity extends activity implements onpagechangelistener {private viewpager mviewpager; private viewgroup mpointviewgroup; private arraylist <View> mviewpagerlist; private Boolean mischanged = false; private int mcurrentpageposition = first_item_index; private int mcurrentindex; Private Static final int point_length = 3; Private Static final int First_item_index = 1; Private Static final string tag = "Moto"; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); initui ();} private void initui () {mviewpager = (viewpager) findviewbyid (R. id. viewpager); mpointviewgroup = (viewgroup) findviewbyid (R. id. point_layout); mviewpagerlist = new arraylist <View> (); // adds 1st interfaces. Display the last interface addtextview (point_length-1); // Add the actually displayed 2, 3, and 4 interfaces for (INT I = 0; I <3; I ++) {addtextview (I); addpoint (I) ;}// add the last 5th interfaces. In fact, the first interface is addtextview (0 ); pageradapter = new custompageradapter (mviewpagerlist); mviewpager. setadapter (pageradapter); mviewpager. setonpagechangelistener (this); mviewpager. setcurrentitem (mcurrentpageposition, false);} private void addtextview (INT pindex) {textvi EW textview = new textview (this); textview. setlayoutparams (New layoutparams (layoutparams. match_parent, layoutparams. match_parent); textview. setgravity (gravity. center); textview. settext ("this is the" + (pindex + 1) + "page"); textview. settextsize (50); mviewpagerlist. add (textview);} private void addpoint (INT pindex) {imageview pointimageview = new imageview (this); layoutparams = new layoutparams (20, 2 0); layoutparams. setmargins (10, 0, 10, 0); pointimageview. setlayoutparams (layoutparams); pointimageview. setbackgroundresource (R. drawable. point_style); If (0 = pindex) {pointimageview. setenabled (false);} mpointviewgroup. addview (pointimageview);} private void setcurrentdot (INT positon) {// The serial number actually displayed on the page is 1st, 2, 3. The number of the vertex should be 0, 1, 2. so subtract 1. positon = positon-1; if (positon <0 | positon> mviewpagerlist. size ()-1 | mcurrentindex = positon) {return;} mpointviewgroup. getchildat (positon ). setenabled (false); mpointviewgroup. getchildat (mcurrentindex ). setenabled (true); mcurrentindex = positon;} @ overridepublic void onpagescrollstatechanged (INT pstate) {If (viewpager. scroll_state_idle = pstate) {If (mischanged) {mischanged = false; mviewpager. setcurrentitem (mcurrentpageposition, false) ;}}@ overridepublic void evaluate (INT arg0, float arg1, int arg2) {}@ overridepublic void onpageselected (INT pposition) {mischanged = true; if (pposition> point_length) {mcurrentpageposition = first_item_index;} else if (pposition <first_item_index) {mcurrentpageposition = point_length;} else {mcurrentpageposition = pposition;} log. I (TAG, "the current position is" + mcurrentpageposition); setcurrentdot (mcurrentpageposition );}}
Source code in the address: https://github.com/bird7310/Demos.git
Summary
I hope to help you and give more comments. Recently, the project was very busy and I haven't read my blog for a long time. All the projects are numb, relax, and be lazy. write a blog.
Infinite Loop viewpager