Fortunately, Android also supports adding tabs dynamically at runtime.
Adding tabs dynamically at runtime works much like the compile-time tabs previously shown, except you use a different flavor of setContent()
, one that takes a TabHost.TabContentFactory
instance. This is just a callback that will be invoked — you provide an implementation of createTabContent()
and use it to build and return the Let’s take a look at an example ( Fancy/DynamicTab
).
First, here is some layout XML for an activity that sets up the tabs and defines one tab, containing a single button:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="62px">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button"
/>
What we want to do is add new tabs whenever the button is clicked. That can be accomplished in just a few lines of code:
public classDynamicTabDemo extendsActivity {
@Override
publicvoid onCreate(Bundle icicle) {
super. onCreate(icicle);
setContentView(R.layout.main);
finalTabHost tabs = (TabHost) findViewById(R.id.tabhost);
tabs. setup();
TabHost.TabSpec spec = tabs. newTabSpec(buttontab);
spec. setContent(R.id.buttontab);
spec. setIndicator(Button);
tabs. addTab(spec);
tabs. setCurrentTab(0);
Button btn = (Button)tabs. getCurrentView(). findViewById(R.id.buttontab);
btn. setOnClickListener( newView. OnClickListener() {
publicvoid onClick(View view) {
TabHost.TabSpec spec = tabs. newTabSpec(tag1);
spec. setContent( newTabHost. TabContentFactory() {
publicView createTabContent(String tag) {
return( new AnalogClock(DynamicTabDemo. this));
}
});
spec. setIndicator(Clock);
tabs. addTab(spec);
}
});
}
}
In our button’s setOnClickListener()
callback, we create a TabHost.TabSpec
object and give it an anonymous TabHost.TabContentFactory
. The factory, in turn, returns the View
to be used for the tab — in this case, just an AnalogClock
. The logic for constructing the tab’s View could be much more elaborate, such as using LayoutInflater
to construct a view from layout XML.
In Figure 10-7 you can see that initially, when the activity is launched, we just have the one tab whereas Figure 10-8 shows multiple tabs.
Figure 10-7. The DynamicTab application, with the single initial tab
Figure 10-8. The DynamicTab application, with three dynamically-created tabs
In the preceding examples, the contents of each tab were set to be a View
, such as a Button
. This is easy and straight-forward, but it is not the only option. You can also integrate another activity from your application via an Intent
.
Intents
are ways of specifying something you want accomplished, then telling Android to go find something to accomplish it. Frequently, these are used to cause activities to spawn. For example, whenever you launch an application from the main Android application launcher, the launcher creates an Intent and has Android open up the activity associated with that Intent
. This whole concept, and how activities can be placed in tabs, will be described in greater detail in Chapter 25.
Sometimes, you want the overall effect of tabs (only some Views visible
at a time), but you do not want the actual UI implementation of tabs. Maybe the tabs take up too much screen space. Maybe you want to switch between perspectives based on a gesture or a device shake. Or maybe you just like being different.
The good news is that the guts of the view-flipping logic from tabs can be found in the ViewFlipper
container, which can be used in other ways than the traditional tab.
ViewFlipper
inherits from FrameLayout
, just like we used to describe the innards of a TabWidget
. However, initially, it just shows the first child view. It is up to you to arrange for the views to flip, either manually by user interaction, or automatically via a timer.
For example, here is a layout for a simple activity ( Fancy/Flipper1
) using a Button
and a ViewFlipper
:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Flip Me!"
/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FF00FF00"
android:text="This is the first panel"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FFFF0000"
android:text="This is the second panel"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#FFFFFF00"
android:text="This is the third panel"
/>
Notice that the layout defines three child views for the ViewFlipper
, each a TextView
with a simple message. Of course, you could have very complicated child views, if you so chose.
Читать дальше