6

SwipeRefreshLayout is so sensitive that when I swipe through the screen horizontally, with only a little bit vertical movement, the "pull to refresh" indicator will show. How can I avoid this? SwipeRefreshLayout has a function

public void setDistanceToTriggerSync (int distance)

can it has something like

public void setDistanceToTriggerIndicator (int distance)

to show the indicator only after a certain amount of vertical distance is moved downwards, or do you guys have some work around?

Thanks!

Viky Leaf
  • 195
  • 3
  • 16
  • 2
    Possible duplicate of [HorizontalScrollView inside SwipeRefreshLayout](http://stackoverflow.com/questions/23989910/horizontalscrollview-inside-swiperefreshlayout) – Sufian Oct 13 '16 at 11:32

1 Answers1

3

Hey @Viky I have faced same issue with View pager inside swipe refresh layout and I found this solution is perfect hope it help.

Following code is taken from this SO answer:

public class MySwipeRefreshLayout extends SwipeRefreshLayout {

    private int mTouchSlop;
    private float mPrevX;
    // Indicate if we've already declined the move event
    private boolean mDeclined;

    public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPrevX = MotionEvent.obtain(event).getX();
                mDeclined = false; // New action
                break;

            case MotionEvent.ACTION_MOVE:
                final float eventX = event.getX();
                float xDiff = Math.abs(eventX - mPrevX);

                if (mDeclined || xDiff > mTouchSlop) {
                    mDeclined = true; // Memorize
                    return false;
                }
        }

        return super.onInterceptTouchEvent(event);
    }

}
Community
  • 1
  • 1
Antwan
  • 3,837
  • 9
  • 41
  • 62
  • 2
    Please mark questions as duplicate if you find the question to be the same. – Sufian Oct 13 '16 at 11:31
  • @Sufian This question has a much more meaningful title (which is how/why I found it) than the older SO question. Was just wondering if marking this question as a duplicate will have any bearing on how discoverable it is? – ban-geoengineering Jan 24 '17 at 15:41
  • @ban-geoengineering AFAIK marking it as a duplicate will add a link to the other (older) question. So instead of reading comments, etc, people will see it at the top of the page (that this is a dup). – Sufian Jan 24 '17 at 15:49