No more nonsense to say, first to everyone on the left and right infinite sliding code.
1. Left and right infinite sliding
public class Mainactivity extends Appcompatactivity {private static Viewpager Viewpager; private radiogroup Group;//Picture Capital
Source, the actual project needs to be obtained from the network private int[] Imageids = {r.drawable.ym1, r.drawable.ym2, r.drawable.ym3, r.drawable.ym4};
An array of stored pictures private list<imageview> mlist;
The current index position and the previous index position private static int index = 0, Preindex = 0;
Whether to require a carousel flag private Boolean iscontinue = true;
Timer, used to implement the carousel private Timer timer = new timer ();
Private MyHandler Mhandler; public static class MyHandler extends Handler {private weakreference<mainactivity> weakreference, public MyHandler (mainactivity activity)
{weakreference = new weakreference<> (activity);} @Override public void Handlemessage (message msg) {if (weakreference.get ()!= null) {index++; Viewpager.setcurrentitem
DEX);
Super.handlemessage (msg); }} @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Initview ();
InitData ();
AddListener (); //Let the current picture be located in the middle of the position in order to begin to be able to slide viewpager.setcurrentitem (imageids.length * 100);
Initradiobutton (imageids.length); Note that this sentence and the above order can not be written back, otherwise the first dot will not display the selected state Startswitch (); /** * Initialization control/public void Initview () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); group = (Radiogroup) findv
Iewbyid (R.id.group); /** * Initialization data */public void InitData () {mlist = new arraylist<> (); Viewpager.setadapter (pageradapter); Mhandler =
New MyHandler (this);
/** * Add listener/public void AddListener () {Viewpager.addonpagechangelistener (Onpagechangelistener);
Viewpager.setontouchlistener (Ontouchlistener); /** * do picture carousel/public void Startswitch () {//Perform timed task Timer.schedule (new TimerTask () {@Override public void run () {//First
To determine if a carousel is needed, we're sending a message if (iscontinue) {mhandler.sendemptymessage (1);}}
}, 3000, 3500);//delay 3 seconds, send messages every 3.5 seconds; /** * Based on the number of pictures to initialize the button * @param length picture is the set of the long * * private void Initradiobutton (int length) {for (int i = 0; i < length; i++) {ImageView ImageView = new ImageView (this); imageView.setimageresource (R.drawable.rg_selector)//Set Background selector imageview.setpadding (20, 0, 0, 0);//Set the spacing between each button/ Add the button to the Radiogroup Group.addview (ImageView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_
CONTENT);
The first button is selected by default because the first picture Group.getchildat (0) is displayed by default. setenabled (FALSE); }/** * Based on current touch events to determine whether to carousel/view.ontouchlistener Ontouchlistener = new View.ontouchlistener () {@Override public boolean Ontouch (View V, motionevent event) {switch (event.getaction ()) {//Stop the picture when the finger is pressed and scratched MotionEvent.ACTION_DOWN:case M
OtionEvent.ACTION_MOVE:isContinue = false;
Break
Default:iscontinue = true;
Return false;//Note that only false can be returned, if the return True,dwon will consume the event, move cannot get the event,//cause the picture cannot slide}}; /** * According to the currently selected page Settings button selected/Viewpager.onpagechangelistener Onpagechangelistener = new Viewpager.onpagechangelistener () {@ Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {} @Override public voi d onpageselected (int position) {index = position;//current position assignment to index setCurrentdot (index% imageids.length);//Because there are only four buttons, so here we want to the length area, guaranteed range of 0 to 4} @Override public void onpagescrollstatechanged (int state)
{
}
}; /** * Set the status of the corresponding position button * @param i current position */private void Setcurrentdot (int i) {if (Group.getchildat (i)!= null) {Group.getchild at (i). SetEnabled (FALSE)//The current button is selected to display the blue} if (Group.getchildat (preindex)!= null) {Group.getchildat (Preindex). SetEnabled (TRUE);//Last unchecked. Show Gray Preindex = i;//current position to previous, continue next Carousel}} pageradapter Pageradapter = new Pageradapter () {@Override public int getcount ()
{//Returns a larger value in order to implement an infinite round-broadcast return integer.max_value;} @Override public boolean isviewfromobject (view view, Object object) {return View = = object;} @Override public Object ins Tantiateitem (ViewGroup container, int position) {position = position% imageids.length;//Because the position is very large,
The position we need cannot be larger than the picture set length//So imageview ImageView = new ImageView (mainactivity.this);
Imageview.setimageresource (Imageids[position]);
Imageview.setscaletype (ImageView.ScaleType.FIT_XY); Container.addvIew (ImageView);
Mlist.add (ImageView);
return ImageView; @Override public void Destroyitem (ViewGroup container, int position, object) {//note do not do anything here, because we need to slide to the left, otherwise it will produce I
Ndexoutofboundsexception}}; @Override protected void OnDestroy () {Super.ondestroy ();//Cancel Timer if (timer!= null) {preindex = 0; Timer.cancel ()
; }
}
}
2. One-way sliding
public class Mainactivity extends Appcompatactivity {private static Viewpager Viewpager; private radiogroup Group;//Picture Capital
Source, the actual project needs to be obtained from the network private int[] Imageids = {r.drawable.ym1, r.drawable.ym2, r.drawable.ym3, r.drawable.ym4};
An array of stored pictures private list<imageview> mlist;
The current index position and the previous index position private static int index = 0, Preindex = 0;
Whether to require a carousel flag private Boolean iscontinue = true;
Timer, used to implement the carousel private Timer timer = new timer ();
Private MyHandler Mhandler; public static class MyHandler extends Handler {private weakreference<mainactivity> weakreference, public MyHandler (mainactivity activity)
{weakreference = new weakreference<> (activity);} @Override public void Handlemessage (message msg) {if (weakreference.get ()!= null) {index++; Viewpager.setcurrentitem
DEX);
Super.handlemessage (msg); }} @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Initview ();
InitData ();
AddListener (); InItradiobutton (imageids.length);
Startswitch (); /** * Initialization control/public void Initview () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); group = (Radiogroup) findv
Iewbyid (R.id.group); /** * Initialization data */public void InitData () {mlist = new arraylist<> (); Viewpager.setadapter (pageradapter); Mhandler =
New MyHandler (this);
/** * Add listener/public void AddListener () {Viewpager.addonpagechangelistener (Onpagechangelistener);
Viewpager.setontouchlistener (Ontouchlistener); /** * do picture carousel/public void Startswitch () {//Perform timed task Timer.schedule (new TimerTask () {@Override public void run () {//First
To determine if a carousel is needed, we're sending a message if (iscontinue) {mhandler.sendemptymessage (1);}}
}, 3000, 3500);//delay 3 seconds, send messages every 3.5 seconds; /** * Based on the number of pictures to initialize the button * @param length picture is the set of the long * * private void Initradiobutton (int length) {for (int i = 0; i < length;
i++) {ImageView ImageView = new ImageView (this); Imageview.setimageresource (r.drawable.rg_selector);//Set Background selector Imageview.setpadding (20, 0, 0, 0)//Set the spacing between each button//Add the button to theRadiogroup Group.addview (ImageView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
The first button is selected by default because the first picture Group.getchildat (0) is displayed by default. setenabled (FALSE); }/** * Based on current touch events to determine whether to carousel/view.ontouchlistener Ontouchlistener = new View.ontouchlistener () {@Override public boolean Ontouch (View V, motionevent event) {switch (event.getaction ()) {//Stop the picture when the finger is pressed and scratched MotionEvent.ACTION_DOWN:case M
OtionEvent.ACTION_MOVE:isContinue = false;
Break
Default:iscontinue = true;
Return false;//Note that only false can be returned, if the return True,dwon will consume the event, move cannot get the event,//cause the picture cannot slide}}; /** * According to the currently selected page Settings button selected/Viewpager.onpagechangelistener Onpagechangelistener = new Viewpager.onpagechangelistener () {@ Override public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {} @Override public voi d onpageselected (int position) {index = position;//current position assignment to index Setcurrentdot (index% imageids.length);//Because there are only four buttons, So we're here to the length area, guaranteed range 0 to 4} @Override public void OnpagescrollstatechanGED (int state) {}}; /** * Set the status of the corresponding position button * @param i current position */private void Setcurrentdot (int i) {if (Group.getchildat (i)!= null) {Group.getchild at (i). SetEnabled (FALSE)//The current button is selected to display the blue} if (Group.getchildat (preindex)!= null) {Group.getchildat (Preindex). SetEnabled (TRUE);//Last unchecked. Show Gray Preindex = i;//current position to previous, continue next Carousel}} pageradapter Pageradapter = new Pageradapter () {@Override public int getcount ()
{//Returns a larger value in order to implement an infinite round-broadcast return integer.max_value;} @Override public boolean isviewfromobject (view view, Object object) {return View = = object;} @Override public Object ins Tantiateitem (ViewGroup container, int position) {position = position% imageids.length;//Because the position is very large,
The position we need cannot be larger than the picture set length//So imageview ImageView = new ImageView (mainactivity.this);
Imageview.setimageresource (Imageids[position]);
Imageview.setscaletype (ImageView.ScaleType.FIT_XY);
Container.addview (ImageView);
Mlist.add (ImageView);
return ImageView; @Override public void Destroyitem (ViewGroup container, INT position, object) {Container.removeview (Mlist.get (position));}; @Override protected void OnDestroy () {Super.ondestroy ();//Cancel Timer if (timer!= null) {preindex = 0; index = 0; Tim
Er.cancel (); }
}
}
3. Add a picture to the Judgment (final version)
public class Mainactivity extends Appcompatactivity {private static Viewpager Viewpager; private radiogroup Group;//Picture Capital
Source, the actual project needs to be obtained from the network//private int[] Imageids = {r.drawable.ym1, r.drawable.ym2, r.drawable.ym3, r.drawable.ym4};
Private int[] Imageids = {R.DRAWABLE.YM1};
An array of stored pictures private list<imageview> mlist;
The current index position and the previous index position private static int index = 0, Preindex = 0;
Whether to require a carousel flag private Boolean iscontinue = true;
Timer, used to implement the carousel private Timer timer = new timer ();
Private MyHandler Mhandler; public static class MyHandler extends Handler {private weakreference<mainactivity> weakreference, public MyHandler (mainactivity activity)
{weakreference = new weakreference<> (activity);} @Override public void Handlemessage (message msg) {if (weakreference.get ()!= null) {index++; Viewpager.setcurrentitem
DEX);
Super.handlemessage (msg); }} @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);
Initview ();
InitData ();
AddListener ();
Initradiobutton (imageids.length); Note that this sentence and the above order can not be written back, otherwise the first dot will not display the selected state Startswitch (); /** * Initialization control/public void Initview () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); group = (Radiogroup) findv
Iewbyid (R.id.group); /** * Initialization data */public void InitData () {mlist = new arraylist<> (); Viewpager.setadapter (pageradapter); Mhandler =
New MyHandler (this);
/** * Add listener/public void AddListener () {Viewpager.addonpagechangelistener (Onpagechangelistener);
Viewpager.setontouchlistener (Ontouchlistener); /** * do picture carousel/public void Startswitch () {//Perform timed task Timer.schedule (new TimerTask () {@Override public void run () {//First
To determine if a carousel is needed, we're sending a message if (iscontinue) {if (imageids.length!=1)//More than one picture is Carousel mhandler.sendemptymessage (1);}
}, 3000, 3500);//delay 3 seconds, send messages every 3.5 seconds; /** * Based on the number of pictures to initialize the button * @param length picture is the set of the long * * private void Initradiobutton (int length) {for (int i = 0; i < length; i++) {ImageView ImageView = new ImageView (this); if (length = = 1) {imageview.setvisibility (view.gone); return;} imageview.setimageresource (R.drawable.rg_selector); Set the background selector imageview.setpadding (20, 0, 0, 0);//Set the spacing between each button//Add the button to the Radiogroup Group.addview (ImageView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
The first button is selected by default because the first picture Group.getchildat (0) is displayed by default. setenabled (FALSE); }/** * Based on current touch events to determine whether to carousel/view.ontouchlistener Ontouchlistener = new View.ontouchlistener () {@Override public boolean Ontouch (View V, motionevent event) {switch (event.getaction ()) {//Stop the picture when the finger is pressed and scratched MotionEvent.ACTION_DOWN:case M
OtionEvent.ACTION_MOVE:isContinue = false;
Break
Default:iscontinue = true; } if (imageids.length = = 1) {return TRUE;//1 picture does not allow sliding} returns false;//Note that only false can be returned, if the return True,dwon will consume the event, move cannot get the event,//
Cause the picture cannot be slid}}; /** * According to the currently selected page Settings button selected/Viewpager.onpagechangelistener Onpagechangelistener = new Viewpager.onpagechangelistener () {@ Override public void onpagescrolled (int position, float Positionoffset, int positionoffsetpixels) {} @Override public void onpageselected (int position) {index = position;//current position assigned to index Setcurre Ntdot (index% imageids.length);//Because there are only four buttons, so here we want to the length area, guaranteed range of 0 to 4} @Override public void onpagescrollstatechanged (int
State) {}}; /** * Set the status of the corresponding position button * @param i current position */private void Setcurrentdot (int i) {if (Group.getchildat (i)!= null) {Group.getchild at (i). SetEnabled (FALSE)//The current button is selected to display the blue} if (Group.getchildat (preindex)!= null) {Group.getchildat (Preindex). SetEnabled (TRUE);//Last unchecked. Show Gray Preindex = i;//current position to previous, continue next Carousel}} pageradapter Pageradapter = new Pageradapter () {@Override public int getcount ()
{//Returns a larger value in order to implement an infinite round-broadcast return integer.max_value;} @Override public boolean isviewfromobject (view view, Object object) {return View = = object;} @Override public Object ins Tantiateitem (ViewGroup container, int position) {position = position% imageids.length;//Because the position is very large, And the position we need cannot be larger than the picture set length//So imageview ImageView = new ImageView (mainactivity.this);
Imageview.setimageresource (Imageids[position]);
Imageview.setscaletype (ImageView.ScaleType.FIT_XY);
Container.addview (ImageView);
Mlist.add (ImageView);
return ImageView; @Override public void Destroyitem (ViewGroup container, int position, object) {//note do not do anything here, because we need to slide to the left, otherwise it will produce I
Ndexoutofboundsexception Container.removeview (mlist.get (position));
}
}; @Override protected void OnDestroy () {Super.ondestroy ();//Cancel Timer if (timer!= null) {preindex = 0; index = 0; Tim
Er.cancel (); }
}
}
The above is a small set to introduce the Android image automatic carousel and support gestures around infinite sliding, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!