Month: April 2015

AdvancedRecyclerViewDemo

The AdvancedRecyclerView Library is incredible. It has support for so many niceties, it’s almost indispensable. Unfortunately, documentation is sparse.

I wrote a quick demo for the draggable and swipable functions of the library that may be perused below. I plan to add support for the expandable items in the future. So, if you happen to need some documentation for the library, click the link below.

Demo

Implementing a “Swipe” and “Finger Up” listener for Android’s SwipeRefreshLayout

Android’s SwipeRefreshLayout is pretty useful for internet based apps that need to notify a user when a pull to refresh action has occurred. However, if you have a complex view hierarchy that uses a FrameLayout to draw certain items over another, say for example a contextual toolbar, the circular ProgressBar drawn by the SwipeRefreshLayout is obscured and hidden from the user.

A way to fix this is to create a class that extends the SwipeRefreshLayout, and Override the OnTouchEvent. Once overridden, call on the superclass to make sure the widget performs as intended, keep the boolean it returns in a local variable, and handle the motion events which will in turn fire off an interface callback when certain conditions are met.

It’s not complicated when presented in code form. The extended class is shown in it’s entirety below


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