一侧 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 仅滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:一侧 ViewPager 仅滑动

基础教程推荐