user3896501 user3896501 - 8 days ago 5
Android Question

SwipeRefreshLayout + ViewPager, limit horizontal scroll only?

I've implemented

SwipeRefreshLayout
and
ViewPager
in my app but there is a big trouble: whenever I'm going to swipe left / right to switch between pages the scrolling is too sensitive. A little swipe down will trigger the
SwipeRefreshLayout
refresh too.

I want to set a limit to when horizontal swipe starts, then force horizontal only until swiping is over. In other words, I want to cancel vertical swipping when finger is moving horizontally.

This problem only occurs on
ViewPager
, if I swipe down and
SwipeRefreshLayout
refresh function is triggered (the bar is shown) and then I move my finger horizontally, it still only allows vertical swipes.

I've tried to extend the
ViewPager
class but it isn't working at all:

public class CustomViewPager extends ViewPager {

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean in = super.onInterceptTouchEvent(ev);
if (in) {
getParent().requestDisallowInterceptTouchEvent(true);
this.requestDisallowInterceptTouchEvent(true);
}
return false;
}

}


Layout xml:

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/viewTopic"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.myapp.listloader.foundation.CustomViewPager
android:id="@+id/topicViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>


any help would be appreciated, thanks

Answer

Solved very simply without extending anything

mPager.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mLayout.setEnabled(false);
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                mLayout.setEnabled(true);
                break;
        }
        return false;
    }
});

work like a charm