Android Viewpager An instance code that jumps to the main page after the last slide _android

Source: Internet
Author: User
Tags gety object object

First of all, to give you the idea of realization

There are two main listening:

One is Addonpagechangelistener (); second is Setontouchlistener ();

Addonpagechangelistener () is mainly to obtain the position (slide to the first few pages)

Setontouchlistener () is mainly to determine whether the last page of the slide left, and then enter the home page

Before we share the code, let's show you the effect chart:

Main function code

Addonpagechangelistener (); Viewpager.addonpagechangelistener (New Viewpager.onpagechangelistener () {@Override public void onpagescrolled (int Position, float positionoffset, int positionoffsetpixels) {} @Override public void onpageselected (int position) {current
Item = position;//Get position, that is, page log.i ("Guide", "Monitor Change" +position); 
@Override public void onpagescrollstatechanged (int state) {}});
Setontouchlistener (); Viewpager.setontouchlistener (new View.ontouchlistener () {float startx; float starty;//not used
float EndX; Float endy;//not used @Override public boolean ontouch (View V, motionevent event) {switch (event.getaction ()) {case Motioneve Nt.
Action_down:startx=event.getx ();
Starty=event.gety ();
Break
Case Motionevent.action_up:endx=event.getx ();
Endy=event.gety ();
WindowManager windowmanager= (WindowManager) Getapplicationcontext (). Getsystemservice (Context.WINDOW_SERVICE);
Gets the width point size of the screen = new Point ();
Windowmanager.getdefaultdisplay (). GetSize (size);
int width=size.x; The first thing to be sure is whether it'sThe last page, and then determine whether to slide to the left, and whether the sliding distance matches, I am here to judge the distance is one of the screen width of 4 (here can be properly controlled) if (currentitem== (Imageviews.size ()-1) && Startx-endx>= (WIDTH/4)) {log.i (Log, "enter Touch"); Gotomainactivity ()//Enter home page overridependingtransition (r.anim.slide_
In_right,r.anim.slide_in_left);//This part of the code is to switch activity when the animation, looks will not be very blunt} break;
return false; }
});

The following is all code

Guideactivity package com.tc.mobileshop;
Import Android.content.Context;
Import android.content.Intent;
Import Android.graphics.Point;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Display;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.WindowManager;
Import Android.widget.ImageView;
Import Com.tc.mobileshop.utils.DisplayUtils;
Import java.util.ArrayList;
Import java.util.List;
public class Guideactivity extends Appcompatactivity {private static final String LOG = "guideactivity"; int touchcount;
int CurrentItem;
List<integer> imageidlist;
List<imageview> imageviews;
Viewpager Viewpager; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_guide);
Initialize the bootstrap data initguidedata ();Initialize the boot page initguideview ();
Initializes a paging control iniview (); /** * Initialize boot page data/private void Initguidedata () {imageidlist = new ArrayList (); Imageidlist.add (R.MIPMAP.APK_IMG1); imag
Eidlist.add (R.MIPMAP.APK_IMG2);
Imageidlist.add (R.MIPMAP.APK_IMG3); /** * Initialize boot page/private void Initguideview () {imageviews = new arraylist<> (); for (int i = 0; i < imageidlist. Size ();
i++) {Imageviews.add (new ImageView (This));}  /** * Initialize paging control/private void Iniview () {Viewpager = (Viewpager) Findviewbyid (R.id.guide_pager); Viewpager.setadapter (new
Guideadapter ()); Viewpager.addonpagechangelistener (New Viewpager.onpagechangelistener () {@Override public void onpagescrolled (int Position, float positionoffset, int positionoffsetpixels) {} @Override public void onpageselected (int position) {current
Item = position;
LOG.I ("Guide", "Monitor Change" +position);
@Override public void onpagescrollstatechanged (int state) {}}); Viewpager.setontouchlistener (New View.ontouchlistener () {float startx; float starty; floatEndX;
float EndY; @Override public boolean Ontouch (View v., motionevent event) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:st
Artx=event.getx ();
Starty=event.gety ();
Break
Case Motionevent.action_up:endx=event.getx ();
Endy=event.gety ();
WindowManager windowmanager= (WindowManager) Getapplicationcontext (). Getsystemservice (Context.WINDOW_SERVICE);
Gets the width point size of the screen = new Point ();
Windowmanager.getdefaultdisplay (). GetSize (size);
int width=size.x; The first thing to determine is whether to the last page, and then to determine whether to slide to the left, and whether the sliding distance is consistent, I am here to judge the distance is one of the screen width of 4 points (here can be properly controlled) if (currentitem== (Imageviews.size ()-1)
&&startx-endx>0&&startx-endx>= (WIDTH/4)) {log.i (Log, "entered the Touch"); gotomainactivity ();
Overridependingtransition (R.anim.slide_in_right,r.anim.slide_in_left);
} break;
return false;
}
}); 
private void Gotomainactivity () {Intent intent=new Intent (this,mainactivity.class); startactivity (Intent); Finish ();} /** * Viewpager Adapter */Private class Guideadapter extends Pageradapter {@Override public int GetcoUnt () {return imageviews.size ();}/** * To determine if the current paging is view * Because the Viewpager inside the pagination can be filled in fragment * * @param view * @param object * @ Return/@Override public boolean isviewfromobject (view view, Object object) {return View = = object;}/** * Clean memory * from the first page Slide to the second page, at which time the first page of memory should be released * * @param container * @param position * @param object/@Override public void Destroyitem (ViewGroup container, int position, object object) {Container.removeview (Imageviews.get (position))//Release previous page after sliding (/** * get----> No use for the time being * * @param object * @return/@Override public int GetItemPosition (object) {return super.getitemposition (
object); /** * Initialize Paging * * @param container * @param position * @return * * @Override public Object instantiateitem (ViewGroup contai NER, int position) {ImageView ImageView = imageviews.get (position); Imageview.setimageresource (Imageidlist.get (
position));
Viewgroup.layoutparams viewlayoutparams = new Viewgroup.layoutparams (displayutils.dip2px (GuideActivity.this, 170), DISPLAYUTILS.DIP2PX (Guideactivity.this, 200));
Container.addview (Imageview,viewlayoutparams)//Set the picture's width high return imageview; }
}
}

The following is the animation resource code

Slide_in_left.xml

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<translate
android:duration=" 1000 "
android:fromxdelta=" 0%p "
android:toxdelta=" -100% "
/>
</set> 
slide_in_right.xml
<?xml version="
1.0 "encoding=" Utf-8 "?>" <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<translate
android:duration = "1000"
android:fromxdelta= "100%p"
android:toxdelta= "0"
/>
</set>

The above is a small set to introduce the Android Viewpager in the last page after sliding to the main page of the example code, 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!

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.