一侧 ViewPager 仅滑动

One side ViewPager swiping only(一侧 ViewPager 仅滑动)
本文介绍了一侧 ViewPager 仅滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 ViewPager,我想随时禁用/允许向右滑动.viewpager 中的每个视图都包含 ListView.我怎样才能做到这一点?我正在尝试以这种方式做到这一点:

I have a ViewPager in my application and I would like to disable/allow swipe to right side in any time. Every view in the viewpager contains ListView. How can I do that? I am trying to do that in this way:

private int oldX;
private int deltaX = 0;

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_MOVE || motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        int newX = (int) motionEvent.getX();
        deltaX = oldX - newX;
        oldX = newX;
    }
    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        oldX = 0;
    }
    return deltaX < 0;        
}

但是视图仍然会向右一点.有人在解决同样的问题吗?

But view is still going to right side a little bit. Anyone was solving the same issue?

推荐答案

通过将 ViewPager 的代码放入我的应用程序并添加以下内容,我能够在 ViewPager 中实现单侧滚动

I was able to achieve one-side scrolling in ViewPager by taking its code to my application and adding the following

private boolean performDrag(float x) {
    boolean needsInvalidate = false;

    final float deltaX = mLastMotionX - x;
    mLastMotionX = x;

    // MY CODE STARTS
    if (deltaX < 0) {
        return false;
    }
    // MY CODE ENDS

    float oldScrollX = getScrollX();
    ...

它似乎工作正常.您可能需要的唯一东西 - 获取适配器代码或提供您自己的适配器,因为 ViewPager 在其代码中使用的某些方法不是从 PagerAdapter 公开的.

It seems to work fine. The only thing You might need - take adapter code or provide Your own adapter, because some methods ViewPager uses in its code are not public from PagerAdapter.

这篇关于一侧 ViewPager 仅滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)