I've implemented a fragment which has a SwipeRefreshLayout as content view. Refresh animation is triggered at onRefresh but it never stops even if setRefreshing is set to false after retrieving the data from server.
@Override
public void onRefresh() {
handler.post(refreshing);
}
private final Runnable refreshing = new Runnable(){
public void run(){
try {
if(isRefreshing()){
handler.postDelayed(this, 1000);
}else{
swipeLayout.setRefreshing(false);
mainActivity.forceUpdate();
setLayout();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
};
private boolean isRefreshing(){
return swipeLayout.isRefreshing();
}
To disable the gesture and progress animation, call setEnabled(false) on the view. This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.
Android SwipeRefreshLayout is a ViewGroup that can hold only one scrollable child. It can be either a ScrollView, ListView or RecyclerView. The basic need for a SwipeRefreshLayout is to allow the users to refresh the screen manually. This is pretty common in the Facebook Newsfeed screen.
well, of course it's not stopping to refresh, look at your loop.
if(isRefreshing()){
handler.postDelayed(this, 1000);
}else{
swipeLayout.setRefreshing(false);
mainActivity.forceUpdate();
setLayout();
}
basically you are never stopping it, the "swipeLayout.setRefreshing(false);" is never called, since the condition it needs is (isRefreshing()) to be false, so it doesn't make any sense, you will always get a true value from "isRefreshing()" since you're never telling it to stop. setRefreshing to false based on the boolean value of isRefreshing is wrong in my opinion, you shouldn't rely on it, you should be the one to decide when you want to stop the refresh state.
usually PullToRefresh is used to get new data from server. so basically I would recommend inserting this check right where you get an answer from your server :
if (swipeLayout.isRefreshing()) {
swipeLayout.setRefreshing(false);
}
because once you get your new data from the server, that's when you need to stop refreshing.
EDIT - added a full code example
protected SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
retrieveDocuments();
}
};
public void retrieveDocuments() {
//some code, server call
//onCompletion is your server response with a success
@Override
public void onCompletion(String result) {
if (mSwipeRefreshLayout.isRefreshing()) {
mSwipeRefreshLayout.setRefreshing(false);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With