📜 ⬆️ ⬇️

SwipeRefreshLayout: not spherical and not in vacuum

About the new SwipeRefreshLayout from the Android support library on Habré has already been written , and Google kindly gives a lot of links to similar examples. They all have one thing in common - the only TextView or ListView is added to SwipeRefreshLayout, and in a minute the developer looks at the working animation with affection. And if we need a slightly more complicated interface?

So, we have an Activity with two elements: on top - the filter panel, on the bottom - a certain View (extra tags have been removed):

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.mycompany.MyView android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

We just need to replace LinearLayout with SwipeRefreshLayout. More precisely, just replace will not work - SwipeRefreshLayout allows you to have only one nested element. Let's just wrap the LinearLayout in SwipeRefreshLayout:

 <android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <fragment android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.mycompany.MyView android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </android.support.v4.widget.SwipeRefreshLayout> 

We start - it works! True, it somehow strangely works: the update indicator either does not appear or disappears.
')
Mothers, google, we study the experience of previous generations. It turns out that you can't put anything in SwipeRefreshLayout. ScrollView is possible, GridView is possible, ListView is possible, and nothing more is impossible. ListView and GridView do not suit us at all, but you can try ScrollView. Wrap our long-suffering LinearLayout in ScrollView:

 <android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <fragment android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.mycompany.MyView android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout> 

Pay attention - Lint immediately requires to change android: layout_height from match_parent to wrap_content from the nested ScrollView elements.

We launch, and ... we see only the filter panel. And where is the lower View? And not him. More precisely, it is, but we do not see it. Previously, LinearLayout stretched it to all free space thanks to match_parent, but now we are inside ScrollView! And ScrollView is why ScrollView, because it’s not going to stretch anyone inside, because its contents can go beyond ScrollView itself.

Exit - either our lower MyView should have a clear height (via android: minHeight or onMeasure ()), or the ScrollView should have the android: fillViewport attribute = "true", which will make it stretch the lower View.

 <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" > 

Glamorous to all animations!

Source: https://habr.com/ru/post/256947/


All Articles