Viewpager gesture Toggle Page + picture left and right Carousel effect (Lite version)

Source: Internet
Author: User

is: gestures can be left and right to slide the interface, and automatically from the beginning of the first carousel to the last picture, and then start from the last picture to the opposite direction of the first carousel

If you need a carousel effect, (I'm a lite version) if you need gestures to switch pictures back and forth if you don't need the UI to provide positioning dots then this blog is worth looking at

Source address will not be transmitted, the code has been posted, you can just put 5 pictures to

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "com.example.welcomeactivity.MainActivity" >    <android.support.v4.view.viewpager        android:id= "@+id/viewpager"        android:layout_width= "fill_parent"        android:layout_height= "fill_parent ">    </android.support.v4.view.ViewPager>    <linearlayout        android:id=" @+id/ll_points "        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:layout_ Alignparentbottom= "true"        android:layout_centerhorizontal= "true"        android:orientation= "Horizontal" >    </LinearLayout></RelativeLayout>

Mainactivity

Package Com.example.welcomeactivity;import Java.util.arraylist;import Android. R.integer;import android.app.activity;import android.os.bundle;import Android.os.handler;import android.os.Message ; Import Android.os.systemclock;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.view;import Android.view.viewgroup;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.linearlayout.layoutparams;public class MainActivity extends Activity implements Onpagechangelistener {private Viewpager viewpager;private linearlayout ll_points;private ArrayList <ImageView> imageviews;private int[] imageviewids;private int previousselectposition = 0;private int lunbopos = 0;/ /whether to start auto-play private Boolean toright = true;//to the left to start autoplay private Boolean toleft = false;private Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {Super.handleMessage (msg); if (Lunbopos <= 4) {Viewpager.setcurrentitem (Lunbopos); if (Lunbopos = = 4) {toright = False;toleft = true;} if (Lunbopos = = 0) {ToRight = True;toleft = false;}}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview (); Initpics (); Ll_points.getchildat (previousselectposition). SetEnabled (True); Viewpager.setadapter (New Viewpageradapter ()); Viewpager.setonpagechangelistener (this);//auto-switch page function new Thread (new Runnable () {@Overridepublic void run () {while (true) {if (toright) {systemclock.sleep (); Handler.sendemptymessage (0 ); lunbopos++;} if (toleft) {systemclock.sleep (+); handler.sendemptymessage (0); lunbopos--;}}}). Start ();} private void Initpics () {for (int i = 0; i < imageviewids.length; i++) {ImageView ImageView = new ImageView (this); image View.setbackgroundresource (Imageviewids[i]); Imageviews.add (ImageView); View view = new view (this); View.setbackgrounddrawable (Getresources (). getdrawabLe (R.drawable.point_background)); Layoutparams LP = new Layoutparams, Lp.leftmargin = 10;VIEW.SETLAYOUTPARAMS (LP); view.setenabled (false); Ll_ Points.addview (view);}} private void Initview () {Viewpager = (Viewpager) Findviewbyid (r.id.viewpager); ll_points = (linearlayout) Findviewbyid ( r.id.ll_points); imageviews = new arraylist<imageview> (); imageviewids = new int[] {r.drawable.bg1, R.drawable.bg2 , R.drawable.bg3, r.drawable.pic_01, r.drawable.pic_02};} Class Viewpageradapter extends Pageradapter {@Overridepublic int getcount () {return imageviews.size ();} /** * Determine if the view is equal to come in view if true directly multiplexing */@Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = Arg1;} /** * Destroys a View object other than preload, passing in the index position of the object to be destroyed is position */@Overridepublic void Destroyitem (ViewGroup container, int position, Object object) {Container.removeview (Imageviews.get (position));} /** * Create a view */@Overridepublic Object Instantiateitem (viewgroup container, int position) {Container.addview ( Imageviews.gET (position)); return imageviews.get (position);}} @Overridepublic void onpagescrollstatechanged (int position) {} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {} @Overridepublic void onpageselected (int position) {Ll_points.getchildat (previousselectposition). SetEnabled (FALSE); Place the previous point as normal state ll_points.getchildat (position). SetEnabled (True); Set the point of the currently selected position to the enabled state previousselectposition = position;}}

In addition, the dot is an XML implementation, not a picture form

Point_background.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:drawable=" @drawable/point_enable "android:state_enabled=" true "></item >    <item android:drawable= "@drawable/point_normal" android:state_enabled= "false" ></item></ Selector>

Point_enable.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Oval" >        <corners android:radius= "5dip"/>    <solid android:color= "#aaFFFFFF" /></shape>
Point_normal.xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Oval" >        <corners android:radius= "5dip"/>    <solid android:color= "#55000000" /></shape>


Viewpager gesture Toggle Page + picture left and right Carousel effect (Lite version)

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.