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 中滑动方向
				
        
 
            
        基础教程推荐
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				