How To Implement Swipe-to-Refresh In Android?

If you are updating you app content automatically and user does not need to do any thing for that.But you can also provide feature to user so that they can request for manual update.For example in video streaming app user can request manual update to get latest videos.
To provide consistence user experience for requesting updates.Android provide swipe-to-refresh design pattern,which allow user to trigger update with vertical swipe gesture.

Add Swipe-to-Refresh To App.

Swipe-to-Refresh design pattern is completely implemented with SwipeRefreshLayout widget.which detects vertical swipe gesture of user.Display distinctive progress bar and call callback method in app.You can add swipe-to-Refresh feature by adding this widget in to you layout file as a parent of RecyclerView , ListView or GridView.
Note : SwipeRefreshLayout only support signle RecyclerView , ListView or GridView as his child.

Following code snipped shows how to add SwipeRefershLayout in layout file containing RecyclerView.


 Add Refresh Action To Action Bar.

 

You should add refresh action in action bar , so that user with disability could trigger manual update with external device like keyboard and D-pads.
Following code snipped shows how to add swipe-to-refresh action to menu overflow area.


Refresh App Content With Refresh Requests.


Respond To User Gesture.

To respond to user's vertical swipe gesture you have to implement SwipeRefreshLayout.OnRefreshListener interface and it's onRefresh() method,OnRefresh() method called when user perform vertical swipe gesture.System automatically shows distinctive progress bar when user perform swipe gesture and you should update app content in onRefresh() method.

Following code snipped shows how to implement SwipeRefreshLayout.OnRefreshListener and update app content when you get refresh request from user's vertical swipe gesture.


Respond To The Action Bar Refresh Action.

When user request manual refresh request from action bar,Android OS calls the onOptionsItemSelected() method.Your app should show progress indicator and update app's data when this method calls.To get callback of menu item selection override onOptionsItemSelected() method.To display SwipeRefreshLayout progress indicator call setRefreshing() method of SwipeRefreshLayout with true value,And once update has finished,call setRefreshing(false) to remove progress indicator.

Following code snippet shows how to respond request action.


No comments:

Post a Comment