Bug 1181284 - Enable swipe to refresh on remote panel list based on scroll position r=mcomella.
authorvivek <vivekb.balakrishnan@gmail.com>
Wed, 29 Jul 2015 23:08:38 +0300
changeset 287150 d59e6ed60e5aa0437c91f01a1bc0a8033c319f78
parent 287149 be626b9af2fd4faae6d7be2d2f3790d9c73d8231
child 287151 1e93eab73c6f734efa37d8049f12ee7fc402dbdc
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs1181284
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1181284 - Enable swipe to refresh on remote panel list based on scroll position r=mcomella.
mobile/android/base/home/RemoteTabsSplitPlaneFragment.java
mobile/android/base/resources/layout/home_remote_tabs_split_plane_panel.xml
--- a/mobile/android/base/home/RemoteTabsSplitPlaneFragment.java
+++ b/mobile/android/base/home/RemoteTabsSplitPlaneFragment.java
@@ -1,18 +1,20 @@
 package org.mozilla.gecko.home;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.database.DataSetObserver;
 import android.os.Bundle;
 import android.view.LayoutInflater;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewStub;
+import android.widget.AbsListView;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ImageView;
 import android.widget.ListAdapter;
 import android.widget.TextView;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.RemoteClientsDialogFragment;
@@ -192,16 +194,20 @@ public class RemoteTabsSplitPlaneFragmen
         mClientList.setAdapter(mClientsAdapter);
 
         mObserver = new RemoteTabDataSetObserver();
         mAdapter.registerDataSetObserver(mObserver);
 
         // Now the adapter is wrapped; we can remove our footer view.
         mClientList.removeFooterView(mFooterView);
 
+        // Register touch handler to conditionally enable swipe refresh layout.
+        mClientList.setOnTouchListener(new ListTouchListener(mClientList));
+        mTabList.setOnTouchListener(new ListTouchListener(mTabList));
+
         // Create callbacks before the initial loader is started
         mCursorLoaderCallbacks = new CursorLoaderCallbacks();
         loadIfVisible();
     }
 
     @Override
     protected void updateUiFromClients(List<RemoteClient> clients, List<RemoteClient> hiddenClients) {
         if (getView() == null) {
@@ -357,9 +363,40 @@ public class RemoteTabsSplitPlaneFragmen
 
             // Update the background based on the state of the selected client.
             final RemoteClient client = getItem(position);
             final boolean isSelected = client.guid.equals(sState.selectedClient);
             adapter.updateClientsItemView(isSelected, context, view, getItem(position));
             return view;
         }
     }
+
+    /**
+     * OnTouchListener implementation for ListView that enables swipe to refresh on the touch down event iff list cannot scroll up.
+     * This implementation does not consume the <code>MotionEvent</code>.
+     */
+    private class ListTouchListener implements View.OnTouchListener {
+        private final AbsListView listView;
+
+        public ListTouchListener(AbsListView listView) {
+            this.listView = listView;
+        }
+
+        @Override
+        public boolean onTouch(View v, MotionEvent event) {
+            final int action = event.getAction();
+            switch (action) {
+                case MotionEvent.ACTION_DOWN:
+                    // Enable swipe to refresh iff the first item is visible and is at the top.
+                    mRefreshLayout.setEnabled(listView.getCount() <= 0
+                    	    || (listView.getFirstVisiblePosition() <= 0 && listView.getChildAt(0).getTop() >= 0));
+                    break;
+                case MotionEvent.ACTION_CANCEL:
+                case MotionEvent.ACTION_UP:
+                    mRefreshLayout.setEnabled(true);
+                    break;
+            }
+
+            // Event is not handled here, it will be consumed in enclosing SwipeRefreshLayout.
+            return false;
+        }
+    }
 }
--- a/mobile/android/base/resources/layout/home_remote_tabs_split_plane_panel.xml
+++ b/mobile/android/base/resources/layout/home_remote_tabs_split_plane_panel.xml
@@ -11,18 +11,17 @@
     <ViewStub android:id="@id/home_empty_view_stub"
               android:layout="@layout/home_empty_panel"
               android:layout_width="match_parent"
               android:layout_height="match_parent"/>
 
     <org.mozilla.gecko.widget.GeckoSwipeRefreshLayout
             android:id="@id/remote_tabs_refresh_layout"
             android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:enabled="false">
+            android:layout_height="match_parent">
 
         <LinearLayout android:layout_width="match_parent"
                       android:layout_height="match_parent"
                       android:orientation="horizontal">
 
             <org.mozilla.gecko.home.HomeListView
                 android:id="@+id/clients_list"
                 style="@style/Widget.RemoteTabsListView"