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(三种方法),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
赞 (0)