Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwipeRefreshLayout detect swipe down event before onRefresh() start

I want to detect when users are pulling down for refresh

(refresh line in under ActionBar is starting expand to its UI width).

I want to replace ActionBar with message ("Swipe down to Refresh") but,

I don't know which event should I use to call my function.

like image 856
Jongz Puangput Avatar asked Apr 28 '14 04:04

Jongz Puangput


1 Answers

You can extend SwipeRefreshLayout, override onTouchEvent, and make your change on the ACTION_DOWN event, ie:

public class MySwipeRefreshLayout extends SwipeRefreshLayout {

    private Runnable onActionDown, onActionUp;

    public MySwipeRefreshLayout(Context context, AttributeSet attributeSet) {
        super(context,attributeSet);
    }

    public void setOnActionDown(Runnable onActionDown) {
        this.onActionDown = onActionDown;
    }

    public void setOnActionUp(Runnable onActionUp) {
        this.onActionUp = onActionUp;
    }

    @Override
    public boolean onTouchEvent (MotionEvent ev) {
        if (ev.getAction() == ev.ACTION_DOWN) {
            if (onActionDown != null) {
                onActionDown.run();
            }
        } else if (ev.getAction() == ev.ACTION_UP) {
            if (onActionUp != null) {
                onActionUp.run();
            }
        }
        return super.onTouchEvent(ev);
    }
 :
 :
}

Make sure you use the extended class in your layout. Then, in your view, you can pass a Runnable to setOnActionDown to update the actionbar or whatever else you want....

like image 114
goldstei Avatar answered Oct 08 '22 04:10

goldstei