2 ViewPager not scrolling in Android 2.3
E

2

9

I am using Navigation Drawer in my app, that contains some Fragments just like in the below picture.

enter image description here

Every Fragment Contains another ViewPager that is an ImageSlider, and below that is a Listview and at the top I am using the SwipeRefreshLayout. My problem is the image slider works well on devices that has Android version 3.0 or higher but the swipe left or right doesn't works on devices 2.3 and lower, instead it invokes the Parent ViewPager's swipe that is it navigates the fragment. I am using support Version 4 library for this purpose to support devices lower than 3.0. All functions works quite well on 2.3 devices except that one. I have googled it but I haven't found any help anywhere. So to make it scroll what should I do for this, any idea/help will be highly appreciated.

Exserviceman answered 8/9, 2014 at 5:22 Comment(4)
I know that bug. You have the same problem if you use a HorizontalScrollView or a RecyclerView. In general the functionallity is still there if you swipe down than you can scroll.Belshazzar
Because i am using swipeRefresh Layout so pulling down will call this method.Exserviceman
I think in that case you need to extend the viewpager and work with onInterceptTouchEvent(MotionEvent event) to handle the touch events yourself and to forward it to the right views.Belshazzar
@Andro If you haven't the solution yet, check my updated answer.Pulchi
P
1

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

public class CustomViewPager extends ViewPager {

    public CustomViewPager(Context context) {
        super(context);
    }


    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


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

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!!

Pulchi answered 12/9, 2014 at 4:8 Comment(0)
H
0

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:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
    for (View view : scrollableChildren)
    {
        // get the hit rectangle for the view
        Rect rect = new Rect();
        view.getHitRect(rect);

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

    return super.onInterceptTouchEvent(ev);
} 
Heinrick answered 17/9, 2014 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.