刷卡后如何在View Pager中进行验证? [英] How to validate in view pager after swiped?

查看:91
本文介绍了刷卡后如何在View Pager中进行验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我的视图寻呼机中有一些片段.我想在下一个片段之前在onPageSelected()方法中进行一些验证.除非验证成功,否则我不希望继续下一个片段.另一个问题是,我怎么知道以前的位置?如果我从左向右滑动,则先前的位置将为位置1,如果是从右至左,则将为位置+1.但是无论如何,我们仍然要知道用户滑动的方向.在下图中,如果用户未填写任何内容,则它应保持不变(不移至下一个片段).希望答案正确.

Let's say I've some fragments in my view pager. I want to make some validation in onPageSelected() method before next fragment. I don't want to go next fragment unless validation is succeeded. Another question is, how can I know the previous position? If I swipe left to right, previous position will be position-1 and if right to left, it will be position+1. But is there anyway to know the direction that user swiped. In the following picture, it should stay remain (without going to next fragment) if the user fill nothing. Hoping the correct answer..

推荐答案

经过几个小时的研究,我找到了解决方案.就是这样.

After few hours research, I got my solution. That's it.

private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

    int currentPosition = 0;

    @Override
    public void onPageSelected(int newPosition) {

        // my code
        boolean isValid = false; // <-- here, you need to check yourself valid or not
        if (!isValid) {
            pager.setCurrentItem(currentPosition);
        }else{
            pager.setCurrentItem(newPosition);
            currentPosition = newPosition;
        }
    } }

用户无论刷卡寻呼机多少次,都无法查看下一个屏幕(如果无效).

User can't see the next screen if not valid no matter how many time he swipe the pager.

这篇关于刷卡后如何在View Pager中进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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