Android viewpager在最后一页滑动之后跳转到主页面的实例代码
程序员文章站
2024-03-05 14:09:12
先给大家说下实现思路
主要有是两个监听:
一是addonpagechangelistener();二是setontouchlistener();
addonp...
先给大家说下实现思路
主要有是两个监听:
一是addonpagechangelistener();二是setontouchlistener();
addonpagechangelistener()主要是为了获取position(滑动到了第几页)
setontouchlistener()主要是判断在最后一页中是否向左滑动了,然后进入主页
在没给大家分享代码之前,先给大家展示下效果图:
主要功能代码
addonpagechangelistener(); 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","监听改变"+position); } @override public void onpagescrollstatechanged(int state) { } }); setontouchlistener(); 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); //获取屏幕的宽度 point size = new point(); windowmanager.getdefaultdisplay().getsize(size); int width=size.x; //首先要确定的是,是否到了最后一页,然后判断是否向左滑动,并且滑动距离是否符合,我这里的判断距离是屏幕宽度的4分之一(这里可以适当控制) if(currentitem==(imageviews.size()-1)&&startx-endx>=(width/4)){ log.i(log,"进入了触摸"); gotomainactivity();//进入主页 overridependingtransition(r.anim.slide_in_right,r.anim.slide_in_left);//这部分代码是切换activity时的动画,看起来就不会很生硬 } break; } return false; } });
以下是全部代码
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); //初始化引导数据 initguidedata(); //初始化引导页 initguideview(); //初始化分页控件 iniview(); } /** * 初始化引导页数据 */ private void initguidedata() { imageidlist = new arraylist(); imageidlist.add(r.mipmap.apk_img1); imageidlist.add(r.mipmap.apk_img2); imageidlist.add(r.mipmap.apk_img3); } /** * 初始化引导页 */ private void initguideview() { imageviews = new arraylist<>(); for (int i = 0; i < imageidlist.size(); i++) { imageviews.add(new imageview(this)); } } /** * 初始化分页控件 */ 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","监听改变"+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); //获取屏幕的宽度 point size = new point(); windowmanager.getdefaultdisplay().getsize(size); int width=size.x; //首先要确定的是,是否到了最后一页,然后判断是否向左滑动,并且滑动距离是否符合,我这里的判断距离是屏幕宽度的4分之一(这里可以适当控制) if(currentitem==(imageviews.size()-1)&&startx-endx>0&&startx-endx>=(width/4)){ log.i(log,"进入了触摸"); 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适配器 */ private class guideadapter extends pageradapter { @override public int getcount() { return imageviews.size(); } /** * 判断当前分页是不是view * 由于viewpager里面的分页可以填入fragment * * @param view * @param object * @return */ @override public boolean isviewfromobject(view view, object object) { return view == object; } /** * 清理内存 * 从第一页滑动到第二页,此时第一页的内存应该释放 * * @param container * @param position * @param object */ @override public void destroyitem(viewgroup container, int position, object object) { container.removeview(imageviews.get(position));//释放滑动过后的前一页 } /** * 得到---->暂时是没有用的 * * @param object * @return */ @override public int getitemposition(object object) { return super.getitemposition(object); } /** * 初始化分页 * * @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);//设置图片的宽高 return imageview; } } }
以下为动画资源代码
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>
以上所述是小编给大家介绍的android viewpager在最后一页滑动之后跳转到主页面的实例代码,希望对大家有所帮助