Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we set distance to trigger indicator for SwipeRefreshLayout?

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!

like image 739
Viky Leaf Avatar asked Sep 17 '15 10:09

Viky Leaf


People also ask

How to stop SwipeRefreshLayout in android?

If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.

How to implement swipe down to refresh in android?

In this file connect the swipeRefreshLayout and textView to its XML file by using the findViewById() method. And also call the setOnRefreshListener() to change the text after the user swipe down the screen. The users can also write the required codes as their needs inside this method.

What is swipe refresh?

Android provides a widget that implements the swipe-to-refresh design pattern, which allows the user to trigger an update with a vertical swipe. This is implemented by the SwipeRefreshLayout widget, which detects the vertical swipe, displays a distinctive progress bar, and triggers callback methods in your app.


1 Answers

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);
    }

}
like image 114
Antwan Avatar answered Nov 15 '22 03:11

Antwan