Skip to content Skip to sidebar Skip to footer

2 Viewpager Not Scrolling In Android 2.3

I am using Navigation Drawer in my app, that contains some Fragments just like in the below picture. Every Fragment Contains another ViewPager that is an ImageSlider, and below th

Solution 1:

You can use this ViewPager as your parent ViewPager. This allows the child ViewPager to scroll.

publicclassCustomViewPagerextendsViewPager {

    publicCustomViewPager(Context context) {
        super(context);
    }


    publicCustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @OverrideprotectedbooleancanScroll(View v, boolean checkV, int dx, int x, int y) {
        try {
            //Handle the issue only in lower versions of androidif (v != this && v instanceof ViewPager && CJRAppCommonUtility.getOSVersion() < android.os.Build.VERSION_CODES.HONEYCOMB) {
                ViewPagerviewPager= (ViewPager) v;
                intcurrentPage= viewPager.getCurrentItem();
                intsize= viewPager.getAdapter().getCount();
                //if ViewPager has reached its end and if user tries to swipe left allow the parent to scrollif (currentPage == (size - 1) && dx < 0) {
                    returnfalse;
                }
                //if ViewPager has reached its start and if user tries to swipe right allow the parent to scrollelseif (currentPage == 0 && dx > 0) {
                    returnfalse;
                } 
                //Allow the child to scroll hence blocking parent scrollelse {
                    returntrue;
                }
            } 
            returnsuper.canScroll(v, checkV, dx, x, y);
        } catch (Exception e) {
            e.printStackTrace();
            returnfalse;
        }
    }
}

Android developers site has a nice explanation about handling touch events in a Viewgroup. You can refer it here: http://developer.android.com/training/gestures/viewgroup.html

Hope it helps!!

Solution 2:

In older version of Android requestDisallowInterceptTouchEvent doesn't work that great. The solution here is to extend view pager and override the onInterceptTouchEvent and store a list of children that are scrollable. Then, when onInterceptTouchEvent is called you can iterate through the list of scrollable children, get their hit rect, and see if the touch event is inside the hit rect. If it is, you can just return false to not handle it and let the child take it.

Something like this:

@OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev)
{
    for (View view : scrollableChildren)
    {
        // get the hit rectangle for the viewRectrect=newRect();
        view.getHitRect(rect);

        // check to see if the click was inside this childif (rect.contains((int) ev.getX(), (int) ev.getY()))
        {
            returnfalse;
        }
    }

    returnsuper.onInterceptTouchEvent(ev);
} 

Post a Comment for "2 Viewpager Not Scrolling In Android 2.3"