To manually flip the views, we need to hook into the Button
and flip them ourselves when the button is clicked:
public classFlipperDemo extendsActivity {
ViewFlipper flipper;
@Override
publicvoid onCreate(Bundle icicle) {
super. onCreate(icicle);
setContentView(R.layout.main);
flipper = (ViewFlipper) findViewById(R.id.details);
Button btn = (Button) findViewById(R.id.flip_me);
btn. setOnClickListener( newView. OnClickListener() {
publicvoid onClick(View view) {
flipper. showNext();
}
});
}
}
This is just a matter of calling showNext()
on the ViewFlipper
, like you can on any ViewAnimator
class.
The result is a trivial activity: click the button, and the next TextView
in sequence is displayed, wrapping around to the first after viewing the last (see Figures 10-9 and 10-10).
Figure 10-9. The Flipper1 application, showing the first panel
Figure 10-10. The same application, after switching to the second panel
This, of course, could be handled more simply by having a single TextView
and changing the text and color on each click. However, you can imagine that the ViewFlipper
contents could be much more complicated, like the contents you might put into a TabView
.
As with the TabWidget
, sometimes, your ViewFlipper
contents may not be known at compile time. As with TabWidget
, though, you can add new contents on the fly with ease.
For example, let’s look at another sample activity ( Fancy/Flipper2
), using this layout:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
Notice that the ViewFlipper
has no contents at compile time. Also note that there is no Button
for flipping between the contents — more on this in a moment.
For the ViewFlipper
contents, we will create large Button
widgets, each containing one of a set of random words. And, we will set up the ViewFlipper
to automatically rotate between the Button
widgets, using an animation for transition:
public classFlipperDemo2 extendsActivity {
staticString[] items={"lorem", "ipsum", "dolor", "sit", "amet",
"consectetuer", "adipiscing", "elit",
"morbi", "vel", "ligula", "vitae",
"arcu", "aliquet", "mollis", "etiam",
"vel", "erat", "placerat", "ante",
"porttitor", "sodales", "pellentesque",
"augue", "purus"};
ViewFlipper flipper;
@Override
publicvoid onCreate(Bundle icicle) {
super. onCreate(icicle);
setContentView(R.layout.main);
flipper = (ViewFlipper) findViewById(R.id.details);
flipper. setInAnimation(AnimationUtils. loadAnimation( this,
R.anim.push_left_in));
flipper. setOutAnimation(AnimationUtils. loadAnimation( this,
R.anim.push_left_out));
for(String item : items) {
Button btn = new Button( this);
btn. setText(item);
flipper. addView(btn,
newViewGroup. LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
}
flipper. setFlipInterval(2000);
flipper. startFlipping();
}
}
After getting our ViewFlipper
widget from the layout, we first set up the “in” and “out” animations. In Android terms, an animation is a description of how a widget leaves (”out”) or enters (”in”) the viewable area. Animations are a complex beast, eventually worthy of their own chapter but not covered in this text. For now, realize that animations are resources, stored in res/anim/
in your project. For this example, we are using a pair of animations supplied by the SDK samples, available under the Apache 2.0 License. As their names suggest, widgets are “pushed” to the left, either to enter or leave the viewable area.
After iterating over the funky words, turning each into a Button
, and adding the Button
as a child of the ViewFlipper
, we set up the flipper to automatically flip between children ( flipper.setFlipInterval(2000);
) and to start flipping ( flipper.startFlipping();
).
The result is an endless series of buttons, each appearing, then sliding out to the left after 2 seconds, being replaced by the next button in sequence, wrapping around to the first after the last has been shown (see Figure 10-11).
Figure 10-11. The Flipper2 application, showing an animated transition
The auto-flipping ViewFlipper
is useful for status panels or other situations where you have a lot of information to display, but not much room. The key is that, since it automatically flips between views, expecting users to interact with individual views is dicey — the view might switch away part-way through their interaction.
Android offers AbsoluteLayout
, where the contents are laid out based on specific coordinate positions. You tell AbsoluteLayout
where to place a child in precise X,Y coordinates, and Android puts it there, no questions asked. On the plus side, this gives you precise positioning. On the minus side, it means your views will only look “right” on screens of a certain dimension, or it requires you to write a bunch of code to adjust the coordinates based on screen size. Since Android screens might run the gamut of sizes, plus have new sizes crop up periodically, using AbsoluteLayout
could get quite annoying.
Android also has a new flavor of list, the ExpandableListView
. This provides a simplified tree representation, supporting two levels of depth: groups and children. Groups contain children; children are “leaves” of the tree. This requires a new set of adapters, since the ListAdapter
family does not provide any sort of group information for the items in the list.
Читать дальше