Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling & Enabling Paging In ViewPager in Android

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.

like image 614
Shashank_Itmaster Avatar asked Nov 15 '11 09:11

Shashank_Itmaster


People also ask

What does it mean if something is disabling?

1 : to cause (something) to be unable to work in the normal way He disabled the alarm. 2 : to impair physically or mentally : to cause disability in an illness that disabled thousands. disable. transitive verb. dis·​able | \ dis-ˈā-bəl, diz- \

What is the synonyms of disable?

Some common synonyms of disable are cripple, debilitate, enfeeble, sap, undermine, and weaken. While all these words mean "to lose or cause to lose strength or vigor," disable suggests bringing about impairment or limitation in a physical or mental ability. disabled by an injury sustained at work.

Does disabled mean turned off?

(2) Turned off. Not active. "Disabled" does not mean broken or in disrepair. It typically refers to software that has numerous options, or features, that are selectable by the user.

What does disable mean in medical terms?

Medical Definition of disabled 1 : impaired or limited by a physical, mental, cognitive, or developmental condition : affected by disability. 2 : incapacitated by illness, injury, or wounds.

Is Disableable a real word?

Capable of being disabled.


1 Answers

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.

like image 67
Igor Čordaš Avatar answered Sep 29 '22 12:09

Igor Čordaš