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 中滑动方向
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
