There are two ways to cycle the carousel, one is the use of a timer another is the use of finger-click, compared to the timer to achieve a circular playback easier, as long as the message in the timer to add a few simple pieces of code can be, the following separately on these two methods to give you detailed details, please see below.
int count = Adapter.getcount ();
if (Count > 1) {//More than 1, cycle
int index = Viewpager.getcurrentitem ();
Index = (index + 1)% count;
Viewpager.setcurrentitem (index, True);
}
But how does it support the function of the infinite loop when the finger is clicked? This problem, I have been tossing for a long time, online data is not available.
The funny thing is that in the adapter, the number is set to the maximum value, and then, at the current position, a value in the middle, such as:
public class Myadapter extends pageradapter{
@Override public
int GetCount () {return
integer.max_value;
@Override Public
boolean isviewfromobject (View arg0, Object arg1) {return
arg0 = = arg1;
}
@Override public
void Destroyitem (View container, int position, object object) {(
Viewpager) container). Removeview (mimageviews[position% count]);
Public Object instantiateitem (view container, int position) {
view view = mimageviews[position% count];
((Viewpager) container). AddView (view, 0);
return view;
}
protected void OnCreate (Bundle savedinstancestate) {
viewpager.setadapter (new Myadapter ());
Sets the default entry for the Viewpager, set to 100 times times the total, to slide to the left at first
Viewpager.setcurrentitem (count *);
}
This practice, in fact, does not really reach the left and right infinite loops, but generally, it is difficult to reach the boundary (not sliding) situation.
By looking at a lot of data, I found that Viewpager itself does not support the function of the infinite loop, I have to say, this is really a failure.
Because Viewpager itself does not support, we can only find ways to ourselves! Here are my ways to achieve an infinite loop of left and right:
Used to display the mviews, more than the data source mlist, more than two node elements (head node 0:b and tail node 5:e for jumps)
The red line without an arrow in the figure below is the case where the mviews is initialized according to the mlist; the red thread with the arrows is the jump.
The specific code is as follows:
public class Adaptercycle extends Pageradapter implements viewpager.onpagechangelistener{private context Mconte xt Context private Layoutinflater minflater; Used to solve XML private linkedlist<view> mviews; <span style= "font-family:arial, Helvetica, Sans-serif;" > For </span><span style= "font-family:arial, Helvetica, Sans-serif;" > shows the view</span> private list<drawable> mlist; Data source <span style= "font-family:arial, Helvetica, Sans-serif;" >Drawable</span> private Viewpager Mviewpager; Page Public adaptercycle (context context, Viewpager Viewpager, list<drawable> List) {mcontext = Co
ntext
Minflater = Layoutinflater.from (context);
Mviewpager = Viewpager;
if (list!= null) {//whether or not more than 1, initialize the first (index:0) mviews = new linkedlist<view> ();
ImageView view = (ImageView) minflater.inflate (r.layout.activity_main_item_cycle, NULL); drawable drawable = LisT.get (List.size ()-1);
View.setimagedrawable (drawable);
Mviews.add (view); Note that if there are more than 1 mviews, two more than Mlist (one at a tail)//assumption: Mlist for mlist[0~n-1], mviews for mviews[0~n+1]//mviews[0], M Views[i] mlist[i-1], mviews[n+1] put mlist[0]//mviews[1~n to be used for loops; before the first mviews[0] and after the end of mviews[n+1] for the jump//First MVI Ews[0], jump to the end (N), after the final mviews[n+1], jump to the first (1) if (List.size () > 1) {//More than 1 to loop for (drawable d:list) {// The Middle N (index:1~n) ImageView v = (imageview) minflater.inflate (r.layout.activity_main_item_cycle,
NULL);
V.setimagedrawable (d);
Mviews.add (v); }//Last (index:n+1) view = (ImageView) minflater.inflate (r.layout.activity_main_item_cycle
, null);
drawable = dlist.get (0);
View.setimagedrawable (drawable);
Mviews.add (view);
@Override public int GetCount () {return mviews.size (); } @Override
public Boolean isviewfromobject (view view, Object object) {return view = = object; @Override public void Destroyitem (ViewGroup container, int position, object object) {(Viewpager) container)
. Removeview (Mviews.get (position)); @Override public Object Instantiateitem (viewgroup container, int position) {View view = Mviews.get (position)
;
Container.addview (view);
return view; }//Implement Viewpager.onpagechangelistener interface @Override public void onpageselected (int position) {Logutils.logi (T
AG, "onpageselected:" + position); if (Mviews.size () > 1) {//More than 1, the loop jumps if (position < 1) {//First, jumps to the end (N) position = Mlist.size ();
Note that this is mlist, not mviews Mviewpager.setcurrentitem (position, false);
else if (Position > Mlist.size ()) {///After the end, jump to First (1) Mviewpager.setcurrentitem (1, false);//false: Animations that do not show the jump process
Position = 1; "}}} @Override public void Onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {//Do nothing} @Override Publ IC void onpagescrollstatechanged (int state) {//Nothing}}//On the outer layer, set Mviewpager initial position to 1 if (madaptercycle.ge Tcount () > 1) {//More than 1, cycle and turn on timer mviewpager.setcurrentitem (1);//set Mviewpager initial position to 1 starttimer ();//open timer, timing Toggle Page}
At this time, the use of timers, to achieve a circular carousel, you need to modify the following:
int count = Adapter.getcount ();
if (Count > 2) {//In fact, more than 1, more than 3
int index = Viewpager.getcurrentitem ();
index = index% (count-2) + 1;
Viewpager.setcurrentitem (index, TRUE) has been modified here;