Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwipeRefreshLayout no animation on fragment creation

I'm using android.support.v4.widget.SwipeRefreshLayout with android.support.v7.widget.RecyclerView.
On fragment view creation I need to show SwipeRefreshLayout animation. But when I call setRefreshing(true) nothing happens. But when I refresh data animation changes.
I suppose that the animation isn't showing without child inside SwipeRefreshLayout.
How to show this animation in the best way?

like image 491
Maksym Avatar asked Oct 21 '14 18:10

Maksym


3 Answers

This is a reported bug (reported here) and a workaround is available:

mSwipeRefreshLayout.setProgressViewOffset(false, 0,
    (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 
        24, 
        getResources().getDisplayMetrics()));
mSwipeRefreshLayout.setRefreshing(true);
like image 110
Tadas Šubonis Avatar answered Oct 07 '22 16:10

Tadas Šubonis


It depends on which API level you're building under - if you're using up to API 20 then you can just turn on setRefreshing(true), this will run the animation in the ActionBar, but in API 21 (Material Design) they changed the progress to be a spinner than is "pulled into view" before it spins

You have 2 ways of getting around this in API 21: 1) shift the spinner down with setProgressViewOffset(), but remember to shift it back up afterwords (note that this works in px, while setDistanceToTriggerSync() uses dp) 2) make a duplicate spinner that is displayed when you're loading the data

The more code-efficient solution is to use the existing spinner, but you have to be careful that you do reset its position

If you need to calculate the pixel density, you can grab it from:

DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
float scale = metrics.density;
like image 2
Phil A Avatar answered Oct 07 '22 17:10

Phil A


Thanks, guys.

I`ve just realised that the easiest solution is to call method measure(int, int) with non-zero values) before calling setRefreshing(true). The problem is happening because setRefreshing(true) is calling before onMeasure.

So you need to call:

swipeRefreshLayout.measure(initialWidth, initialHeight);
swipeRefreshLayout.setRefreshing(true);
like image 2
Maksym Avatar answered Oct 07 '22 17:10

Maksym