如何在android中禁用或启用viewpager滑动

How to disable or enable viewpager swiping in android(如何在android中禁用或启用viewpager滑动)
本文介绍了如何在android中禁用或启用viewpager滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做什么:我正在尝试在程序运行时以编程方式启用/禁用在寻呼机中滑动

What i am trying to do: I am trying to Enable/disable swiping in pager programatically when the program is running

例如: 在流程中,如果我检查条件并且如果它返回 true 启用刷卡,并且如果条件返回 false 禁用滑动.

Ex: When on the flow if i check for a condition and if it returns true enable swiping, and if condition returns false disable swiping.

public class CustomViewPager extends ViewPager {

private boolean enabled;

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.enabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.enabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.enabled = enabled;
} }

然后选择这个而不是XML中的内置viewpager

Then select this instead of the builtin viewpager in XML

<mypackage.CustomViewPager 
android:id="@+id/myViewPager" 
android:layout_height="match_parent" 
android:layout_width="match_parent" />

您只需调用setPagingEnabled"方法,设置为false",用​​户将无法滑动分页.

You just need to call the "setPagingEnabled" method with "false" and users won't be able to swipe to paginate.

上述方法的问题: 我无法在流上设置属性,我:e .... 我可以启用刷卡或禁用刷卡.但我不能根据条件这样做

Problem with above methodology: i cannot set the property on the flow, I:e .... I either can enable swiping or disable swiping. But i cannot do this based on condition

  1. 我可以通过其他方式实现我的目标吗?如果可以,那是什么?
  2. 或者这不可能吗?

推荐答案

以编程方式禁用滑动-

    final View touchView = findViewById(R.id.Pager); 
    touchView.setOnTouchListener(new View.OnTouchListener() 
    {         
        @Override
        public boolean onTouch(View v, MotionEvent event)
        { 
           return true; 
        }
     });

并使用它来手动滑动

touchView.setCurrentItem(int index);

这篇关于如何在android中禁用或启用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)