Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 1000
KVISH Avatar asked Oct 28 '14 05:10

KVISH


People also ask

What is SwipeRefreshLayout?

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.

How to stop SwipeRefreshLayout in Android?

If the listener determines there should not be a refresh, it must call setRefreshing(false) to cancel any visual indication of a refresh. If an activity wishes to show just the progress animation, it should call setRefreshing(true). To disable the gesture and progress animation, call setEnabled(false) on the view.


1 Answers

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.

like image 175
TWiStErRob Avatar answered Sep 29 '22 11:09

TWiStErRob