Skip to content

Instantly share code, notes, and snippets.

@tunjid
Created April 9, 2015 13:25
Show Gist options
  • Save tunjid/3ff00ca6bb0a8d27fa3c to your computer and use it in GitHub Desktop.
Save tunjid/3ff00ca6bb0a8d27fa3c to your computer and use it in GitHub Desktop.
/** * A class that lets an activity know when the SwipeRefreshLayout has * been triggered, and when all finger's are off the device. */
package com.myfab5.mobile.myfab5.ui.helpers.customcomponents;
import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* A class that lets an activity know when the SwipeRefreshLayout has
* been triggered, and when all finger's are off the device.
*/
public class NotifyingSwipeRefreshLayout extends SwipeRefreshLayout {
private OnSwipeListener swipeListener;
public NotifyingSwipeRefreshLayout(Context context) {
super(context);
}
public NotifyingSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final boolean superClassResult = super.onTouchEvent(ev);
final int action = MotionEventCompat.getActionMasked(ev);
switch (action) {
case MotionEvent.ACTION_MOVE:
if (superClassResult) { // The widget has been engaged.
swipeListener.onSwipe();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: // All fingers are up
swipeListener.onFingerUp();
break;
}
return superClassResult;
}
/**
* Set the listener to be notified when a refresh is triggered via the swipe
* gesture or all fingers are off.
*/
public void setOnSwipeListener(OnSwipeListener listener) {
swipeListener = listener;
}
/**
* Classes that wish to be notified when a swipe has occured or fingers are off
* should implement this interface.
*/
public interface OnSwipeListener {
void onSwipe();
void onFingerUp();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment