Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import com.android.browser.view.HorizontalScrollView; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.database.DataSetObserver; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.MotionEvent; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.BaseAdapter; |
| 28 | import android.widget.LinearLayout; |
| 29 | |
| 30 | /** |
| 31 | * custom view for displaying tabs in the nav screen |
| 32 | */ |
| 33 | public class NavTabScroller extends HorizontalScrollView { |
| 34 | |
| 35 | private static final float DEFAULT_ALPHA = 0.5f; |
| 36 | |
| 37 | private LinearLayout mContentView; |
| 38 | private int mSelected; |
| 39 | private BaseAdapter mAdapter; |
| 40 | private boolean mSnapScroll; |
| 41 | |
| 42 | public NavTabScroller(Context context, AttributeSet attrs, int defStyle) { |
| 43 | super(context, attrs, defStyle); |
| 44 | init(context); |
| 45 | } |
| 46 | |
| 47 | public NavTabScroller(Context context, AttributeSet attrs) { |
| 48 | super(context, attrs); |
| 49 | init(context); |
| 50 | } |
| 51 | |
| 52 | public NavTabScroller(Context context) { |
| 53 | super(context); |
| 54 | init(context); |
| 55 | } |
| 56 | |
| 57 | private void init(Context ctx) { |
| 58 | setHorizontalScrollBarEnabled(false); |
| 59 | mContentView = new LinearLayout(ctx); |
| 60 | mContentView.setOrientation(LinearLayout.HORIZONTAL); |
| 61 | int pad = ctx.getResources().getDimensionPixelSize(R.dimen.nav_scroller_padding); |
| 62 | mContentView.setPadding(pad, 0, pad, 0); |
| 63 | mContentView.setLayoutParams( |
| 64 | new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); |
| 65 | addView(mContentView); |
| 66 | mSelected = -1; |
| 67 | } |
| 68 | |
| 69 | protected void setAdapter(BaseAdapter adapter) { |
| 70 | mAdapter = adapter; |
| 71 | mAdapter.registerDataSetObserver(new DataSetObserver() { |
| 72 | |
| 73 | @Override |
| 74 | public void onChanged() { |
| 75 | super.onChanged(); |
| 76 | populateList(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void onInvalidated() { |
| 81 | super.onInvalidated(); |
| 82 | } |
| 83 | }); |
| 84 | populateList(); |
| 85 | } |
| 86 | |
| 87 | protected void setSelection(int ix) { |
| 88 | mSelected = ix; |
| 89 | updateViewAlpha(); |
| 90 | } |
| 91 | |
| 92 | private void updateViewAlpha() { |
| 93 | final int n = mContentView.getChildCount(); |
| 94 | for (int i = 0; i < n; i ++) { |
| 95 | View v = mContentView.getChildAt(i); |
| 96 | v.setAlpha((i == mSelected) ? 1.0f : DEFAULT_ALPHA); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | protected int getSelectionIndex() { |
| 101 | return mSelected; |
| 102 | } |
| 103 | |
| 104 | protected Tab getSelectedItem() { |
| 105 | return (Tab) mAdapter.getItem(mSelected); |
| 106 | } |
| 107 | |
| 108 | protected ViewGroup getContentView() { |
| 109 | return mContentView; |
| 110 | } |
| 111 | |
| 112 | private void populateList() { |
| 113 | clearTabs(); |
| 114 | for (int i = 0; i < mAdapter.getCount(); i++) { |
| 115 | View v = mAdapter.getView(i, null, mContentView); |
| 116 | mContentView.addView(v); |
| 117 | v.setAlpha((i == mSelected) ? 1.0f : DEFAULT_ALPHA); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | View getSelectedTab() { |
| 122 | if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) { |
| 123 | return mContentView.getChildAt(mSelected); |
| 124 | } else { |
| 125 | return null; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void clearTabs() { |
| 130 | for (int i = 0; i < mContentView.getChildCount(); i++) { |
| 131 | ViewGroup vg = (ViewGroup) mContentView.getChildAt(i); |
| 132 | vg.removeViewAt(0); |
| 133 | } |
| 134 | mContentView.removeAllViews(); |
| 135 | } |
| 136 | |
| 137 | protected void onScrollChanged(int sl, int st, int ol, int ot) { |
| 138 | int midx = getScrollX() + getWidth() / 2; |
| 139 | int sel = -1; |
| 140 | for (int i = 0; i < mContentView.getChildCount(); i++) { |
| 141 | View child = mContentView.getChildAt(i); |
| 142 | if (child.getLeft() < midx && child.getRight() > midx) { |
| 143 | sel = i; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | if (sel != -1 && sel != mSelected) { |
| 148 | setSelection(sel); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean onTouchEvent(MotionEvent evt) { |
| 154 | boolean dragged = mIsBeingDragged; |
| 155 | boolean result = super.onTouchEvent(evt); |
| 156 | if (MotionEvent.ACTION_UP == evt.getActionMasked()) { |
| 157 | if (mScroller.isFinished() && dragged) { |
| 158 | snapToSelected(); |
| 159 | } |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void computeScroll() { |
| 166 | super.computeScroll(); |
| 167 | if (mScroller.isFinished() && !mIsBeingDragged) { |
| 168 | if (!mSnapScroll) { |
| 169 | snapToSelected(); |
| 170 | } else { |
| 171 | // reset snap scrolling flag |
| 172 | mSnapScroll = false; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private void snapToSelected() { |
| 178 | // snap to selected |
| 179 | mSnapScroll = true; |
| 180 | View v = mContentView.getChildAt(mSelected); |
| 181 | int left = (v.getLeft() + v.getRight()) / 2; |
| 182 | left -= getWidth() / 2; |
| 183 | scrollTo(left,0); |
| 184 | } |
| 185 | |
| 186 | } |