[Android] Audio browser: allow to slide left and right to change the current view on the albums songs fragment

Adrien Maglo git at videolan.org
Wed Jan 29 19:52:49 CET 2014


vlc-ports/android | branch: master | Adrien Maglo <magsoft at videolan.org> | Wed Jan 29 19:52:42 2014 +0100| [04126d263305c66949a3680c66abb2838999b0a2] | committer: Adrien Maglo

Audio browser: allow to slide left and right to change the current view on the albums songs fragment

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=04126d263305c66949a3680c66abb2838999b0a2
---

 vlc-android/res/layout/audio_albums_songs.xml      |   36 ++++++++-------
 .../vlc/gui/audio/AudioAlbumsSongsFragment.java    |   46 ++++++++++++++++++--
 .../org/videolan/vlc/widget/FlingViewGroup.java    |    7 +++
 3 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/vlc-android/res/layout/audio_albums_songs.xml b/vlc-android/res/layout/audio_albums_songs.xml
index 00c4d89..51692a8 100644
--- a/vlc-android/res/layout/audio_albums_songs.xml
+++ b/vlc-android/res/layout/audio_albums_songs.xml
@@ -19,26 +19,32 @@
 
         <FrameLayout
             android:id="@android:id/tabcontent"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent" >
+            android:layout_width="fill_parent"
+            android:layout_height="fill_parent" >
 
-            <ListView
-                android:id="@+id/albums"
+            <org.videolan.vlc.widget.FlingViewGroup
+                android:id="@+id/fling_view_group"
                 android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:fastScrollEnabled="true"
-                android:paddingLeft="20dp"
-                android:paddingRight="20dp" />
+                android:layout_height="match_parent" >
 
-            <ListView
-                android:id="@+id/songs"
-                android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:fastScrollEnabled="true"
-                android:paddingLeft="20dp"
-                android:paddingRight="20dp" />
+                <ListView
+                    android:id="@+id/albums"
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"
+                    android:fastScrollEnabled="true"
+                    android:paddingLeft="20dp"
+                    android:paddingRight="20dp" />
 
+                <ListView
+                    android:id="@+id/songs"
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"
+                    android:fastScrollEnabled="true"
+                    android:paddingLeft="20dp"
+                    android:paddingRight="20dp" />
+            </org.videolan.vlc.widget.FlingViewGroup>
         </FrameLayout>
+
     </LinearLayout>
 
 </TabHost>
\ No newline at end of file
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 c644b00..512a46a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
@@ -31,6 +31,7 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.Util;
 import org.videolan.vlc.VlcRunnable;
 import org.videolan.vlc.gui.CommonDialogs;
+import org.videolan.vlc.widget.FlingViewGroup;
 
 import android.annotation.TargetApi;
 import android.app.AlertDialog;
@@ -39,6 +40,7 @@ import android.os.Build;
 import android.os.Bundle;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
+import android.view.DragEvent;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -77,6 +79,7 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
     private String mTitle;
 
     TabHost mTabHost;
+    FlingViewGroup mFlingViewGroup;
     private int mCurrentTab = 0;
 
     /* All subclasses of Fragment must include a public empty constructor. */
@@ -114,6 +117,7 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
         mTabHost = (TabHost) v.findViewById(android.R.id.tabhost);
         ListView albumsList = (ListView) v.findViewById(R.id.albums);
         ListView songsList = (ListView) v.findViewById(R.id.songs);
+        mFlingViewGroup = (FlingViewGroup) v.findViewById(R.id.fling_view_group);
 
         songsList.setAdapter(mSongsAdapter);
         albumsList.setAdapter(mAlbumsAdapter);
@@ -126,18 +130,52 @@ public class AudioAlbumsSongsFragment extends SherlockFragment {
 
         mTabHost.setup();
 
-        addNewTab(mTabHost, "albums", "Albums", R.id.albums);
-        addNewTab(mTabHost, "songs", "Songs", R.id.songs);
+        addNewTab(mTabHost, "albums", "Albums");
+        addNewTab(mTabHost, "songs", "Songs");
 
         mTabHost.setCurrentTab(mCurrentTab);
 
+        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
+            @Override
+            public void onTabChanged(String tabId) {
+                mCurrentTab = mTabHost.getCurrentTab();
+                mFlingViewGroup.smoothScrollTo(mCurrentTab);
+            }
+        });
+
+        mFlingViewGroup.setOnViewSwitchedListener(new FlingViewGroup.ViewSwitchListener() {
+            @Override
+            public void onSwitching(float progress) { }
+            @Override
+            public void onSwitched(int position) {
+                mTabHost.setCurrentTab(position);
+            }
+            @Override
+            public void onTouchDown() {}
+            @Override
+            public void onTouchUp() {}
+        });
+
         return v;
     }
 
-    private void addNewTab(TabHost tabHost, String tag, String title, int contentID) {
+    private class DummyContentFactory implements TabHost.TabContentFactory {
+        private final Context mContext;
+        public DummyContentFactory(Context ctx) {
+            mContext = ctx;
+        }
+        @Override
+        public View createTabContent(String tag) {
+            View dummy = new View(mContext);
+            return dummy;
+        }
+    }
+
+    private void addNewTab(TabHost tabHost, String tag, String title) {
+        DummyContentFactory dcf = new DummyContentFactory(tabHost.getContext());
         TabSpec tabSpec = tabHost.newTabSpec(tag);
         tabSpec.setIndicator(getNewTabIndicator(tabHost.getContext(), title));
-        tabSpec.setContent(contentID);
+        tabSpec.setContent(dcf);
         tabHost.addTab(tabSpec);
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/widget/FlingViewGroup.java b/vlc-android/src/org/videolan/vlc/widget/FlingViewGroup.java
index 756994b..c846b22 100644
--- a/vlc-android/src/org/videolan/vlc/widget/FlingViewGroup.java
+++ b/vlc-android/src/org/videolan/vlc/widget/FlingViewGroup.java
@@ -251,6 +251,13 @@ public class FlingViewGroup extends ViewGroup {
         invalidate();
     }
 
+    public void smoothScrollTo(int position) {
+        mCurrentView = position;
+        final int delta = (position * getWidth()) - getScrollX();
+        mScroller.startScroll(getScrollX(), 0, delta, 0, 300);
+        invalidate();
+    }
+
     public void setOnViewSwitchedListener(ViewSwitchListener l) {
         mViewSwitchListener = l;
     }



More information about the Android mailing list