Control which directions the ViewPager can be scrolled, dynamically with UI feedback(使用 UI 反馈动态控制 ViewPager 可以滚动的方向)
问题描述
我正在开发一个使用许多页面滑动的应用程序,但是在某些页面上,我希望能够防止滚动到下一页,直到他们选择了某些内容.
I'm working on an app which uses many pages to swipe through, however at some pages I would like to be able to prevent scrolling to the next page until they've selected something.
目前我已经扩展了 ViewPager:
At the moment I've extended ViewPager:
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager#onInterceptTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.canGoLeft || this.canGoRight) {
return super.onInterceptTouchEvent(event);
}
return false;
}
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
boolean lockScroll = false;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = ev.getX();
lockScroll = false;
return super.onTouchEvent(ev);
case MotionEvent.ACTION_MOVE:
if (canGoLeft && lastX < ev.getX()) {
lockScroll = false;
} else if (canGoRight && lastX > ev.getX()) {
lockScroll = false;
} else {
lockScroll = true;
}
lastX = ev.getX();
break;
}
lastX = ev.getX();
if (lockScroll) {
//make callback to prevent movement
if (mOnPreventCallback != null) {
mOnPreventCallback.onPreventMovement();
}
return false;
} else {
return super.onTouchEvent(ev);
}
}
注意:我已经尝试实现防止滑动到上一页,但是您仍然可以滑动到上一页,但是您需要从最左侧滑动并向右滑动一点.很喜怒无常,但不妨碍完全刷卡.
Note: I've tried to implement prevent swiping to previous however you can still swipe to the previous page but you need to swipe from the uttermost left side and swipe right a bit. It's temperamental but it does not prevent swiping completely.
我想在这里做三件事:
- 在他们选择项目或其他内容之前,不要滚动到下一个屏幕
- 防止滚动到上一个屏幕(部分实现)
- 向用户提供反馈以表明滚动已被禁用,就像 Android 以渐变方式显示顶部和底部屏幕的方式一样.
推荐答案
当我遇到类似的问题时,我用一个gestureDetector做了我自己的Listener,让它完全不响应滑动,你可以很容易地调整它不响应仅在某些情况下向右滑动.这应该回答您的数字 1 和 2.
When I faced a similar problem, I made my own Listener with a gestureDetector and made it not respond to swiping at all, you can easily adjust it to not respond to swiping to the right only under some condition. This should answer your numbers 1 and 2.
这是监听器:
public class OnSwipeTouchListener implements OnTouchListener {
@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View v, final MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
onTouch(e);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
// onTouch(e);
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onTouch(MotionEvent e) {
}
public void onSwipeRight() {
//call this only if your condition was set
}
public void onSwipeLeft() {
//nothing, this means,swipes to left will be ignored
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
这是一个使用示例:
viewPager.setOnTouchListener(new OnSwipeTouchListener() {
public void onSwipeRight() {
if(your conditionIsMet){
// move your pager view to the right
}
}
});
可能有一个更简单、更优雅的解决方案,但这对我来说效果很好.
There might be a simpler and more elegant solution, but this worked well for me.
这篇关于使用 UI 反馈动态控制 ViewPager 可以滚动的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 UI 反馈动态控制 ViewPager 可以滚动的方向
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01