WaltersGE1 WaltersGE1 - 1 month ago 18
Android Question

SwipeRefreshLayout, when visibility == View.GONE, doesn't hide child view

I'm getting inconsistent behavior with SwipeRefreshLayout.setVisibility(View.GONE). Sometimes, my swipe refresh layouts hide the child view, but some times they don't. Since SwipeRefreshLayout subclasses ViewGroup, I'm expecting it to always hide the child view whenever it's visibility is .GONE, but that isn't happening.

Any insights are appreciated.

<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mySwipeRefreshLayout">

<View
android:background="#FF0000"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>


Update: After spending the afternoon stripping everything out of the fragment, I confirmed that it's our setup that is causing the problem. Even so, it's still strange that setting the child view to VIEW.Gone works, but doing so to the swipe refresh layout doesn't hide the child.

Answer

In your xml file provide id to SwipeRefreshLayout and view

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mySwipeRefreshLayout">

    <View
        android:background="#FF0000"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

then use id of both in .java file and use their objects.

 if(swipeRefreshLayout.isRefreshing()){
       view.setVisibility(View.VISIBLE);        
 }else{
       view.setVisibility(View.INVISIBLE);    
 }