KVISH KVISH - 6 months ago 17
Android Question

SwipeRefreshLayout in API 21

I previously was using API 19 and was using

SwipeRefreshLayout
for many of my fragments. When I loaded content for the first time previously, I was using the
setRefreshing(true);
and was able to load content and it worked fine.

I'm noticing in Android 5.0 Google is using a circular progress view. When I call
setRefreshing(true);
now it simply has no effect. Is there anyway to programmatically show the new spinner? I've delved in to this quite a bit but not able to programmatically show it. I have read the following on this:

http://developer.android.com/tools/support-library/index.html

https://yassirh.com/2014/05/how-to-use-swiperefreshlayout-the-right-way/

SwipeRefreshLayout doesn't show any indicator of refreshing

Basically, it shows no indication of refreshing on the first try.

Answer

Instead of directly setting

getSwipeRefreshLayout().setRefreshing(true);

you can delay it on the UI thread for later

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

this works perfectly for me to start loading in onCreateView and if done in a LoaderManager.LoaderCallbacks too.

I haven't seen the progress bar accidentally sticking (because the data load is too quick), but if you suspect that the load may return too early (error?) it's worth to do the same posting for

getSwipeRefreshLayout().setRefreshing(false);

this way the two posts will definitely come one after the other, resulting in a definitely hidden progress view.

Comments