SwipeRefreshLayout update animation does not stop

I have implemented a snippet that has a SwipeRefreshLayout as content. The updated animation runs on onRefresh, but it never stops even if setRefreshing is set to false after fetching data from the 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();
}

      

+3


source to share


5 answers


well of course it doesn't stop to refresh, look at your loop.

if(isRefreshing()){
    handler.postDelayed(this, 1000);
}else{
    swipeLayout.setRefreshing(false);
    mainActivity.forceUpdate();
    setLayout();
}

      

basically you never stop it, "swipeLayout.setRefreshing (false)"; is never called because the condition it needs is (isRefreshing ()) false, so it doesn't make any sense, you will always get a true value from "isRefreshing ()" since you never tell it to stop, setRefreshing to false based on boolean isRefreshing is not correct in my opinion, you shouldn't rely on it, you should decide when you want to stop the refresh state.

usually PullToRefresh is used to get new data from the server. so in principle I would recommend inserting this check right where you got the response from your server:



if (swipeLayout.isRefreshing()) {
     swipeLayout.setRefreshing(false);
}

      

because as soon as you get new data from the server you need to stop updating.

EDIT - Added complete 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);
    }
}

}

      

+19


source


This will remove the animation:



mSwipeRefreshLayout.setRefreshing(false);

      

+9


source


Use setRefreshing(false)

in the UI thread.

Try the following:

Handler mHandler = new Handler();//In UI Thread

      

...

mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        swipeLayout.setRefreshing(isRefreshing);
    }
}, delayTime);

      

+1


source


In my case, I did

mSwipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefreshLayout.setRefreshing(true);
                }
            });

      

twice. Then it is not removed

mSwipeRefreshLayout.setRefreshing(false);

      

+1


source


if you want to stop the loading animation on page load, you first need to find that when the page is fully loaded in the Webview.

Here is the complete code to load the page in Webview and then stop loading the animation after the page is fully loaded.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);


    final WebView wvSocialMediaPage = findViewById(R.id.wv_social_media_page);
    wvSocialMediaPage.setWebViewClient(new CustomWebViewClient());
    WebSettings webSetting = wvSocialMediaPage.getSettings();
    webSetting.setJavaScriptEnabled(true);
    webSetting.setDisplayZoomControls(true);
    wvSocialMediaPage.loadUrl("https://facebook.com");

    swipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_container);
    swipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    wvSocialMediaPage.reload();
                }
            }
    );
}



    private class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        swipeRefreshLayout.setRefreshing(false);
    }
}

      

onPageFinished will find when the page is loaded. Stop the animation successfully.

+1


source







All Articles