Mulgard
Mulgard

Reputation: 10609

SwipeRefreshLayout in both directions?

I love the new SwipeRefreshLayout! It looks great and its quite easy to use. But i want to use it in both directions. I have a message screen and i want to load older messages by swiping from top to bottom and i want to load newer messages by swiping from bottom to top.

This example: http://www.bignerdranch.com/blog/implementing-swipe-to-refresh/ explained quite easily how to get the top to bottom swiping but how can i implement that for both directions?

Upvotes: 2

Views: 1882

Answers (1)

kha
kha

Reputation: 20023

Based on the source code https://github.com/futuresimple/android-support-v4/blob/master/src/java/android/support/v4/widget/SwipeRefreshLayout.java it's only listening to vertical changes (See public boolean onTouchEvent(MotionEvent event)).

You should be able to override the class and implement this override yourself to enable it to listen to horizontal and vertical swipes but I don't think there's anything out of the box that allows you to do that.

The bit that should be of most interest is this:

case MotionEvent.ACTION_MOVE: 
             if (mDownEvent != null && !mReturningToStart) { 
                 final float eventY = event.getY(); 
                 float yDiff = eventY - mDownEvent.getY(); 
                 if (yDiff > mTouchSlop) { 
                     // User velocity passed min velocity; trigger a refresh 
                    if (yDiff > mDistanceToTriggerSync) { 
                        // User movement passed distance; trigger a refresh 
                         startRefresh(); 
                         handled = true; 
                         break; 
                     } else { 
                         // Just track the user's movement 
                         setTriggerPercentage( 
                                 mAccelerateInterpolator.getInterpolation( 
                                         yDiff / mDistanceToTriggerSync)); 
                         float offsetTop = yDiff; 
                         if (mPrevY > eventY) { 
                             offsetTop = yDiff - mTouchSlop; 
                         } 
                         updateContentOffsetTop((int) (offsetTop)); 
                         if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) { 
                             // If the user puts the view back at the top, we 
                             // don't need to. This shouldn't be considered 
                             // cancelling the gesture as the user can restart from the top. 
                             removeCallbacks(mCancel); 
                         } else { 
                             updatePositionTimeout(); 
                         } 
                         mPrevY = event.getY(); 
                         handled = true; 
                     } 
                 } 
             } 
             break; 

Please note that I do not recommend this approach. It's not standard interface and no one will expect it to work that way which can be confusing. Overriding this method in a custom layout also has the added risk that you may have to redo everything in the future when a new version comes out and you want to upgrade.

Upvotes: 1

Related Questions