2

The short question How do you impliment a SwipeRefreshLayout on an Activity that hosts two Fragments side by side in such a way that the refresh loading spinner apeares centered in the screen.

**The Long question I have an activity that hosts several fragments. I would like to impliment a SwipeRefreshLayout on the activity level to the effect that no matter which fragments are showing, the refresh is available.

I have 1 base activity and 3 fragments. The fragments are hidden and shown using a fragment manager according the the program state. In some cases, one fragment will cover the entire space of the activity. in other cases 2 fragments will sit side by side and collectively cover the entire space of the activity.

When I implement the SwipeRefreshLayout on the fragment level, the refresh functions correctly, however in the case where I have 2 fragments side by side, the spinner will be centered to the fragment rather than the activity.

When I Implement the SwipeRefreshLayout on the Activity Level, the Fragments absorb the gesture events and the SwipeRefreshLayout is unresponsive.

What I want to have happen is a downward swipe anywhere in the side by side fragments layout to bring the refresh loading spinner down in the center according to the activity rather than the fragment. For this to happen, either I need to find a way to implement the SwipeRefreshLayout on the activity level where it will pickup on motion events observed in the fragments or I will need to implement the SwipeRefreshLayout on the fragment level and find a way to center the Loading spinner.

If anyone knows how to do either case, I would greatly appreciate the help. This is the xml file for my activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.activity.BaseActivity">

    <!-- Layout with my fragments -->
    <LinearLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <!--Frame Layout for Dashboard List fragment-->
        <FrameLayout
            android:id="@+id/baseDashLayout"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <fragment
                android:id="@+id/baseDashFragment"
                class="com.example.activity.fragment.DashboardListFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>

        <View
            android:id="@+id/seperatorLine"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <android.support.v4.app.FragmentTabHost
            android:id="@+id/tabContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <!--Layout for Tabs widget-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TabWidget
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <LinearLayout
                    android:id="@+id/tabContent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/tabs"
                    android:orientation="vertical" />
            </LinearLayout>
        </android.support.v4.app.FragmentTabHost>

        <!-- Frame Layout for Tabbed page Fragment-->
        <FrameLayout
            android:id="@+id/baseTabLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true">

            <fragment
                android:id="@+id/baseTabFragment"
                class="com.example.fragment.PageTabFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>

        <!-- Frame layout for setup connection fragment -->
        <FrameLayout
            android:id="@+id/baseSetConLayout"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <fragment
                android:id="@+id/baseSetConFragment"
                class="com.example.activity.fragment.SetConnectionFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>
    </LinearLayout>
    <!-- End Layout with my fragments -->

    <!-- Start Swipe to refresh Layout -->
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/baseRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
Lamorak
  • 10,957
  • 9
  • 43
  • 57
wolfaviators
  • 503
  • 1
  • 7
  • 21
  • 1
    The Fragments are most likely absorbing the swipe because your swipe-refresh is sitting behind them. Make sure in the activity layout that you put the View you want to swipe at that bottom so it sits on top of everything. Then implement your swipe gesture. – Xjasz May 13 '15 at 17:03
  • 1
    @Jasz Thanks for the response. If I understand correctly then I should change my xml layout for my activity from to with the effect that the swipe refresh layout will always live on top of the fragments. is that right? – wolfaviators May 13 '15 at 17:53
  • 1
    @ M.Alan Farnsworth that needs to be changed to SwipeRefresh needs to be at the bottom of the layout otherwise everything is going to be sitting on top of it and eating the touch event – Xjasz May 13 '15 at 17:56
  • It needs to be its own object if it has objects inside of it that have their own touch events. Then those will eat the touch event before it can. – Xjasz May 13 '15 at 17:56
  • I rearanged the xml layout so that there would be an instance of SwipeRefreshLayout on top of the fragments. When I run the code, I get a NullPointerException at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35). Think it might have something to do with the container not holding anything, but when I put a view or a space in the container, that view absorbs the touch events and leaves the fragments with nothing. – wolfaviators May 13 '15 at 18:27
  • 1
    Ya I feel your pain and could never find a simple method to solve this. I had to make my own Class to handle gestures on the top view. Following this http://stackoverflow.com/questions/937313/android-basic-gesture-detection Using a gesture detector you can decide whether or not someone is trying to refresh or not. – Xjasz May 13 '15 at 18:40
  • `SwipeRefreshLayout` extends `ViewGroup`, you should wrap the refreshable content in it. – Lamorak May 14 '15 at 15:41

0 Answers0