ViewFlipper inherits from frame layout, so it displays a single view at a time.
consider this layout:
<?xml version="1.0" encoding="utf-8"?>just a ViewFlipper container that contains three text views
<LinearLayout
android_orientation="vertical"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_text="@string/hello"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Flip"
android_id="@+id/btn"
android_onClick="ClickHandler"
/>
<ViewFlipper
android_layout_width="fill_parent"
android_layout_height="fill_parent"
android_id="@+id/flip"
>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_text="Item1"
/>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_text="Item2"
/>
<TextView
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_text="Item3"
/>
</ViewFlipper>
</LinearLayout>
now we want to flip the views when the button is clicked
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
flip=(ViewFlipper)findViewById(R.id.flip);
}
public void ClickHandler(View v)
{
flip.showNext();
}
if we want to flip in reverese direction we could use
flip.showPrevious() instead
we can add animations to the child views when they appear or disappear:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
flip=(ViewFlipper)findViewById(R.id.flip);
//when a view is displayed
flip.setInAnimation(this,android.R.anim.fade_in);
//when a view disappears
flip.setOutAnimation(this, android.R.anim.fade_out);
}
we can also set the ViewFlipper to flip views automatically when the button is clicked:
public void ClickHandler(View v)
{
//specify flipping interval
flip.setFlipInterval(1000);
flip.startFlipping();
}
we can stop the flipping by calling flip.stopFlipping(); method.
or we can set the flipper to flip autommatically when the activity starts
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
flip=(ViewFlipper)findViewById(R.id.flip);
flip.setInAnimation(this,android.R.anim.fade_in);
flip.setOutAnimation(this, android.R.anim.fade_out);
flip.setFlipInterval(1000);
flip.setAutoStart(true);
}
0 comments:
Post a Comment