[Android] Audio browser: define a new widget for the scrolling header

Adrien Maglo git at videolan.org
Tue Nov 26 09:00:58 CET 2013


vlc-ports/android | branch: master | Adrien Maglo <magsoft at videolan.org> | Tue Nov 26 08:54:48 2013 +0100| [bbabe07b1f7baaa14aa90dbe4008fde5fc536623] | committer: Adrien Maglo

Audio browser: define a new widget for the scrolling header

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

 .../org/videolan/vlc/widget/HeaderScrollView.java  |  107 ++++++++++++++++++++
 1 file changed, 107 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/widget/HeaderScrollView.java b/vlc-android/src/org/videolan/vlc/widget/HeaderScrollView.java
new file mode 100644
index 0000000..253f711
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/widget/HeaderScrollView.java
@@ -0,0 +1,107 @@
+/*****************************************************************************
+ * AudioBrowserActivity.java
+ *****************************************************************************
+ * Copyright © 2011-2013 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+package org.videolan.vlc.widget;
+
+import org.videolan.vlc.R;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.HorizontalScrollView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+public class HeaderScrollView extends HorizontalScrollView {
+
+    private int mTabWidth;
+    private float mProgress = 0;
+    public final static int MODE_TOTAL = 4;
+
+    public HeaderScrollView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+
+        addOnLayoutChangeListener(new OnLayoutChangeListener() {
+            @Override
+            public void onLayoutChange(View v, int left, int top, int right, int bottom,
+                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
+                restoreScroll();
+            }
+        });
+    }
+
+    @Override
+    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+        super.onSizeChanged(w, h, oldw, oldh);
+
+        mTabWidth = w / 2;
+
+        post(new Runnable() {
+            @Override
+            public void run() {
+                LinearLayout hl = (LinearLayout) findViewById(R.id.header_layout);
+                for (int i = 0; i < hl.getChildCount(); ++i) {
+                    View t = (View)hl.getChildAt(i);
+                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mTabWidth, LayoutParams.WRAP_CONTENT, 1);
+                    if (i == 0)
+                        lp.setMargins(mTabWidth / 2, 0, 0, 0);
+                    else if (i == hl.getChildCount() - 1)
+                        lp.setMargins(0, 0, mTabWidth / 2, 0);
+                    t.setLayoutParams(lp);
+                }
+                requestLayout();
+            }
+        });
+    }
+
+    public void scroll(float progress) {
+        mProgress = progress;
+        restoreScroll();
+    }
+
+    private void restoreScroll() {
+        /*
+         * How progress works:
+         * |------|------|------|
+         * 0     1/3    2/3     1
+         *
+         * To calculate the "progress" of a particular tab, one can use this
+         * formula:
+         *
+         * <tab beginning with 0> * (1 / (total tabs - 1))
+         */
+        int x = (int) (mProgress * (MODE_TOTAL - 1) * mTabWidth);
+        smoothScrollTo(x, 0);
+    }
+
+    public void highlightTab(int existingPosition, int newPosition) {
+        LinearLayout hl = (LinearLayout) findViewById(R.id.header_layout);
+        TypedArray attrs = getContext().obtainStyledAttributes(new int[] { R.attr.font_light, R.attr.font_default});
+        if (hl == null)
+            return;
+        TextView oldView = (TextView) hl.getChildAt(existingPosition);
+        if (oldView != null)
+            oldView.setTextColor(attrs.getColor(0, 0));
+        TextView newView = (TextView) hl.getChildAt(newPosition);
+        if (newView != null)
+            newView.setTextColor(attrs.getColor(1, 0));
+    }
+}



More information about the Android mailing list