Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23410 tejbeer 1
/*
2
 * Copyright (C) 2011 The Android Open Source Project
3
 * Copyright (C) 2011 Jake Wharton
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package com.viewpagerindicator;
18
 
19
import android.content.Context;
20
import android.support.v4.view.PagerAdapter;
21
import android.support.v4.view.ViewPager;
22
import android.support.v4.view.ViewPager.OnPageChangeListener;
23
import android.util.AttributeSet;
24
import android.view.View;
25
import android.view.ViewGroup;
26
import android.widget.HorizontalScrollView;
27
import android.widget.LinearLayout;
28
import android.widget.TextView;
29
 
30
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
31
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
32
 
33
/**
34
 * This widget implements the dynamic action bar tab behavior that can change
35
 * across different configurations or circumstances.
36
 */
37
public class TabPageIndicator extends HorizontalScrollView implements PageIndicator {
38
    /** Title text used when no title is provided by the adapter. */
39
    private static final CharSequence EMPTY_TITLE = "";
40
 
41
    /**
42
     * Interface for a callback when the selected tab has been reselected.
43
     */
44
    public interface OnTabReselectedListener {
45
        /**
46
         * Callback when the selected tab has been reselected.
47
         *
48
         * @param position Position of the current center item.
49
         */
50
        void onTabReselected(int position);
51
    }
52
 
53
    private Runnable mTabSelector;
54
 
55
    private final OnClickListener mTabClickListener = new OnClickListener() {
56
        public void onClick(View view) {
57
            TabView tabView = (TabView)view;
58
            final int oldSelected = mViewPager.getCurrentItem();
59
            final int newSelected = tabView.getIndex();
60
            mViewPager.setCurrentItem(newSelected);
61
            if (oldSelected == newSelected && mTabReselectedListener != null) {
62
                mTabReselectedListener.onTabReselected(newSelected);
63
            }
64
        }
65
    };
66
 
67
    private final IcsLinearLayout mTabLayout;
68
 
69
    private ViewPager mViewPager;
70
    private ViewPager.OnPageChangeListener mListener;
71
 
72
    private int mMaxTabWidth;
73
    private int mSelectedTabIndex;
74
 
75
    private OnTabReselectedListener mTabReselectedListener;
76
 
77
    public TabPageIndicator(Context context) {
78
        this(context, null);
79
    }
80
 
81
    public TabPageIndicator(Context context, AttributeSet attrs) {
82
        super(context, attrs);
83
        setHorizontalScrollBarEnabled(false);
84
 
85
        mTabLayout = new IcsLinearLayout(context, R.attr.vpiTabPageIndicatorStyle);
86
        addView(mTabLayout, new ViewGroup.LayoutParams(WRAP_CONTENT, MATCH_PARENT));
87
    }
88
 
89
    public void setOnTabReselectedListener(OnTabReselectedListener listener) {
90
        mTabReselectedListener = listener;
91
    }
92
 
93
    @Override
94
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
95
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
96
        final boolean lockedExpanded = widthMode == MeasureSpec.EXACTLY;
97
        setFillViewport(lockedExpanded);
98
 
99
        final int childCount = mTabLayout.getChildCount();
100
        if (childCount > 1 && (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST)) {
101
            if (childCount > 2) {
102
                mMaxTabWidth = (int)(MeasureSpec.getSize(widthMeasureSpec) * 0.4f);
103
            } else {
104
                mMaxTabWidth = MeasureSpec.getSize(widthMeasureSpec) / 2;
105
            }
106
        } else {
107
            mMaxTabWidth = -1;
108
        }
109
 
110
        final int oldWidth = getMeasuredWidth();
111
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
112
        final int newWidth = getMeasuredWidth();
113
 
114
        if (lockedExpanded && oldWidth != newWidth) {
115
            // Recenter the tab display if we're at a new (scrollable) size.
116
            setCurrentItem(mSelectedTabIndex);
117
        }
118
    }
119
 
120
    private void animateToTab(final int position) {
121
        final View tabView = mTabLayout.getChildAt(position);
122
        if (mTabSelector != null) {
123
            removeCallbacks(mTabSelector);
124
        }
125
        mTabSelector = new Runnable() {
126
            public void run() {
127
                final int scrollPos = tabView.getLeft() - (getWidth() - tabView.getWidth()) / 2;
128
                smoothScrollTo(scrollPos, 0);
129
                mTabSelector = null;
130
            }
131
        };
132
        post(mTabSelector);
133
    }
134
 
135
    @Override
136
    public void onAttachedToWindow() {
137
        super.onAttachedToWindow();
138
        if (mTabSelector != null) {
139
            // Re-post the selector we saved
140
            post(mTabSelector);
141
        }
142
    }
143
 
144
    @Override
145
    public void onDetachedFromWindow() {
146
        super.onDetachedFromWindow();
147
        if (mTabSelector != null) {
148
            removeCallbacks(mTabSelector);
149
        }
150
    }
151
 
152
    private void addTab(int index, CharSequence text, int iconResId) {
153
        final TabView tabView = new TabView(getContext());
154
        tabView.mIndex = index;
155
        tabView.setFocusable(true);
156
        tabView.setOnClickListener(mTabClickListener);
157
        tabView.setText(text);
158
 
159
        if (iconResId != 0) {
160
            tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
161
        }
162
 
163
        mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
164
    }
165
 
166
    @Override
167
    public void onPageScrollStateChanged(int arg0) {
168
        if (mListener != null) {
169
            mListener.onPageScrollStateChanged(arg0);
170
        }
171
    }
172
 
173
    @Override
174
    public void onPageScrolled(int arg0, float arg1, int arg2) {
175
        if (mListener != null) {
176
            mListener.onPageScrolled(arg0, arg1, arg2);
177
        }
178
    }
179
 
180
    @Override
181
    public void onPageSelected(int arg0) {
182
        setCurrentItem(arg0);
183
        if (mListener != null) {
184
            mListener.onPageSelected(arg0);
185
        }
186
    }
187
 
188
    @Override
189
    public void setViewPager(ViewPager view) {
190
        if (mViewPager == view) {
191
            return;
192
        }
193
        if (mViewPager != null) {
194
            mViewPager.setOnPageChangeListener(null);
195
        }
196
        final PagerAdapter adapter = view.getAdapter();
197
        if (adapter == null) {
198
            throw new IllegalStateException("ViewPager does not have adapter instance.");
199
        }
200
        mViewPager = view;
201
        view.setOnPageChangeListener(this);
202
        notifyDataSetChanged();
203
    }
204
 
205
    public void notifyDataSetChanged() {
206
        mTabLayout.removeAllViews();
207
        PagerAdapter adapter = mViewPager.getAdapter();
208
        IconPagerAdapter iconAdapter = null;
209
        if (adapter instanceof IconPagerAdapter) {
210
            iconAdapter = (IconPagerAdapter)adapter;
211
        }
212
        final int count = adapter.getCount();
213
        for (int i = 0; i < count; i++) {
214
            CharSequence title = adapter.getPageTitle(i);
215
            if (title == null) {
216
                title = EMPTY_TITLE;
217
            }
218
            int iconResId = 0;
219
            if (iconAdapter != null) {
220
                iconResId = iconAdapter.getIconResId(i);
221
            }
222
            addTab(i, title, iconResId);
223
        }
224
        if (mSelectedTabIndex > count) {
225
            mSelectedTabIndex = count - 1;
226
        }
227
        setCurrentItem(mSelectedTabIndex);
228
        requestLayout();
229
    }
230
 
231
    @Override
232
    public void setViewPager(ViewPager view, int initialPosition) {
233
        setViewPager(view);
234
        setCurrentItem(initialPosition);
235
    }
236
 
237
    @Override
238
    public void setCurrentItem(int item) {
239
        if (mViewPager == null) {
240
            throw new IllegalStateException("ViewPager has not been bound.");
241
        }
242
        mSelectedTabIndex = item;
243
        mViewPager.setCurrentItem(item);
244
 
245
        final int tabCount = mTabLayout.getChildCount();
246
        for (int i = 0; i < tabCount; i++) {
247
            final View child = mTabLayout.getChildAt(i);
248
            final boolean isSelected = (i == item);
249
            child.setSelected(isSelected);
250
            if (isSelected) {
251
                animateToTab(item);
252
            }
253
        }
254
    }
255
 
256
    @Override
257
    public void setOnPageChangeListener(OnPageChangeListener listener) {
258
        mListener = listener;
259
    }
260
 
261
    private class TabView extends TextView {
262
        private int mIndex;
263
 
264
        public TabView(Context context) {
265
            super(context, null, R.attr.vpiTabPageIndicatorStyle);
266
        }
267
 
268
        @Override
269
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
270
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
271
 
272
            // Re-measure if we went beyond our maximum size.
273
            if (mMaxTabWidth > 0 && getMeasuredWidth() > mMaxTabWidth) {
274
                super.onMeasure(MeasureSpec.makeMeasureSpec(mMaxTabWidth, MeasureSpec.EXACTLY),
275
                        heightMeasureSpec);
276
            }
277
        }
278
 
279
        public int getIndex() {
280
            return mIndex;
281
        }
282
    }
283
}