Disabling & Enabling Paging In ViewPager in Android

旧巷老猫 提交于 2019-12-29 03:59:29

问题


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.

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;
        }
    }

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

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


回答1:


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);
}

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




回答2:


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

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

replace "return false" to "return this.enable" and set default value of this.enable to false




回答3:


You're setting it to false but have no case for resetting it back to true. Also, your intercept touch logic seems a bit odd... the only time you allow the pager to intercept (and thereby process in onTouchEvent()) is when you set paging enabled to false. How about the following?

 @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean result = true;
        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 = false;
            } else {
                setPagingEnabled(true);
            }
        }
        return result;
    }


来源:https://stackoverflow.com/questions/8134336/disabling-enabling-paging-in-viewpager-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!