1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
/**
*
* Displays a list of tab labels representing each page in the parent's tab
* collection. The container object for this widget is
* {@link android.widget.TabHost TabHost}. When the user selects a tab, this
* object sends a message to the parent container, TabHost, to tell it to switch
* the displayed page. You typically won't use many methods directly on this
* object. The container TabHost is used to add labels, add the callback
* handler, and manage callbacks. You might call this object to iterate the list
* of tabs, or to tweak the layout of the tab list, but most methods should be
* called on the containing TabHost object.
*/
public class TabWidget extends LinearLayout implements OnFocusChangeListener {
private OnTabSelectionChanged mSelectionChangedListener;
private int mSelectedTab = 0;
private Drawable mBottomLeftStrip;
private Drawable mBottomRightStrip;
private boolean mStripMoved;
public TabWidget(Context context) {
this(context, null);
}
public TabWidget(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.tabWidgetStyle);
}
public TabWidget(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
initTabWidget();
TypedArray a =
context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TabWidget,
defStyle, 0);
a.recycle();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mStripMoved = true;
super.onSizeChanged(w, h, oldw, oldh);
}
private void initTabWidget() {
setOrientation(LinearLayout.HORIZONTAL);
mBottomLeftStrip = mContext.getResources().getDrawable(
com.android.internal.R.drawable.tab_bottom_left);
mBottomRightStrip = mContext.getResources().getDrawable(
com.android.internal.R.drawable.tab_bottom_right);
// Deal with focus, as we don't want the focus to go by default
// to a tab other than the current tab
setFocusable(true);
setOnFocusChangeListener(this);
}
@Override
public void childDrawableStateChanged(View child) {
if (child == getChildAt(mSelectedTab)) {
// To make sure that the bottom strip is redrawn
invalidate();
}
super.childDrawableStateChanged(child);
}
@Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
View selectedChild = getChildAt(mSelectedTab);
mBottomLeftStrip.setState(selectedChild.getDrawableState());
mBottomRightStrip.setState(selectedChild.getDrawableState());
if (mStripMoved) {
Rect selBounds = new Rect(); // Bounds of the selected tab indicator
selBounds.left = selectedChild.getLeft();
selBounds.right = selectedChild.getRight();
final int myHeight = getHeight();
mBottomLeftStrip.setBounds(
Math.min(0, selBounds.left
- mBottomLeftStrip.getIntrinsicWidth()),
myHeight - mBottomLeftStrip.getIntrinsicHeight(),
selBounds.left,
getHeight());
mBottomRightStrip.setBounds(
selBounds.right,
myHeight - mBottomRightStrip.getIntrinsicHeight(),
Math.max(getWidth(),
selBounds.right + mBottomRightStrip.getIntrinsicWidth()),
myHeight);
mStripMoved = false;
}
mBottomLeftStrip.draw(canvas);
mBottomRightStrip.draw(canvas);
}
/**
* Sets the current tab.
* This method is used to bring a tab to the front of the Widget,
* and is used to post to the rest of the UI that a different tab
* has been brought to the foreground.
*
* Note, this is separate from the traditional "focus" that is
* employed from the view logic.
*
* For instance, if we have a list in a tabbed view, a user may be
* navigating up and down the list, moving the UI focus (orange
* highlighting) through the list items. The cursor movement does
* not effect the "selected" tab though, because what is being
* scrolled through is all on the same tab. The selected tab only
* changes when we navigate between tabs (moving from the list view
* to the next tabbed view, in this example).
*
* To move both the focus AND the selected tab at once, please use
* {@link #setCurrentTab}. Normally, the view logic takes care of
* adjusting the focus, so unless you're circumventing the UI,
* you'll probably just focus your interest here.
*
* @param index The tab that you want to indicate as the selected
* tab (tab brought to the front of the widget)
*
* @see #focusCurrentTab
*/
public void setCurrentTab(int index) {
if (index < 0 || index >= getChildCount()) {
return;
}
getChildAt(mSelectedTab).setSelected(false);
mSelectedTab = index;
getChildAt(mSelectedTab).setSelected(true);
mStripMoved = true;
}
/**
* Sets the current tab and focuses the UI on it.
* This method makes sure that the focused tab matches the selected
* tab, normally at {@link #setCurrentTab}. Normally this would not
* be an issue if we go through the UI, since the UI is responsible
* for calling TabWidget.onFocusChanged(), but in the case where we
* are selecting the tab programmatically, we'll need to make sure
* focus keeps up.
*
* @param index The tab that you want focused (highlighted in orange)
* and selected (tab brought to the front of the widget)
*
* @see #setCurrentTab
*/
public void focusCurrentTab(int index) {
final int oldTab = mSelectedTab;
// set the tab
setCurrentTab(index);
// change the focus if applicable.
if (oldTab != index) {
getChildAt(index).requestFocus();
}
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
int count = getChildCount();
for (int i=0; i<count; i++) {
View child = getChildAt(i);
child.setEnabled(enabled);
}
}
@Override
public void addView(View child) {
if (child.getLayoutParams() == null) {
final LinearLayout.LayoutParams lp = new LayoutParams(
0,
ViewGroup.LayoutParams.WRAP_CONTENT, 1);
lp.setMargins(0, 0, 0, 0);
child.setLayoutParams(lp);
}
// Ensure you can navigate to the tab with the keyboard, and you can touch it
child.setFocusable(true);
child.setClickable(true);
super.addView(child);
// TODO: detect this via geometry with a tabwidget listener rather
// than potentially interfere with the view's listener
child.setOnClickListener(new TabClickListener(getChildCount() - 1));
child.setOnFocusChangeListener(this);
}
/**
* Provides a way for {@link TabHost} to be notified that the user clicked on a tab indicator.
*/
void setTabSelectionListener(OnTabSelectionChanged listener) {
mSelectionChangedListener = listener;
}
public void onFocusChange(View v, boolean hasFocus) {
if (v == this && hasFocus) {
getChildAt(mSelectedTab).requestFocus();
return;
}
if (hasFocus) {
int i = 0;
while (i < getChildCount()) {
if (getChildAt(i) == v) {
setCurrentTab(i);
mSelectionChangedListener.onTabSelectionChanged(i, false);
break;
}
i++;
}
}
}
// registered with each tab indicator so we can notify tab host
private class TabClickListener implements OnClickListener {
private final int mTabIndex;
private TabClickListener(int tabIndex) {
mTabIndex = tabIndex;
}
public void onClick(View v) {
mSelectionChangedListener.onTabSelectionChanged(mTabIndex, true);
}
}
/**
* Let {@link TabHost} know that the user clicked on a tab indicator.
*/
static interface OnTabSelectionChanged {
/**
* Informs the TabHost which tab was selected. It also indicates
* if the tab was clicked/pressed or just focused into.
*
* @param tabIndex index of the tab that was selected
* @param clicked whether the selection changed due to a touch/click
* or due to focus entering the tab through navigation. Pass true
* if it was due to a press/click and false otherwise.
*/
void onTabSelectionChanged(int tabIndex, boolean clicked);
}
}
|