只有一边ViewPager刷卡 [英] One side ViewPager swiping only

查看:120
本文介绍了只有一边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?

推荐答案

我是能够采取的code到我的应用程序,并增加了实现一侧滚动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();
    ...

这似乎很好地工作。唯一可能需要 - 需要适配器code,或提供自己的适配器,因为一些方法 ViewPager 使用其code是不公开的 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刷卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆