禁用和在 Android 的 ViewPager 中启用分页

Disabling amp; Enabling Paging In ViewPager in Android(禁用和在 Android 的 ViewPager 中启用分页)

本文介绍了禁用和在 Android 的 ViewPager 中启用分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 android 兼容包版本 4 在我的应用程序中显示 pdf 页面.我用过 PagerAdapter &ViewPager 用于显示 pdf 页面,如水平滚动视图.

I am using android compatibility package version 4 for displaying pdf pages in my app. I have used PagerAdapter & ViewPager for displaying pdf pages like horizontal scroll view.

现在问题出在与分页相关的东西上.我可以根据这个线程通过检测 viewpager 内的子项来停止分页 android:ViewPager 和 Horizo​​ntalScrollVIew,但是当用户在该视图之外触摸时,我怎样才能启用它.我为 CustomViewPager 使用了以下代码.

Now the problem is in paging related stuff.I am able to stop paging by detecting childs inside the viewpager according to this thread android: ViewPager and HorizontalScrollVIew, but how can I can enable that back when user touches outside of that view. I have used following code for CustomViewPager.

public class CustomViewPager extends ViewPager {

        private boolean enabled;
        private int childId;

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

        public void setChildId(int childId) {
            this.childId = childId;
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent event) {
            boolean result = false;
            View scroll = getChildAt(childId);
            if (scroll != null) {
                Rect rect = new Rect();
                CommonLogic.logMessage("PDF Page Rectangle  ", TAG, Log.VERBOSE);
                scroll.getHitRect(rect);
                if (rect.contains((int) event.getX(), (int) event.getY())) {
                    setPagingEnabled(false);
                    result = true;
                }
            }
            return result;
        }

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

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

当尝试触摸 pdf 页面外部时,if(scroll!=null) 在两个方向都变为 true.

When try to touch outside the pdf page then also that if(scroll!=null) becomes true in both orientation.

谁能帮我弄清楚如何重新启用它,以便在 ViewPager 中继续分页.

Can any one help me out how to enable it back so that paging will continue in ViewPager.

推荐答案

我在分页图像文件时遇到了类似的问题,需要捏合缩放.简单地说,需要一种在放大图像时禁用分页并在显示整个图像时启用它的方法.我像这样解决了它,并认为你可以做类似的事情.首先扩展:类 MyViewPager 扩展 ViewPager {...}然后在该类中覆盖以下两个方法

I was having a similar problem with paging image files that need pinch to zoom. Simply put Needing a way to disable paging when image is zoomed in and enable it when the whole image is shown. I solved it like this and think you could do a similar thing. First extend : class MyViewPager extends ViewPager {...} And then in that class override following two methods

    @Override
public boolean onTouchEvent(MotionEvent event) {
    if (YOUR_CRITERIA_TOENABLE_DISABLE) {
        return true;

    } else {
        return super.onTouchEvent(event);
    }
}

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

请务必在 xml 布局或从代码动态创建中使用您的视图分页器.

Be sure to use your view pager in xml layouts or dynamic creation from code.

这篇关于禁用和在 Android 的 ViewPager 中启用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:禁用和在 Android 的 ViewPager 中启用分页

基础教程推荐