Swipe Direction in ViewPager(在 ViewPager 中滑动方向)
问题描述
我有一个使用 ViewPager 和 FragmentStatePagerAdapter 的 Android 应用.然而,我需要添加一个新功能,我必须知道滑动方向才能从一个片段视图移动到另一个片段视图,向左或向右移动.(即,当我向左滑动时,我会做一件事,当我向右滑动时,我会做其他事情).
I have an Android app that uses ViewPager and FragmentStatePagerAdapter. I however need to add a new feature in which I have to know the swipe direction to move from one fragment view to another, left or right. (i.e, when I swipe left, I would do one thing, and when I swipe right I would do something else).
请注意,我不需要在滑动结束后发生事件.当滑动发生时,我必须做这些事件.我想过使用 setOnPageChangeListener()
但它并没有告诉我滑动方向.你能告诉我如何确定滑动方向吗?
Please note, I do not need the events to happen AFTER the swipe is over. I have to do those events as the swipe occurs. I thought of using setOnPageChangeListener()
but it does not tell me swipe direction. Can you advise me please on how to figure out swipe direction?
谢谢.
推荐答案
我遇到了这个问题,需要知道滑动的方向因为它正在发生,以防止在某个特定位置滑动方向.这是我解决问题的方法,希望对某人有所帮助.我发现前面的所有示例都无法满足我的需要.
I ran into this issue and need to know the direction of the swipe as it was happening in order to prevent swiping in a certain direction. Here is how I solved my problem, hopefully it is helpful to someone. I found all of the previous examples inefficient for what I needed.
如果您将 ViewPager 子类化,您可以响应 ViewPager
的 onInterceptTouchEvent(MotionEvent event)
和 onTouchEvent(MotionEvent event)
方法
If you subclass your ViewPager, you can respond to the onInterceptTouchEvent(MotionEvent event)
and onTouchEvent(MotionEvent event)
methods of the ViewPager
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean wasSwipeToRight = this.wasSwipeToRightEvent(event));
// Do what you want here with left/right swipe
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean wasSwipeToRight = this.wasSwipeToRightEvent(event));
// Do what you want here with left/right swipe
return super.onTouchEvent(event);
}
这里是用来确定事件方向的方法.
Here is the method which is called to determine the direction of the event.
// Stores the starting X position of the ACTION_DOWN event
float downX;
/**
* Checks the X position value of the event and compares it to
* previous MotionEvents. Returns a true/false value based on if the
* event was an swipe to the right or a swipe to the left.
*
* @param event - Motion Event triggered by the ViewPager
* @return - True if the swipe was from left to right. False otherwise
*/
private boolean wasSwipeToRightEvent(MotionEvent event){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
return false;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
return downX - event.getX() > 0;
default:
return false;
}
}
这篇关于在 ViewPager 中滑动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 ViewPager 中滑动方向
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- UITextView 内容插图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01