【问题标题】:ViewPager block forward on pageViewPager 在页面上阻止转发
【发布时间】:2015-08-19 22:01:49
【问题描述】:

关于如何在 ViewPager 中阻止滑动有很多问题和解决方案。

示例:Control which directions the ViewPager can be scrolled, dynamically with UI feedback

但是我只想阻止在特定页面上向前滑动 (我有一个表单和注册按钮,可以通过编程方式获取下一页)

示例: 第 1 页 -> 滑动 滑动 按钮 -> 第 4 页

尝试使用 onInterceptTouchEvent 和 onInterceptTouchEvent 方法扩展 ViewPager 类,但这样做时我无法让按钮工作,因为所有内容都被读取为 MotionEvent.ACTION_MOVE 所以不知道何时让事件通过或何时使用返回 false;

【问题讨论】:

标签: android android-layout


【解决方案1】:

import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent;

public class CustomViewPager extends ViewPager {

private boolean enabled; private boolean blockSwipe = false;

public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); this.enabled = true; }

@Override public boolean onTouchEvent(MotionEvent event) { if (this.enabled) { return super.onTouchEvent(event); }

return false; }

public void setBlockSwipe(boolean blockSwipe) { this.blockSwipe = blockSwipe; }

@Override public boolean onInterceptTouchEvent(MotionEvent event) { if (blockSwipe) return false; else return super.onInterceptTouchEvent(event); }

}

【讨论】:

  • 你添加这个类并使用 viewpager 引用...阻止滑动只是 ex viewPager.setBlockSwipe(true);
猜你喜欢
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 2012-10-11
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多