Android新特性页面之ViewPager拖拽到最后一页再拖拽打开其他Activity(三种方法)
程序员文章站
2024-03-05 13:29:30
android新特性页面,viewpager拖拽到最后一页再拖拽打开其他activity.实现的方式有很多,效果比较好的就是到了最后一页再拖拽出现禁止蓝色条时再...
android新特性页面,viewpager拖拽到最后一页再拖拽打开其他activity.实现的方式有很多,效果比较好的就是到了最后一页再拖拽出现禁止蓝色条时再跳转activity
方式一:拿到viewpager的边界条edgeeffectcompat,判断是否到了边界(获取edgeeffectcompat通过反射,方法来自网络最后边会给出原文)
/** * 初始化view */ private void initview(){ pager = (viewpager) findviewbyid(r.id.help_viewpages); try { field leftedgefield = pager.getclass().getdeclaredfield("mleftedge"); field rightedgefield = pager.getclass().getdeclaredfield("mrightedge"); if (leftedgefield != null && rightedgefield != null) { leftedgefield.setaccessible(true); rightedgefield.setaccessible(true); leftedge = (edgeeffectcompat) leftedgefield.get(pager); rightedge = (edgeeffectcompat) rightedgefield.get(pager); } } catch (exception e) { e.printstacktrace(); } pager.setonpagechangelistener(new onpagechangelistener() { @override public void onpagescrollstatechanged(int arg0) { if(rightedge!=null&&!rightedge.isfinished()){//到了最后一张并且还继续拖动,出现蓝色限制边条了 startactivity(new intent(helpactivity.this, mainactivity.class)); helpactivity.this.finish(); } } @override public void onpageselected(int arg0) { } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } });
方法二:也是在onpagechangelistener里判断当前滚动的页数是不是最后一页
pager.setonpagechangelistener(new onpagechangelistener() { @override public void onpageselected(int arg0) { } @override public void onpagescrolled(int arg0, float arg1, int arg2) { } @override public void onpagescrollstatechanged(int arg0) { //判断当前页数是否==总页数 } });
在此小编推荐方法一,方法二需要判断pagescrollstatechanged(int arg0)的滚动状态,也可以实现一样的效果
转载高手viewpager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法(以备自己以后查阅)
package com.example.viewpagertest; import java.lang.reflect.field; import java.util.arraylist; import java.util.list; import android.app.activity; import android.os.bundle; import android.support.v4.view.pageradapter; import android.support.v4.view.viewpager; import android.support.v4.view.viewpager.onpagechangelistener; import android.support.v4.widget.edgeeffectcompat; import android.view.view; import android.view.viewgroup; import android.widget.imageview; public class mainactivity extends activity implements onpagechangelistener { private viewpager viewpager; private list<imageview> views; private edgeeffectcompat leftedge; private edgeeffectcompat rightedge; private int[] ids = {r.drawable.iframe_4,r.drawable.iframe_5,r.drawable.iframe_6,r.drawable.iframe_7,r.drawable.iframe_8}; private myadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); } private void initview() { viewpager = (viewpager) findviewbyid(r.id.viewpager); initviewpager(); initdata(); } private void initdata() { views = new arraylist<imageview>(); for(int i=0;i<ids.length;i++){ imageview iv = new imageview(this); iv.setimageresource(ids[i]); views.add(iv); } adapter = new myadapter(); viewpager.setadapter(adapter); viewpager.setonpagechangelistener(this); } class myadapter extends pageradapter{ @override public int getcount() { return views.size(); } @override public boolean isviewfromobject(view arg0, object arg1) { return arg0==arg1; } @override public void destroyitem(viewgroup container, int position, object object) { ((viewpager)container).removeview(views.get(position)); } @override public object instantiateitem(viewgroup container, int position) { ((viewpager)container).addview(views.get(position)); return views.get(position); } } @override public void onpagescrollstatechanged(int arg0) { } @override public void onpagescrolled(int arg0, float arg1, int arg2) { if (leftedge != null && rightedge != null) { leftedge.finish(); rightedge.finish(); leftedge.setsize(0, 0); rightedge.setsize(0, 0); } } @override public void onpageselected(int arg0) { } private void initviewpager() { try { field leftedgefield = viewpager.getclass().getdeclaredfield("mleftedge"); field rightedgefield = viewpager.getclass().getdeclaredfield("mrightedge"); if (leftedgefield != null && rightedgefield != null) { leftedgefield.setaccessible(true); rightedgefield.setaccessible(true); leftedge = (edgeeffectcompat) leftedgefield.get(viewpager); rightedge = (edgeeffectcompat) rightedgefield.get(viewpager); } } catch (exception e) { e.printstacktrace(); } } }
以上所述是小编给大家介绍的android新特性页面之viewpager拖拽到最后一页再拖拽打开其他activity(三种方法),希望对大家有所帮助
上一篇: aspx中页面按钮写返回上一页代码
下一篇: Android实现滑动到顶部悬停的效果