KVISH
KVISH

Reputation: 13178

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.

Upvotes: 18

Views: 6280

Answers (6)

KVISH
KVISH

Reputation: 13178

This is not a very clean solution, but it works for now. setRefreshing(boolean) the progress view is behind the action bar. The solution is to push it down:

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int height = size.y;
    getSwipeRefreshLayout().setProgressViewOffset(false, -200, height / 9);

If you have a better solution, please post it below and I'll accept it!

Please don't use this solution, the accepted answer is better.

Upvotes: 1

Jesús Castro
Jesús Castro

Reputation: 2119

KVISH, please review my answer on: SwipeRefreshLayout doesn't show any indicator of refreshing

The approach is to add your view in a ScrollView widget. If you use AppCompat Library v21.0.3, it would should work.

Upvotes: 0

TWiStErRob
TWiStErRob

Reputation: 46460

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.

Upvotes: 30

Maksym
Maksym

Reputation: 116

You can try to call measure(int, int) method before setRefreshing(true)

Take a look at https://stackoverflow.com/a/26692841/2114930.

Upvotes: 0

Phil A
Phil A

Reputation: 97

What you have to remember is that the Material Design SwipeRefreshLayout has changed, while the old one used the BakedBezierInterpolator, but the new one uses the "dragged into position" progress spinner

Unfortunately API 21 doesn't include the BakedBezierInterpolator anymore, so from my point of view you have 2 options: 1) shift the spinner down (and remember to shift it back up afterwards) 2) make your own spinner to sit on the screen when you're initially loading the data

If you want to keep your spinner the same, then definitely use the SwipeRefreshLayout one, but make sure that you remember to re-position it

I hope this helps in some way...

Upvotes: 0

ae86
ae86

Reputation: 1

I encountered the same problem,but only in the case of using StickyListHeadersListView. Using android.widget.ListView,everything is OK.

I think in api 21,Google has rewritten Listview class。 If you are using Listview subclass,I guess that you might need to make the subclass exends android.widget.ListView in api 21.

Upvotes: 0

Related Questions