[Android] [PATCH] Swipe to refresh

Geoffrey Métais geoffrey.metais at gmail.com
Fri Nov 28 17:26:55 CET 2014


---
 vlc-android/res/layout/audio_albums_songs.xml      |  66 +++---
 vlc-android/res/layout/audio_browser.xml           | 259 +++++++++++----------
 vlc-android/res/layout/directory_view.xml          |  22 +-
 vlc-android/res/layout/history_list.xml            |  36 +--
 vlc-android/res/layout/video_grid.xml              |  41 ++--
 .../videolan/vlc/gui/DirectoryViewFragment.java    |  24 +-
 .../src/org/videolan/vlc/gui/HistoryFragment.java  |  28 ++-
 .../vlc/gui/audio/AudioAlbumsSongsFragment.java    |  29 ++-
 .../vlc/gui/audio/AudioBrowserFragment.java        |  31 ++-
 .../videolan/vlc/gui/video/VideoGridFragment.java  |  29 ++-
 10 files changed, 356 insertions(+), 209 deletions(-)

diff --git a/vlc-android/res/layout/audio_albums_songs.xml b/vlc-android/res/layout/audio_albums_songs.xml
index a0e51c1..b785f51 100644
--- a/vlc-android/res/layout/audio_albums_songs.xml
+++ b/vlc-android/res/layout/audio_albums_songs.xml
@@ -20,40 +20,44 @@
             android:id="@android:id/tabcontent"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent" >
-
-            <org.videolan.vlc.widget.FlingViewGroup
-                android:id="@+id/fling_view_group"
+            <android.support.v4.widget.SwipeRefreshLayout
+                android:id="@+id/swipeLayout"
                 android:layout_width="match_parent"
-                android:layout_height="match_parent" >
-
-                <ListView
-                    android:id="@+id/albums"
+                android:layout_height="match_parent">
+                <org.videolan.vlc.widget.FlingViewGroup
+                    android:id="@+id/fling_view_group"
                     android:layout_width="match_parent"
-                    android:layout_height="match_parent"
-                    android:clipToPadding="false"
-                    android:fastScrollEnabled="true"
-                    android:paddingBottom="@dimen/listview_bottom_padding"
-                    android:paddingLeft="20dp"
-                    android:paddingRight="20dp"
-                    android:nextFocusUp="@+id/ml_menu_search"
-                    android:nextFocusDown="@id/albums"
-                    android:nextFocusLeft="@id/albums"
-                    android:nextFocusRight="@id/albums" />
+                    android:layout_height="match_parent" >
 
-                <ListView
-                    android:id="@+id/songs"
-                    android:layout_width="match_parent"
-                    android:layout_height="match_parent"
-                    android:clipToPadding="false"
-                    android:fastScrollEnabled="true"
-                    android:paddingBottom="@dimen/listview_bottom_padding"
-                    android:paddingLeft="20dp"
-                    android:paddingRight="20dp"
-                    android:nextFocusUp="@+id/ml_menu_search"
-                    android:nextFocusDown="@id/songs"
-                    android:nextFocusLeft="@id/songs"
-                    android:nextFocusRight="@id/songs" />
-            </org.videolan.vlc.widget.FlingViewGroup>
+                    <ListView
+                        android:id="@+id/albums"
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:clipToPadding="false"
+                        android:fastScrollEnabled="true"
+                        android:paddingBottom="@dimen/listview_bottom_padding"
+                        android:paddingLeft="20dp"
+                        android:paddingRight="20dp"
+                        android:nextFocusUp="@+id/ml_menu_search"
+                        android:nextFocusDown="@id/albums"
+                        android:nextFocusLeft="@id/albums"
+                        android:nextFocusRight="@id/albums" />
+
+                    <ListView
+                        android:id="@+id/songs"
+                        android:layout_width="match_parent"
+                        android:layout_height="match_parent"
+                        android:clipToPadding="false"
+                        android:fastScrollEnabled="true"
+                        android:paddingBottom="@dimen/listview_bottom_padding"
+                        android:paddingLeft="20dp"
+                        android:paddingRight="20dp"
+                        android:nextFocusUp="@+id/ml_menu_search"
+                        android:nextFocusDown="@id/songs"
+                        android:nextFocusLeft="@id/songs"
+                        android:nextFocusRight="@id/songs" />
+                </org.videolan.vlc.widget.FlingViewGroup>
+            </android.support.v4.widget.SwipeRefreshLayout>
         </FrameLayout>
     </LinearLayout>
 
diff --git a/vlc-android/res/layout/audio_browser.xml b/vlc-android/res/layout/audio_browser.xml
index 6f9bfc1..27ef224 100644
--- a/vlc-android/res/layout/audio_browser.xml
+++ b/vlc-android/res/layout/audio_browser.xml
@@ -8,75 +8,75 @@
         android:layout_width="fill_parent"
         android:layout_height="wrap_content">
         <org.videolan.vlc.widget.HeaderScrollView
-	        android:id="@+id/header"
-	        android:scrollbars="none"
-	        android:fadingEdge="none"
-	        android:layout_width="fill_parent"
-	        android:layout_height="wrap_content"
-	        android:background="@drawable/background_header_item"
-	        android:nextFocusUp="@+id/ml_menu_search"
-	        android:nextFocusDown="@id/header"
-	        android:nextFocusLeft="@id/header"
-	        android:nextFocusRight="@id/header" >
-	        <LinearLayout
-	            android:id="@+id/header_layout"
-	            android:layout_width="wrap_content"
-	            android:layout_height="wrap_content"
-	            android:orientation="horizontal"
+            android:id="@+id/header"
+            android:scrollbars="none"
+            android:fadingEdge="none"
+            android:layout_width="fill_parent"
+            android:layout_height="wrap_content"
+            android:background="@drawable/background_header_item"
+            android:nextFocusUp="@+id/ml_menu_search"
+            android:nextFocusDown="@id/header"
+            android:nextFocusLeft="@id/header"
+            android:nextFocusRight="@id/header" >
+            <LinearLayout
+                android:id="@+id/header_layout"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:orientation="horizontal"
                 android:paddingTop="5sp"
                 android:paddingBottom="5sp" >
                 <TextView
-	                android:text="@string/artists"
-	                android:textAllCaps="true"
-	                android:textSize="16sp"
-	                android:textColor="?attr/font_light"
-	                android:id="@+id/artists"
-	                android:layout_width="80sp"
-	                android:layout_height="wrap_content"
-	                android:gravity="center_horizontal" />
-	            <TextView
-	                android:text="@string/albums"
-	                android:textAllCaps="true"
-	                android:textSize="16sp"
-	                android:textColor="?attr/font_light"
-	                android:id="@+id/albums"
-	                android:layout_width="80sp"
-	                android:layout_height="wrap_content"
-	                android:gravity="center_horizontal" />
-	            <TextView
-	                android:text="@string/songs"
-	                android:textAllCaps="true"
-	                android:textSize="16sp"
-	                android:textColor="?attr/font_light"
-	                android:id="@+id/songs"
-	                android:layout_width="80sp"
-	                android:layout_height="wrap_content"
-	                android:gravity="center_horizontal" />
-	            <TextView
-	                android:text="@string/genres"
-	                android:textAllCaps="true"
-	                android:textSize="16sp"
-	                android:textColor="?attr/font_light"
-	                android:id="@+id/genres"
-	                android:layout_width="80sp"
-	                android:layout_height="wrap_content"
-	                android:gravity="center_horizontal" />
-	        </LinearLayout>
-	    </org.videolan.vlc.widget.HeaderScrollView>
-	    <View
-	        android:layout_width="20dip"
-	        android:layout_height="10dip"
-	        android:layout_alignBottom="@+id/header"
-	        android:layout_alignLeft="@+id/header"
-	        android:layout_alignTop="@+id/header"
-	        android:background="?attr/header_fadein_overlay" />
-	    <View
-	        android:layout_width="20dip"
-	        android:layout_height="10dip"
-	        android:layout_alignBottom="@+id/header"
-	        android:layout_alignRight="@+id/header"
-	        android:layout_alignTop="@+id/header"
-	        android:background="?attr/header_fadeout_overlay" />
+                    android:text="@string/artists"
+                    android:textAllCaps="true"
+                    android:textSize="16sp"
+                    android:textColor="?attr/font_light"
+                    android:id="@+id/artists"
+                    android:layout_width="80sp"
+                    android:layout_height="wrap_content"
+                    android:gravity="center_horizontal" />
+                <TextView
+                    android:text="@string/albums"
+                    android:textAllCaps="true"
+                    android:textSize="16sp"
+                    android:textColor="?attr/font_light"
+                    android:id="@+id/albums"
+                    android:layout_width="80sp"
+                    android:layout_height="wrap_content"
+                    android:gravity="center_horizontal" />
+                <TextView
+                    android:text="@string/songs"
+                    android:textAllCaps="true"
+                    android:textSize="16sp"
+                    android:textColor="?attr/font_light"
+                    android:id="@+id/songs"
+                    android:layout_width="80sp"
+                    android:layout_height="wrap_content"
+                    android:gravity="center_horizontal" />
+                <TextView
+                    android:text="@string/genres"
+                    android:textAllCaps="true"
+                    android:textSize="16sp"
+                    android:textColor="?attr/font_light"
+                    android:id="@+id/genres"
+                    android:layout_width="80sp"
+                    android:layout_height="wrap_content"
+                    android:gravity="center_horizontal" />
+            </LinearLayout>
+        </org.videolan.vlc.widget.HeaderScrollView>
+        <View
+            android:layout_width="20dip"
+            android:layout_height="10dip"
+            android:layout_alignBottom="@+id/header"
+            android:layout_alignLeft="@+id/header"
+            android:layout_alignTop="@+id/header"
+            android:background="?attr/header_fadein_overlay" />
+        <View
+            android:layout_width="20dip"
+            android:layout_height="10dip"
+            android:layout_alignBottom="@+id/header"
+            android:layout_alignRight="@+id/header"
+            android:layout_alignTop="@+id/header"
+            android:background="?attr/header_fadeout_overlay" />
     </RelativeLayout>
 
     <TextView
@@ -88,66 +88,71 @@
         android:textSize="20sp"
         android:visibility="gone" />
 
-    <org.videolan.vlc.widget.FlingViewGroup
-        android:id="@+id/content"
-        android:layout_width="fill_parent"
-        android:layout_height="fill_parent" >
+    <android.support.v4.widget.SwipeRefreshLayout
+        android:id="@+id/swipeLayout"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+        <org.videolan.vlc.widget.FlingViewGroup
+            android:id="@+id/content"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent" >
 
-        <ListView
-            android:id="@+id/artists_list"
-            android:layout_width="fill_parent"
-            android:layout_height="fill_parent"
-            android:paddingLeft="20dp"
-            android:paddingRight="20dp"
-            android:fastScrollEnabled="true"
-            android:paddingBottom="@dimen/listview_bottom_padding"
-            android:clipToPadding="false"
-            android:focusable="true"
-            android:nextFocusUp="@id/header"
-            android:nextFocusDown="@id/header"
-            android:nextFocusLeft="@id/artists_list"
-            android:nextFocusRight="@+id/albums_list" />
-        <ListView
-            android:id="@+id/albums_list"
-            android:layout_width="fill_parent"
-            android:layout_height="fill_parent"
-            android:paddingLeft="20dp"
-            android:paddingRight="20dp"
-            android:fastScrollEnabled="true"
-            android:paddingBottom="@dimen/listview_bottom_padding"
-            android:clipToPadding="false"
-            android:focusable="true"
-            android:nextFocusUp="@id/header"
-            android:nextFocusDown="@id/header"
-            android:nextFocusLeft="@id/artists_list"
-            android:nextFocusRight="@+id/songs_list" />
-       <ListView
-            android:id="@+id/songs_list"
-            android:layout_width="fill_parent"
-            android:layout_height="fill_parent"
-            android:paddingLeft="20dp"
-            android:paddingRight="20dp"
-            android:fastScrollEnabled="true"
-            android:paddingBottom="@dimen/listview_bottom_padding"
-            android:clipToPadding="false"
-            android:focusable="true"
-            android:nextFocusUp="@id/header"
-            android:nextFocusDown="@id/header"
-            android:nextFocusLeft="@id/albums_list"
-            android:nextFocusRight="@+id/genres_list" />
-        <ListView
-            android:id="@+id/genres_list"
-            android:layout_width="fill_parent"
-            android:layout_height="fill_parent"
-            android:paddingLeft="20dp"
-            android:paddingRight="20dp"
-            android:fastScrollEnabled="true"
-            android:paddingBottom="@dimen/listview_bottom_padding"
-            android:clipToPadding="false"
-            android:focusable="true"
-            android:nextFocusUp="@id/header"
-            android:nextFocusDown="@id/header"
-            android:nextFocusLeft="@id/songs_list"
-            android:nextFocusRight="@+id/genres_list" />
-    </org.videolan.vlc.widget.FlingViewGroup>
+            <ListView
+                android:id="@+id/artists_list"
+                android:layout_width="fill_parent"
+                android:layout_height="fill_parent"
+                android:paddingLeft="20dp"
+                android:paddingRight="20dp"
+                android:fastScrollEnabled="true"
+                android:paddingBottom="@dimen/listview_bottom_padding"
+                android:clipToPadding="false"
+                android:focusable="true"
+                android:nextFocusUp="@id/header"
+                android:nextFocusDown="@id/header"
+                android:nextFocusLeft="@id/artists_list"
+                android:nextFocusRight="@+id/albums_list" />
+            <ListView
+                android:id="@+id/albums_list"
+                android:layout_width="fill_parent"
+                android:layout_height="fill_parent"
+                android:paddingLeft="20dp"
+                android:paddingRight="20dp"
+                android:fastScrollEnabled="true"
+                android:paddingBottom="@dimen/listview_bottom_padding"
+                android:clipToPadding="false"
+                android:focusable="true"
+                android:nextFocusUp="@id/header"
+                android:nextFocusDown="@id/header"
+                android:nextFocusLeft="@id/artists_list"
+                android:nextFocusRight="@+id/songs_list" />
+            <ListView
+                android:id="@+id/songs_list"
+                android:layout_width="fill_parent"
+                android:layout_height="fill_parent"
+                android:paddingLeft="20dp"
+                android:paddingRight="20dp"
+                android:fastScrollEnabled="true"
+                android:paddingBottom="@dimen/listview_bottom_padding"
+                android:clipToPadding="false"
+                android:focusable="true"
+                android:nextFocusUp="@id/header"
+                android:nextFocusDown="@id/header"
+                android:nextFocusLeft="@id/albums_list"
+                android:nextFocusRight="@+id/genres_list" />
+            <ListView
+                android:id="@+id/genres_list"
+                android:layout_width="fill_parent"
+                android:layout_height="fill_parent"
+                android:paddingLeft="20dp"
+                android:paddingRight="20dp"
+                android:fastScrollEnabled="true"
+                android:paddingBottom="@dimen/listview_bottom_padding"
+                android:clipToPadding="false"
+                android:focusable="true"
+                android:nextFocusUp="@id/header"
+                android:nextFocusDown="@id/header"
+                android:nextFocusLeft="@id/songs_list"
+                android:nextFocusRight="@+id/genres_list" />
+        </org.videolan.vlc.widget.FlingViewGroup>
+    </android.support.v4.widget.SwipeRefreshLayout>
 </LinearLayout>
diff --git a/vlc-android/res/layout/directory_view.xml b/vlc-android/res/layout/directory_view.xml
index f798b3d..0a9aef0 100644
--- a/vlc-android/res/layout/directory_view.xml
+++ b/vlc-android/res/layout/directory_view.xml
@@ -1,11 +1,15 @@
 <?xml version="1.0" encoding="utf-8"?>
-<ListView xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@android:id/list"
+<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/swipeLayout"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:paddingBottom="@dimen/listview_bottom_padding"
-    android:clipToPadding="false"
-    android:divider="?attr/background_menu_divider"
-    android:dividerHeight="0.1dp" >
-
-</ListView>
+    android:layout_height="match_parent">
+    <ListView
+        android:id="@android:id/list"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:paddingBottom="@dimen/listview_bottom_padding"
+        android:clipToPadding="false"
+        android:divider="?attr/background_menu_divider"
+        android:dividerHeight="0.1dp" >
+    </ListView>
+</android.support.v4.widget.SwipeRefreshLayout>
diff --git a/vlc-android/res/layout/history_list.xml b/vlc-android/res/layout/history_list.xml
index 3369b83..e6aff9d 100644
--- a/vlc-android/res/layout/history_list.xml
+++ b/vlc-android/res/layout/history_list.xml
@@ -4,22 +4,26 @@
     android:layout_height="match_parent"
     android:orientation="vertical" >
 
-    <ListView
-        android:id="@android:id/list"
+    <android.support.v4.widget.SwipeRefreshLayout
+        android:id="@+id/swipeLayout"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:clipToPadding="false"
-        android:paddingBottom="@dimen/listview_bottom_padding"
-        android:paddingLeft="20dp"
-        android:paddingRight="20dp" />
-
-    <TextView
-        android:id="@android:id/empty"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:gravity="center_horizontal"
-        android:paddingTop="30dip"
-        android:text="@string/nohistory"
-        android:textSize="20sp" />
+        android:layout_height="match_parent">
+        <ListView
+            android:id="@android:id/list"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:clipToPadding="false"
+            android:paddingBottom="@dimen/listview_bottom_padding"
+            android:paddingLeft="20dp"
+            android:paddingRight="20dp" />
 
+        <TextView
+            android:id="@android:id/empty"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:gravity="center_horizontal"
+            android:paddingTop="30dip"
+            android:text="@string/nohistory"
+            android:textSize="20sp" />
+    </android.support.v4.widget.SwipeRefreshLayout>
 </LinearLayout>
\ No newline at end of file
diff --git a/vlc-android/res/layout/video_grid.xml b/vlc-android/res/layout/video_grid.xml
index b6e8e14..4bd3836 100644
--- a/vlc-android/res/layout/video_grid.xml
+++ b/vlc-android/res/layout/video_grid.xml
@@ -1,25 +1,30 @@
 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="fill_parent"
-    android:layout_height="fill_parent"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
     android:paddingTop="20dip">
 
-    <GridView
-        android:id="@id/android:list"
-        android:layout_width="fill_parent"
-        android:layout_height="fill_parent"
-        android:paddingTop="10dip"
-        android:paddingBottom="10dip"
-        android:clipToPadding="false"
-        android:scrollbarStyle="outsideInset"
-        android:numColumns="auto_fit"
-        android:fastScrollEnabled="true"
-        android:fadingEdge="none"
-        android:gravity="center"
-        android:nextFocusUp="@+id/ml_menu_search"
-        android:nextFocusDown="@id/android:list"
-        android:nextFocusLeft="@id/android:list"
-        android:nextFocusRight="@id/android:list" />
+    <android.support.v4.widget.SwipeRefreshLayout
+                android:id="@+id/swipeLayout"
+                android:layout_width="match_parent"
+                android:layout_height="match_parent">
+        <GridView
+            android:id="@id/android:list"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:paddingTop="10dip"
+            android:paddingBottom="10dip"
+            android:clipToPadding="false"
+            android:scrollbarStyle="outsideInset"
+            android:numColumns="auto_fit"
+            android:fastScrollEnabled="true"
+            android:fadingEdge="none"
+            android:gravity="center"
+            android:nextFocusUp="@+id/ml_menu_search"
+            android:nextFocusDown="@id/android:list"
+            android:nextFocusLeft="@id/android:list"
+            android:nextFocusRight="@id/android:list" />
+    </android.support.v4.widget.SwipeRefreshLayout>
 
     <LinearLayout
         android:id="@id/android:empty"
diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
index b46096d..0743533 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
@@ -42,6 +42,7 @@ import android.content.IntentFilter;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.ListFragment;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.widget.PopupMenu;
 import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;
@@ -52,15 +53,17 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AbsListView;
 import android.widget.AdapterView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.AdapterView.OnItemLongClickListener;
 import android.widget.ListView;
 
-public class DirectoryViewFragment extends ListFragment implements IRefreshable, ISortable {
+public class DirectoryViewFragment extends ListFragment implements IRefreshable, ISortable, SwipeRefreshLayout.OnRefreshListener {
     public final static String TAG = "VLC/DirectoryViewFragment";
 
     private DirectoryAdapter mDirectoryAdapter;
+    private SwipeRefreshLayout mSwipeRefreshLayout;
 
     /* All subclasses of Fragment must include a public empty constructor. */
     public DirectoryViewFragment() { }
@@ -119,6 +122,19 @@ public class DirectoryViewFragment extends ListFragment implements IRefreshable,
                 }
             }
         });
+        mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeLayout);
+
+        mSwipeRefreshLayout.setColorSchemeResources(R.color.darkerorange);
+        mSwipeRefreshLayout.setOnRefreshListener(this);
+
+        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
+            @Override
+            public void onScrollStateChanged(AbsListView view, int scrollState) {}
+            @Override
+            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
+                mSwipeRefreshLayout.setEnabled(firstVisibleItem == 0);
+            }
+        });
 
         registerForContextMenu(listView);
         return v;
@@ -230,6 +246,7 @@ public class DirectoryViewFragment extends ListFragment implements IRefreshable,
             focusHelper(mDirectoryAdapter.getCount() == 0);
         } else
             focusHelper(true);
+        mSwipeRefreshLayout.setRefreshing(false);
     }
 
     private final BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@@ -270,4 +287,9 @@ public class DirectoryViewFragment extends ListFragment implements IRefreshable,
             }
 
     };
+
+    @Override
+    public void onRefresh() {
+        refresh();
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
index a9d0fe3..f7dfc28 100644
--- a/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/HistoryFragment.java
@@ -28,8 +28,8 @@ import org.videolan.vlc.interfaces.IRefreshable;
 
 import android.os.Bundle;
 import android.support.v4.app.ListFragment;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBarActivity;
-import android.util.Log;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
@@ -37,13 +37,15 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AbsListView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.ListView;
 
-public class HistoryFragment extends ListFragment implements IRefreshable {
+public class HistoryFragment extends ListFragment implements IRefreshable, SwipeRefreshLayout.OnRefreshListener {
     public final static String TAG = "VLC/HistoryFragment";
 
     private HistoryAdapter mHistoryAdapter;
+    private SwipeRefreshLayout mSwipeRefreshLayout;
 
     /* All subclasses of Fragment must include a public empty constructor. */
     public HistoryFragment() { }
@@ -53,7 +55,6 @@ public class HistoryFragment extends ListFragment implements IRefreshable {
         super.onCreate(savedInstanceState);
 
         mHistoryAdapter = new HistoryAdapter(getActivity());
-        Log.d(TAG, "HistoryFragment()");
     }
 
     private void focusHelper(boolean idIsEmpty) {
@@ -81,6 +82,20 @@ public class HistoryFragment extends ListFragment implements IRefreshable {
         focusHelper(mHistoryAdapter.getCount() == 0);
         listView.requestFocus();
         registerForContextMenu(listView);
+
+        mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeLayout);
+
+        mSwipeRefreshLayout.setColorSchemeColors(R.color.darkerorange/*, R.attr.colorPrimary, R.attr.colorPrimaryDark*/);
+        mSwipeRefreshLayout.setOnRefreshListener(this);
+
+        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
+            @Override
+            public void onScrollStateChanged(AbsListView view, int scrollState) {}
+            @Override
+            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
+                mSwipeRefreshLayout.setEnabled(firstVisibleItem == 0);
+            }
+        });
         return v;
     }
 
@@ -124,11 +139,16 @@ public class HistoryFragment extends ListFragment implements IRefreshable {
 
     @Override
     public void refresh() {
-        Log.d(TAG, "Refreshing view!");
         if( mHistoryAdapter != null ) {
             mHistoryAdapter.refresh();
             focusHelper(mHistoryAdapter.getCount() == 0);
         } else
             focusHelper(true);
+        mSwipeRefreshLayout.setRefreshing(false);
+    }
+
+    @Override
+    public void onRefresh() {
+        refresh();
     }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
index 5197579..075b92d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
@@ -42,6 +42,7 @@ import android.content.Context;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBarActivity;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
@@ -51,6 +52,7 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AbsListView;
 import android.widget.AdapterView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.AdapterView.OnItemClickListener;
@@ -63,7 +65,7 @@ import android.widget.TabHost;
 import android.widget.TabHost.TabSpec;
 import android.widget.TextView;
 
-public class AudioAlbumsSongsFragment extends Fragment {
+public class AudioAlbumsSongsFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
 
     public final static String TAG = "VLC/AudioAlbumsSongsFragment";
 
@@ -72,6 +74,7 @@ public class AudioAlbumsSongsFragment extends Fragment {
 
     private AudioBrowserListAdapter mSongsAdapter;
     private AudioBrowserListAdapter mAlbumsAdapter;
+    private SwipeRefreshLayout mSwipeRefreshLayout;
 
     public final static String EXTRA_NAME = "name";
     public final static String EXTRA_NAME2 = "name2";
@@ -167,9 +170,32 @@ public class AudioAlbumsSongsFragment extends Fragment {
             }
         });
 
+        mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeLayout);
+
+        mSwipeRefreshLayout.setColorSchemeResources(R.color.darkerorange);
+        mSwipeRefreshLayout.setOnRefreshListener(this);
+
+        songsList.setOnScrollListener(mScrollListener);
+        albumsList.setOnScrollListener(mScrollListener);
+
         return v;
     }
 
+    AbsListView.OnScrollListener mScrollListener = new AbsListView.OnScrollListener(){
+            @Override
+            public void onScrollStateChanged(AbsListView view, int scrollState) {}
+            @Override
+            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
+                                 int totalItemCount) {
+                mSwipeRefreshLayout.setEnabled(firstVisibleItem == 0);
+            }
+    };
+
+    @Override
+    public void onRefresh() {
+        updateList();
+    }
+
     private static class DummyContentFactory implements TabHost.TabContentFactory {
         private final Context mContext;
         public DummyContentFactory(Context ctx) {
@@ -336,6 +362,7 @@ public class AudioAlbumsSongsFragment extends Fragment {
                         }
                         mAlbumsAdapter.notifyDataSetChanged();
                         mSongsAdapter.notifyDataSetChanged();
+                        mSwipeRefreshLayout.setRefreshing(false);
                     }
                 });
             }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index b9524f0..fd223f4 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -28,6 +28,7 @@ import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.support.v4.app.Fragment;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBarActivity;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
@@ -40,6 +41,7 @@ import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnTouchListener;
 import android.view.ViewGroup;
+import android.widget.AbsListView;
 import android.widget.AdapterView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.AdapterView.OnItemClickListener;
@@ -67,10 +69,11 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
-public class AudioBrowserFragment extends Fragment {
+public class AudioBrowserFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
     public final static String TAG = "VLC/AudioBrowserFragment";
 
     private FlingViewGroup mFlingViewGroup;
+    private SwipeRefreshLayout mSwipeRefreshLayout;
     private int mFlingViewPosition = 0;
 
     private HeaderScrollView mHeader;
@@ -159,9 +162,29 @@ public class AudioBrowserFragment extends Fragment {
         registerForContextMenu(albumList);
         registerForContextMenu(genreList);
 
+        mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeLayout);
+
+        mSwipeRefreshLayout.setColorSchemeResources(R.color.darkerorange);
+        mSwipeRefreshLayout.setOnRefreshListener(this);
+
+        songsList.setOnScrollListener(mScrollListener);
+        artistList.setOnScrollListener(mScrollListener);
+        albumList.setOnScrollListener(mScrollListener);
+        genreList.setOnScrollListener(mScrollListener);
+
         return v;
     }
 
+    AbsListView.OnScrollListener mScrollListener = new AbsListView.OnScrollListener(){
+            @Override
+            public void onScrollStateChanged(AbsListView view, int scrollState) {}
+            @Override
+            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
+                                 int totalItemCount) {
+                mSwipeRefreshLayout.setEnabled(firstVisibleItem == 0);
+            }
+    };
+
     @Override
     public void onPause() {
         super.onPause();
@@ -442,6 +465,11 @@ public class AudioBrowserFragment extends Fragment {
      */
     private Handler mHandler = new AudioBrowserHandler(this);
 
+    @Override
+    public void onRefresh() {
+        updateLists();
+    }
+
     private static class AudioBrowserHandler extends WeakHandler<AudioBrowserFragment> {
         public AudioBrowserHandler(AudioBrowserFragment owner) {
             super(owner);
@@ -503,6 +531,7 @@ public class AudioBrowserFragment extends Fragment {
                                 }
                             }
                             focusHelper(false, R.id.artists_list);
+                            mSwipeRefreshLayout.setRefreshing(false);
                         }
                     });
                 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 021599c..49a98f6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -53,6 +53,7 @@ import android.os.Bundle;
 import android.os.Handler;
 import android.preference.PreferenceManager;
 import android.support.v4.app.FragmentActivity;
+import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.ActionBarActivity;
 import android.util.DisplayMetrics;
@@ -65,6 +66,7 @@ import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AbsListView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.GridView;
 import android.widget.LinearLayout;
@@ -72,7 +74,7 @@ import android.widget.PopupMenu;
 import android.widget.PopupMenu.OnMenuItemClickListener;
 import android.widget.TextView;
 
-public class VideoGridFragment extends SherlockGridFragment implements ISortable, VideoBrowserInterface {
+public class VideoGridFragment extends SherlockGridFragment implements ISortable, VideoBrowserInterface, SwipeRefreshLayout.OnRefreshListener {
 
     public final static String TAG = "VLC/VideoListFragment";
 
@@ -96,6 +98,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
     private MediaLibrary mMediaLibrary;
     private Thumbnailer mThumbnailer;
     private VideoGridAnimator mAnimator;
+    private SwipeRefreshLayout mSwipeRefreshLayout;
 
     private AudioServiceController mAudioController;
 
@@ -136,7 +139,19 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         mLayoutFlipperLoading = (LinearLayout) v.findViewById(R.id.layout_flipper_loading);
         mTextViewNomedia = (TextView) v.findViewById(R.id.textview_nomedia);
         mGridView = (GridView) v.findViewById(android.R.id.list);
+        mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipeLayout);
 
+        mSwipeRefreshLayout.setColorSchemeResources(R.color.darkerorange);
+        mSwipeRefreshLayout.setOnRefreshListener(this);
+
+        mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
+            @Override
+            public void onScrollStateChanged(AbsListView view, int scrollState) {}
+            @Override
+            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
+                mSwipeRefreshLayout.setEnabled(firstVisibleItem == 0);
+            }
+        });
         return v;
     }
 
@@ -380,6 +395,8 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         }
 
     public void updateList() {
+        if (!mSwipeRefreshLayout.isRefreshing())
+            mSwipeRefreshLayout.setRefreshing(true);
         List<Media> itemList = mMediaLibrary.getVideoItems();
 
         if (mThumbnailer != null)
@@ -414,6 +431,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
             focusHelper(false);
         } else
             focusHelper(true);
+        stopRefresh();
     }
 
     @Override
@@ -452,4 +470,13 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
             }
         }
     };
+
+    public void stopRefresh() {
+        mSwipeRefreshLayout.setRefreshing(false);
+    }
+
+    @Override
+    public void onRefresh() {
+        updateList();
+    }
 }
-- 
1.9.1




More information about the Android mailing list