Home>

Let me tell you about the realization

There are two main listeners:

One is addonpagechangelistener ();the other is setontouchlistener ();

addonpagechangelistener () is mainly to get the position (slide to the first few pages)

setontouchlistener () is mainly used to determine whether to slide to the left in the last page.Then go to homepage

Before I share the code with you,Let me show you the effect picture first:

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) {
currentitem=position;//Get the position,Which page
log.i ("guide", "Listen for changes" + 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 motionevent.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);
//Get the width of the screen
point size=new point ();
windowmanager.getdefaultdisplay (). getsize (size);
int width=size.x;
//The first thing to make sure is,Is it the last page,Then determine whether to swipe left,And whether the sliding distance matches,My judgment distance here is a quarter of the screen width (you can control it properly here)
if (currentitem == (imageviews.size ()-1)&&startx-endx>= (width/4)) {
log.i (log, "Entered touch");
gotomainactivity ();//Enter the homepage
overridependingtransition (r.anim.slide_in_right, r.anim.slide_in_left);//This part of the code is an animation when switching activities,It doesn't look stiff
}
break;
}
return false;
}
});

Here is the full 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 boot data
initguidedata ();
//Initialize the boot page
initguideview ();
//Initialize the paging control
iniview ();
}
/**
* Initialize the boot page data
* /
private void initguidedata () {
imageidlist=new arraylist ();
imageidlist.add (r.mipmap.apk_img1);
imageidlist.add (r.mipmap.apk_img2);
imageidlist.add (r.mipmap.apk_img3);
}
/**
* Initialize the boot page
* /
private void initguideview () {
imageviews=new arraylist<>();
for (int i=0;i<imageidlist.size ();i ++) {
imageviews.add (new imageview (this));
}
}
/**
* Initialize the 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) {
currentitem=position;
log.i ("guide", "Listen for changes" + position);
}
@override
public void onpagescrollstatechanged (int state) {
}
});
viewpager.setontouchlistener (new view.ontouchlistener () {
float startx;
float starty;
float endx;
float endy;
@override
public boolean ontouch (view v, motionevent event) {
switch (event.getaction ()) {
case motionevent.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);
//Get the width of the screen
point size=new point ();
windowmanager.getdefaultdisplay (). getsize (size);
int width=size.x;
//The first thing to make sure is,Is it the last page,Then determine whether to swipe left,And whether the sliding distance matches,My judgment distance here is a quarter of the screen width (you can control it properly here)
if (currentitem == (imageviews.size ()-1)&&startx-endx>0&&startx-endx>= (width/4)) {
log.i (log, "Entered 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 ();
}
/**
* Determine if the current pagination is view
* Since the pagination in the viewpager can be filled into the fragment
*
* @param view
* @param object
* @return
* /
@override
public boolean isviewfromobject (view view, object object) {
return view == object;
}
/**
* Clear memory
* Swipe from the first page to the second page,At this point 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 the previous page after sliding
}
/**
* Get ---->temporarily useless
*
* @param object
* @return
* /
@override
public int getitemposition (object object) {
return super.getitemposition (object);
}
/**
* Initialize paging
*
* @param container
* @param position
* @return
* /
@override
public object instantiateitem (viewgroup container, 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 width and height of the image
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>
  • Previous jQuery imitation Jingdong Mall staircase navigation positioning menu
  • Next PHP integrated dynamic password authentication