| 23410 |
tejbeer |
1 |
/*
|
|
|
2 |
* Copyright (C) 2011 The Android Open Source Project
|
|
|
3 |
* Copyright (C) 2012 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.Gravity;
|
|
|
25 |
import android.view.View;
|
|
|
26 |
import android.widget.HorizontalScrollView;
|
|
|
27 |
import android.widget.ImageView;
|
|
|
28 |
|
|
|
29 |
import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
|
|
|
30 |
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* This widget implements the dynamic action bar tab behavior that can change
|
|
|
34 |
* across different configurations or circumstances.
|
|
|
35 |
*/
|
|
|
36 |
public class IconPageIndicator extends HorizontalScrollView implements PageIndicator {
|
|
|
37 |
private final IcsLinearLayout mIconsLayout;
|
|
|
38 |
|
|
|
39 |
private ViewPager mViewPager;
|
|
|
40 |
private OnPageChangeListener mListener;
|
|
|
41 |
private Runnable mIconSelector;
|
|
|
42 |
private int mSelectedIndex;
|
|
|
43 |
|
|
|
44 |
public IconPageIndicator(Context context) {
|
|
|
45 |
this(context, null);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public IconPageIndicator(Context context, AttributeSet attrs) {
|
|
|
49 |
super(context, attrs);
|
|
|
50 |
setHorizontalScrollBarEnabled(false);
|
|
|
51 |
|
|
|
52 |
mIconsLayout = new IcsLinearLayout(context, R.attr.vpiIconPageIndicatorStyle);
|
|
|
53 |
addView(mIconsLayout, new LayoutParams(WRAP_CONTENT, FILL_PARENT, Gravity.CENTER));
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
private void animateToIcon(final int position) {
|
|
|
57 |
final View iconView = mIconsLayout.getChildAt(position);
|
|
|
58 |
if (mIconSelector != null) {
|
|
|
59 |
removeCallbacks(mIconSelector);
|
|
|
60 |
}
|
|
|
61 |
mIconSelector = new Runnable() {
|
|
|
62 |
public void run() {
|
|
|
63 |
final int scrollPos = iconView.getLeft() - (getWidth() - iconView.getWidth()) / 2;
|
|
|
64 |
smoothScrollTo(scrollPos, 0);
|
|
|
65 |
mIconSelector = null;
|
|
|
66 |
}
|
|
|
67 |
};
|
|
|
68 |
post(mIconSelector);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
@Override
|
|
|
72 |
public void onAttachedToWindow() {
|
|
|
73 |
super.onAttachedToWindow();
|
|
|
74 |
if (mIconSelector != null) {
|
|
|
75 |
// Re-post the selector we saved
|
|
|
76 |
post(mIconSelector);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
@Override
|
|
|
81 |
public void onDetachedFromWindow() {
|
|
|
82 |
super.onDetachedFromWindow();
|
|
|
83 |
if (mIconSelector != null) {
|
|
|
84 |
removeCallbacks(mIconSelector);
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
@Override
|
|
|
89 |
public void onPageScrollStateChanged(int arg0) {
|
|
|
90 |
if (mListener != null) {
|
|
|
91 |
mListener.onPageScrollStateChanged(arg0);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
@Override
|
|
|
96 |
public void onPageScrolled(int arg0, float arg1, int arg2) {
|
|
|
97 |
if (mListener != null) {
|
|
|
98 |
mListener.onPageScrolled(arg0, arg1, arg2);
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
@Override
|
|
|
103 |
public void onPageSelected(int arg0) {
|
|
|
104 |
setCurrentItem(arg0);
|
|
|
105 |
if (mListener != null) {
|
|
|
106 |
mListener.onPageSelected(arg0);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
@Override
|
|
|
111 |
public void setViewPager(ViewPager view) {
|
|
|
112 |
if (mViewPager == view) {
|
|
|
113 |
return;
|
|
|
114 |
}
|
|
|
115 |
if (mViewPager != null) {
|
|
|
116 |
mViewPager.setOnPageChangeListener(null);
|
|
|
117 |
}
|
|
|
118 |
PagerAdapter adapter = view.getAdapter();
|
|
|
119 |
if (adapter == null) {
|
|
|
120 |
throw new IllegalStateException("ViewPager does not have adapter instance.");
|
|
|
121 |
}
|
|
|
122 |
mViewPager = view;
|
|
|
123 |
view.setOnPageChangeListener(this);
|
|
|
124 |
notifyDataSetChanged();
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
public void notifyDataSetChanged() {
|
|
|
128 |
mIconsLayout.removeAllViews();
|
|
|
129 |
IconPagerAdapter iconAdapter = (IconPagerAdapter) mViewPager.getAdapter();
|
|
|
130 |
int count = iconAdapter.getCount();
|
|
|
131 |
for (int i = 0; i < count; i++) {
|
|
|
132 |
ImageView view = new ImageView(getContext(), null, R.attr.vpiIconPageIndicatorStyle);
|
|
|
133 |
view.setImageResource(iconAdapter.getIconResId(i));
|
|
|
134 |
mIconsLayout.addView(view);
|
|
|
135 |
}
|
|
|
136 |
if (mSelectedIndex > count) {
|
|
|
137 |
mSelectedIndex = count - 1;
|
|
|
138 |
}
|
|
|
139 |
setCurrentItem(mSelectedIndex);
|
|
|
140 |
requestLayout();
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
@Override
|
|
|
144 |
public void setViewPager(ViewPager view, int initialPosition) {
|
|
|
145 |
setViewPager(view);
|
|
|
146 |
setCurrentItem(initialPosition);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
@Override
|
|
|
150 |
public void setCurrentItem(int item) {
|
|
|
151 |
if (mViewPager == null) {
|
|
|
152 |
throw new IllegalStateException("ViewPager has not been bound.");
|
|
|
153 |
}
|
|
|
154 |
mSelectedIndex = item;
|
|
|
155 |
mViewPager.setCurrentItem(item);
|
|
|
156 |
|
|
|
157 |
int tabCount = mIconsLayout.getChildCount();
|
|
|
158 |
for (int i = 0; i < tabCount; i++) {
|
|
|
159 |
View child = mIconsLayout.getChildAt(i);
|
|
|
160 |
boolean isSelected = (i == item);
|
|
|
161 |
child.setSelected(isSelected);
|
|
|
162 |
if (isSelected) {
|
|
|
163 |
animateToIcon(item);
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
@Override
|
|
|
169 |
public void setOnPageChangeListener(OnPageChangeListener listener) {
|
|
|
170 |
mListener = listener;
|
|
|
171 |
}
|
|
|
172 |
}
|